View Full Version : can you use fput() to upload an external URL to your server?

03-01-2008, 05:27 PM
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:

$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";
} 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

interestingly I get the following output:

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?


03-01-2008, 05:29 PM
Its likely they don't accept files from external sites. Just be advised that you are opening yourself up to a lot of security issues when doing this. Merely renaming the file may not be enough. Here is a good read http://www.eatmybusiness.com/food/2007/10/25/php-using-fopen-instead-of-upload-to-import-external-images-to-website-over-http/49/

03-01-2008, 10:33 PM
that was a very informative read

i learned a lot

i also learned that i wont do what i was planning to do due to the security risks

uploading pictures is enough for me! thanks!