简体   繁体   中英

download file to my server with php with a url without file name

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.";
    }
?>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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