2013-04-21 40 views
0

下面的代碼在我的本地主機測試機器上工作,但我需要知道如何讓它在外部服務器上工作。我需要能夠下載.csv文件。有沒有一種方法可以提示用戶下載文件,或者我必須在服務器上選擇一個位置?在外部服務器上下載CSV文件

<?php 
    session_start(); 
    require ("../login.php"); 

    $success = false; 

    include ("header.php"); 
    include ("adminnav.php"); 
?> 

    <h2>Download Previews Totals</h2> 

<?php 



    $stmt = $db->prepare 
    ("SELECT s.short_name, p.name, t.title, SUM(sub.quantity) AS quantity 
    FROM store s 
    JOIN users u 
     ON s.short_name = u.store 
    JOIN subscriptions sub 
     ON u.id = sub.user 
    JOIN titles t 
     ON sub.title = t.id 
    JOIN publishers p 
     ON t.publisher = p.name 
    GROUP BY s.short_name, p.name, t.title 
    ORDER BY s.short_name, p.name, t.title"); 

    if ($stmt->execute()) { 
     $rows = $stmt->fetchAll(); 

     // Pick a filename and destination directory for the file 
     // Remember that the folder where you want to write the file has to be writable 
     $filename = "C:/Previews_Files/previews".time().".csv"; 

     // Actually create the file 
     // The w+ parameter will wipe out and overwrite any existing file with the same name 
     $handle = fopen($filename, 'w+'); 
     if (!$handle) 
      $errors[] = '<p>File failed to open'; 
     else { 
      // Write the spreadsheet column titles/labels 
      fputcsv($handle, array('Store','Publisher', 'Title', 'Quantity')); 

      // Write all the user records to the spreadsheet 
      foreach($rows as $row) 
      { 
       fputcsv($handle, array($row['short_name'], $row['name'], $row['title'], $row['quantity'])); 
      } 

      // Finish writing the file 
      fclose($handle); 
      $success = true; 
     } 
    } 
    else 
     $errors[] = '<p>There are no previews totals to display.</p>'; 

    if ($success == true) 
     echo '<p>Your file has successfully been downloaded to \'C:/Previews_Files\'</p>'; 
    else 
     $errors[] = '<p>Your file could not be downloaded. Please make sure the directory \'C:/Previews_Files\' has been created and is writeable.'; 

     if (!empty($errors)) 
      foreach($errors as $error) 
       echo $error; 
?> 


<?php 
    include ("footer.php"); 
?> 

回答

0

您可能使用CURL從網上抓取數據。 CURL可以做很多事情。

http://www.php.net/manual/en/intro.curl.php

但是,你需要把它安裝在你的服務器/ VPS。你不能共享主機。由於CURL默認情況下未安裝。

+0

我想只使用PHP本身,如果可能的話。 – JimRomeFan 2013-04-21 01:37:03

相關問題