[PHP] Downloading file from Server and saving to localhost

  • Zealous
  • Guru
  • Guru
  • User avatar
  • Posts: 1240
  • Loc: Sydney

Post 3+ Months Ago

I am building a script to take a CSV and download that to the scripts localhost and then import it into the database and make several DB changes. But i am stuck on a simple issue at the start where i can't seam to find much help using a browser to download the file from another host back to the script and save it on the localhost where the script is running.

still searching the php manual but figured someone mite have a little insight on where to take this.
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • ScottG
  • Proficient
  • Proficient
  • ScottG
  • Posts: 448

Post 3+ Months Ago

Would cURL be out of the question? If I understood correctly the CSV is on another server so I would do something like

PHP Code: [ Select ]
// create a new cURL resource
$ch = curl_init();
 
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/"); // Path to your CSV file
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 
// grab URL and pass it to the browser
$result = curl_exec($ch);
 
// close cURL resource, and free up system resources
curl_close($ch);
 
// Now if all went as planned $results should contain the CSV info
// Lets make a file ;)
$fp = fopen('data.txt', 'w');
fwrite($fp, $result);
fclose($fp);
 
  1. // create a new cURL resource
  2. $ch = curl_init();
  3.  
  4. // set URL and other appropriate options
  5. curl_setopt($ch, CURLOPT_URL, "http://www.example.com/"); // Path to your CSV file
  6. curl_setopt($ch, CURLOPT_HEADER, false);
  7. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  8.  
  9. // grab URL and pass it to the browser
  10. $result = curl_exec($ch);
  11.  
  12. // close cURL resource, and free up system resources
  13. curl_close($ch);
  14.  
  15. // Now if all went as planned $results should contain the CSV info
  16. // Lets make a file ;)
  17. $fp = fopen('data.txt', 'w');
  18. fwrite($fp, $result);
  19. fclose($fp);
  20.  

Post Information

  • Total Posts in this topic: 2 posts
  • Users browsing this forum: No registered users and 118 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.