Page 1 of 1

retrieve a http file via php and save to ftp

Posted: Wed Sep 20, 2006 4:46 pm
by duke
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi all

Sorry in advance I am newbie.

I'm trying to put together a little php script that will retrieve a file at a http link and then download it to ftp

Code: Select all

<?php
$file = fopen ("ftp://change.me.com", "w");
if (!$file) {
   echo "<p>Unable to open remote file for writing.\n";
   exit;
}
/* Write the data here. */
fwrite ($file, $_SERVER['http_get changeme] . "\n");
fclose ($file);
?>
Any advice/help much appreciated


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Wed Sep 20, 2006 4:56 pm
by Luke
I'm not sure I know exactly what you are trying to do, but these function may be of assistance:
http://us3.php.net/manual/en/ref.ftp.php

Posted: Wed Sep 20, 2006 5:54 pm
by duke
Thanks that was helpful. to an extent.

I'm trying to pull a file (say http://www.example.com/misc/file.avi)

And put it on my ftp. Via php and executed through a cron job on my cpanel server...

I can handle the cron job part, but my php abilities suck

crazy ?

Posted: Wed Sep 20, 2006 6:10 pm
by Luke
so where's the problem? ftp function should be able to connect to the server and download the file. The link I posted has a list of the functions you will need to use.

I'm not going to write the code for you, but maybe this will help... here's what you need to do:

connect to the remote server
get the file (download it to your server... which is what I'm guessing you mean by "my ftp")
close the connection

There should be functions listed on that page to do all three of those things

BUT... these functions will only work if you have ftp access to the remote server... do you have ftp access to the remote server?

Posted: Wed Sep 20, 2006 6:27 pm
by duke
Thanks again for the reply

The issue being that the target file is only avaliable via http, and is hosted on a 3rd party site.

This may not be possible at all.

So the script would run as a cron job and will take the http file and put it either in http or ftp on my server.

Hope this makes sense

Posted: Wed Sep 20, 2006 6:45 pm
by Luke
OK I see... well I think probably file_get_contents() (or fopen()) would work for that... try it. Use file_get_contents to load the file into variable and then write that variable to a local file... ? I'm not really sure if that would work or not.

Posted: Wed Sep 20, 2006 8:28 pm
by duke
How does this look ?

Code: Select all

<?php

$source_file = file_get_contents("http://www.example.com/file.wmv");

// check get file status
if (!$source_file) {
       echo "Retrieve file has failed!";
   } else {
       echo "Retrieved $source_file";
   }

$destination_file = "";

$ftp_server = "blah.com";

$ftp_user_name = "user";

$ftp_user_pass = "password";

// set up basic connection
$conn_id = ftp_connect($ftp_server);

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

// check connection
if ((!$conn_id) || (!$login_result)) {
       echo "FTP connection has failed!";
       echo "Attempted to connect to $ftp_server for user $ftp_user_name";
       exit;
   } else {
       echo "Connected to $ftp_server, for user $ftp_user_name";
   }

// upload the file
$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY);

// check upload status
if (!$upload) {
       echo "FTP upload has failed!";
   } else {
       echo "Uploaded $source_file to $ftp_server as $destination_file";
   }

// close the FTP stream
ftp_close($conn_id);
?>
if file size is an issue I may try to implement something like

Code: Select all

function readfile_chunked($filename,$retbytes=true) {
   $chunksize = 1*(1024*1024); // how many bytes per chunk
   $buffer = '';
   $cnt =0;
   // $handle = fopen($filename, 'rb');
   $handle = fopen($filename, 'rb');
   if ($handle === false) {
       return false;
   }
   while (!feof($handle)) {
       $buffer = fread($handle, $chunksize);
       echo $buffer;
       if ($retbytes) {
           $cnt += strlen($buffer);
       }
   }
       $status = fclose($handle);
   if ($retbytes && $status) {
       return $cnt; // return num. bytes delivered like readfile() does.
   }
   return $status;

}