The Little Guy Posted February 26, 2007 Share Posted February 26, 2007 Anyone know how/where I can learn to do a image upload from another website? So instead of using the browse button on my computer, I would like to use a url link to the image, then save it on my server. Anyone know how to do this? Link to comment https://forums.phpfreaks.com/topic/40241-file-upload-from-another-site/ Share on other sites More sharing options...
fert Posted February 26, 2007 Share Posted February 26, 2007 http://us3.php.net/manual/en/function.fopen.php http://us3.php.net/manual/en/function.fread.php http://us3.php.net/manual/en/function.fwrite.php http://us2.php.net/manual/en/function.copy.php Link to comment https://forums.phpfreaks.com/topic/40241-file-upload-from-another-site/#findComment-194694 Share on other sites More sharing options...
The Little Guy Posted February 27, 2007 Author Share Posted February 27, 2007 I don't know If this is close, but this is wat I came up with. I get no errors, but it doesn't save the image to my server. I also don't know how fwrite would fit into this, since the file must exist to write to it. $handle = fopen($url, "wb"); $contents = fread($handle, filesize($url)); if (!copy($contents, time().$url)) { echo "failed to copy $file...\n"; } fclose($handle); Link to comment https://forums.phpfreaks.com/topic/40241-file-upload-from-another-site/#findComment-195430 Share on other sites More sharing options...
fert Posted February 27, 2007 Share Posted February 27, 2007 you only use wb when you're writing to a file, use rb Link to comment https://forums.phpfreaks.com/topic/40241-file-upload-from-another-site/#findComment-195451 Share on other sites More sharing options...
Orio Posted February 27, 2007 Share Posted February 27, 2007 You could also do something like this: <?php //I took your avatar as an example: $url = "http://tzfiles.com/users/ryan/image.gif"; $extension = strrchr($url, "."); while (!file_exists($name = rand(10,99).time().$extension)); //Create a unique file-name $h = fopen($name, "w"); //Create the file fwrite($h, file_get_contents($url)); fclose($h); echo $name." Created!"; ?> Of course you should add some validation (that the url is ok and the file-type is ok etc') and some changes, but you get the idea. Orio. Link to comment https://forums.phpfreaks.com/topic/40241-file-upload-from-another-site/#findComment-195470 Share on other sites More sharing options...
The Little Guy Posted February 27, 2007 Author Share Posted February 27, 2007 awsome, I'll give it a try as soon as my host decides it want's to work. Link to comment https://forums.phpfreaks.com/topic/40241-file-upload-from-another-site/#findComment-195479 Share on other sites More sharing options...
The Little Guy Posted February 27, 2007 Author Share Posted February 27, 2007 That didn't work Is the $name variable ok? $url is defined from a form foreach($img as $url){ $extension = strrchr($url, "."); while (!file_exists($name = 'product/large/'.rand(10,99).time().$extension)); $h = fopen($name, "w"); //Create the file fwrite($h, file_get_contents($url)); fclose($h); } Link to comment https://forums.phpfreaks.com/topic/40241-file-upload-from-another-site/#findComment-195499 Share on other sites More sharing options...
The Little Guy Posted February 28, 2007 Author Share Posted February 28, 2007 any help? Link to comment https://forums.phpfreaks.com/topic/40241-file-upload-from-another-site/#findComment-195821 Share on other sites More sharing options...
Orio Posted February 28, 2007 Share Posted February 28, 2007 Yeah I have a mistake there- try removing the not (!) sign from the while. Orio. Link to comment https://forums.phpfreaks.com/topic/40241-file-upload-from-another-site/#findComment-196102 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.