Page 1 of 1

can you use fput() to upload an external URL to your server?

Posted: Sat Mar 01, 2008 10:27 am
by bobafart
I have a form where users submit images. They can upload an image or they can point to an external URL for the image.

If they point to an external URL for the image in my form I am trying to upload that image to my server. My host doesn't allow fopen() but does allow php ftp() functions like fput() (go figure??)

Here is my code:

Code: Select all

 
 
<?php
$pixelURL='http://www.bluesnews.com/images/bluesLogo.gif'; // example external image
$ftp_server = 'ftp.com';
$ftp_user_name = 'myusername';
$ftp_user_pass = 'mypassword';
$conn_id = ftp_connect($ftp_server);
$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";
    }
$target_path = "/home/mypath/www/img/subdir/";
$destination_file = $target_path . 'fakefilename.gif';
// upload the file
$upload = ftp_put($conn_id, $destination_file, $pixelURL, FTP_BINARY); 
echo '<p>'.print_r($upload).'</p>';
// 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);
?>
 
 
interestingly I get the following output:

Code: Select all

 
Connected to ftp.com, for user username
 
1 // output of print_r($upload)
 
FTP upload has failed!
 
I can connect but I can't upload. The subdir is chmod 777 by the way.

I assume it is failing because the sourcefile is a URI... is this true?

thanks

Re: can you use fput() to upload an external URL to your server?

Posted: Sat Mar 01, 2008 1:06 pm
by Sekka
You trying to create an FTP connection to your own server and copy the image from a URI to your server? This won't work as the item to be sent via ftp_put() must me a local file path, and not a URI.

I think fopen() is the only way you can do this.