...

View Full Version : how do you copy an external file (image) URL from a form to your own server?



Bobafart
03-01-2008, 05:44 AM
I am trying to copy an image that the user inputs into a form to a local copy on my server.. and for security reasons rename the file

so basically, user inputs a URL to the image (http://www.foo.com/image.gif)
user hits submit
script copies http://www.foo.com/image.gif and renames it to a random md5 name

how would I do this using PHP?

oesxyl
03-01-2008, 09:06 AM
<?php
// this must be done somehow how you want
$filename = 'test.txt';

$handle = fopen("http://www.foo.com/", "rb");
if($handle){
$somecontent = stream_get_contents($handle);
fclose($handle);

$handle = fopen($filename, 'wb');
if($handle){
if (fwrite($handle, $somecontent) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
echo "Success, wrote ($somecontent) to file ($filename)";
fclose($handle);
}else{
echo "Cannot create file ($filename)";
exit;
}
}
?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum