I've tried using curl to download and save image from url, but this totally failed. The page shown picture like an image opened using notepad this is the code I've tried: $linkgambar = $result['gambar']; $name = get_the_title(); $ch = curl_init($linkgambar); $fp = fopen('/pict/', 'wb'); curl_setopt($ch, CURLOPT_FILE, $fp); curl_setopt($ch, CURLOPT_HEADER, 0); curl_exec($ch); curl_close($ch); fclose($fp); Code (markup): how I can save the image using $name without know the file extension? can someone give an example of function to download, rename and save the image to server with $name as the new image name? thanks
Is $result['gambar'] the location of the image file, or the page on which the image appears? If it's the page, you're saving text, not an image. You have to give it the file name of the file you want to download. (The page the image is on just has an <img /> tag, not an image.) $url = 'http://www.gambar.com/images/myfile.jpg'; $path = '/path/to/local/copy/of/myfile.jpg'; $fp = fopen($path, 'wb'); $ch = curl_init($url); curl_setopt($ch, CURLOPT_FILE, $fp); $data = curl_exec($ch); curl_close($ch); fclose($fp); PHP:
thanks rukbat! yes, $result['gambar'] is the location of image file. How I can save the image name with format automatically, the file name using $name? thanks
With the code I gave you. If you don't know the extension of the file you can read the beginning of it and figure out the file type, then rename it.
Something like this will upload an image and overwrite the existing file with the new ones. addin file-type extensions on your own, set up the submit HTML for the upload. simple /// leave the name newimage.jpg $newimage = "Your_Image_Folder/newimage.jpg"; $result = @move_uploaded_file($_FILES['image_file']['tmp_name'], $newimage); if(empty($result)) $error["result"] = "There was an error moving the uploaded file.";
I'm give up with this I have no time learn more for this. Anyone who can do this please PM me and tell your quotes. this is the threat: http://forums.digitalpoint.com/showthread.php?t=2287381
Please use this curl_get method I wrote for you, it sends useragent in header, better is put following in a file to include, example of usage is at bottom. A word-press plugin can be created easily. So far this is code for image fetching and saving on server. If you need WP plugin, let me know. function curl_get($page) { $ch = curl_init(); curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1" ); curl_setopt($ch, CURLOPT_URL,$page); curl_setopt($ch, CURLOPT_POST, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); #curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); curl_setopt($ch, CURLOPT_HEADER ,0); $data = curl_exec ($ch); curl_close ($ch); return $data; } function get_image_and_save($image_url, $save_to) { $data = curl_get($image_url); if (!$data || return (!$data || $data=='' ) ? FALSE : file_put_contents($save_to, $data); } PHP: #using it $res = get_image_and_save("http://www.babyannouncementwording.org/wp-content/uploads/baby2.jpg", "./upload_dir/myfile_.jpg"); if (!$res) echo "Some error occurred!"; PHP: I hope it helps.
Try this file_put_contents("baby2.jpg", file_get_contents("http://www.babyannouncementwording.org/wp-content/uploads/baby2.jpg")); Code (markup):