【问题标题】:Download file with PHP and Curl, without knowing the file full path在不知道文件完整路径的情况下使用 PHP 和 Curl 下载文件
【发布时间】:2018-06-14 20:37:51
【问题描述】:

我需要一些帮助来解决以下问题。 我的php网页我需要从网上下载一个excel文件,但我没有文件路径,当我输入url链接时下载开始,例如:www.example.com/prices。 输入该网址开始下载。 有什么帮助吗? 我没有尝试过卷曲。有什么代码可以帮忙吗?

//DESCARGA DE ARCHIVO
libxml_use_internal_errors(true);     
$url = "http://www.distribuidoraidem.com.ar/precios";
$filecontent = file_get_contents($url);
$tmpfname = tempnam(sys_get_temp_dir(),"tmpxls");
file_put_contents($tmpfname,$filecontent);

之前我使用 curl 在网络上进行了登录,所以如果您尝试该代码将无法正常工作。 谢谢!

【问题讨论】:

  • 向我们展示您尝试过的代码
  • 如果您向尝试从不同来源下载文件的页面发出 cURL 请求,则 cURL 也会尝试下载该文件。只需将您的 cURL 请求指向 www.example.com/prices
  • 在 php.net/curl_setopt 查看CURLOPT_FOLLOWLOCATION,您可以在 curl 请求中使用它来跟踪重定向到实际下载。

标签: php file curl download


【解决方案1】:

您可以尝试以下方法。

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,'www.example.com/prices');
    $fp = fopen('prices.xlsx', 'w+');
    curl_setopt($ch, CURLOPT_FILE, $fp);
    curl_exec ($ch);
    curl_close ($ch);
    fclose($fp);

【讨论】:

  • 但是文件名每天都在变,我不知道他的名字
  • 看来你可以随心所欲地把名字放在你身边。我会像这样在文件中添加一个日期: fopen('prices-'.date("Y-m-d") .'.xlsx', 'w+');
  • 不可能这样。文件名并非整天都更改。如果只尝试扩展名?还是这个函数需要全名?
猜你喜欢
  • 1970-01-01
  • 2015-07-08
  • 2020-12-08
  • 1970-01-01
  • 2020-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多