带有没有文件名的URL的PHP​​将文件下载到我的服务器

发布于 2025-01-29 00:17:51 字数 505 浏览 3 评论 0原文

我可以通过固定的URL下载CSV文件。 URL只是没有文件名。 当我在浏览器中打开URL时,会生成文件导出。CSV,大约10秒后,下载开始。 现在,我想将此导出CSV文件直接下载到我自己的服务器,

我有以下代码,但在我的情况下它不起作用。 你能帮助我吗?我的感谢很大

<?php
  
    
    $url = 
    'https://www.example.com/product-export/link123456789';
      
   
    $file_name = basename($url);
      
   
    if (file_put_contents($file_name, file_get_contents($url)))
    {
        echo "File downloaded successfully";
    }
    else
    {
        echo "File downloading failed.";
    }
?>

I can download a csv file via a fixed url. The url just doesn't have a file name.
When I open the url in a browser, the file export.csv is generated and after about 10 sec the download starts.
Now I want to download this export csv file directly to my own server,

I have the code below, but it doesn't work in my case.
Can you help me? my thanks are big

<?php
  
    
    $url = 
    'https://www.example.com/product-export/link123456789';
      
   
    $file_name = basename($url);
      
   
    if (file_put_contents($file_name, file_get_contents($url)))
    {
        echo "File downloaded successfully";
    }
    else
    {
        echo "File downloading failed.";
    }
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鹿! 2025-02-05 00:17:51

由于生成的输出文件名始终是导出的。csv,我们可以使用以下来指定文件名:(

$file_name = dirname(__FILE__) . "/export.csv";    

确保目录已写入 - 以便PHP可以保存文件“ export.csv”)

因此,代码将是:

<?php
      
  $url = 'http://www.createchhk.com/SOanswers/export1a.csv';
         
//$file_name = basename($url);
  
  $file_name = dirname(__FILE__) . "/export.csv";    
   

  if (file_put_contents($file_name, file_get_contents($url))) {
        echo "File downloaded successfully";
        } else {
        echo "File downloading failed.";
    }
?>

对于您的cronjob,这将是:

1 1 * * * php /path_to_the_php/test.php

As the generated output filename is always export.csv, we may use the following to specify the filename:

$file_name = dirname(__FILE__) . "/export.csv";    

(make sure the directory is write-permitted so that the php can save the file "export.csv")

Hence, the code will be:

<?php
      
  $url = 'http://www.createchhk.com/SOanswers/export1a.csv';
         
//$file_name = basename($url);
  
  $file_name = dirname(__FILE__) . "/export.csv";    
   

  if (file_put_contents($file_name, file_get_contents($url))) {
        echo "File downloaded successfully";
        } else {
        echo "File downloading failed.";
    }
?>

For your cronjob, it will be something like :

1 1 * * * php /path_to_the_php/test.php
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文