简体   繁体   English

在外部服务器上下载CSV文件

[英]Download a CSV file on an external server

The code below works on my localhost test machine, but I need to know how to get it to work on an external server. 下面的代码可在我的localhost测试计算机上使用,但是我需要知道如何使它在外部服务器上运行。 I need to be able to download the .csv file. 我需要能够下载.csv文件。 Is there a way I can prompt the user to download the file or do I have to choose a location on the server? 有什么方法可以提示用户下载文件,还是必须选择服务器上的位置?

<?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");
?>

you may used CURL to grab data from web. 您可以使用CURL从Web抓取数据。 CURL can do a lot of things. CURL可以做很多事情。

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

However, you will need to installed it on your Server/VPS. 但是,您需要将其安装在服务器/ VPS上。 You can't do it on shared hosting. 您不能在共享主机上做到这一点。 As CURL is not installed by default. 由于默认情况下未安装CURL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM