【问题标题】:How Do I Set PHP to Download File to a Specific Directory?如何设置 PHP 将文件下载到特定目录?
【发布时间】:2011-10-15 07:30:46
【问题描述】:

我正在寻找有关这方面的一般指导...

我创建了一个使用 cURL 下载 csv 文件的 PHP 脚本。目前,当我运行脚本时,它会将文件下载到我的计算机上。我想修改下载路径以将其路由到我的网络主机上的目录。有没有简单的方法可以用 PHP 做到这一点?

这里是下载 CSV 文件的 PHP 脚本的快速摘要:

<?php 

$ch = curl_init(); 
//this is where I submit the form to download the csv file...
curl_close ($ch);  

header("Content-Disposition: attachment; filename=file_I_am_downloading.csv"); 
header("Content-Type: application/octet-stream"); 
readfile("http://www.example.com/file_I_am_downloading.csv");
?>

总而言之,我希望修改脚本,这样当我运行这个 PHP 文件时,它会将“file_I_am_downloading.csv”下载到我主机上的特定目录,而不是先下载到我的计算机上。感谢您抽出宝贵时间,非常感谢您提供的任何指导。

【问题讨论】:

标签: php file curl download filepath


【解决方案1】:
$ch = curl_init();
.... curl setup
$data = curl_exec($ch);
if ($data === FALSE) {
   die(curl_error($ch));
}

// do this, instead of the header/readfile business.
file_put_contents('/some/path/on/your/webhost', $data);

【讨论】:

  • 我试过了,但它给了我一个错误:警告:file_put_contents(/file/path/) [function.file-put-contents]: failed to open stream: Permission denied...我将 777 分配给目录,我不确定为什么会出现错误。不过,我会继续使用它。感谢您的输入:)
  • 您需要指定一个文件名,而不仅仅是一个目录。 /some/path/file.txt
  • 太棒了!现在效果很好:) 谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-23
  • 2011-12-06
  • 2018-01-12
  • 1970-01-01
  • 2021-08-21
相关资源
最近更新 更多