1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
//Remove Image Url $imageUrl = "http://www.removeimageurl.com/some/remote/image.jpg"; //Get File Extension $extension = substr(strrchr($imageUrl,'.'),1); //This is the name you will give the image $name = $wholedata["name"]; //Remove all the junk from the $name $match = array("/\s+/","/[^a-zA-Z0-9\-]/","/-+/","/^-+/","/-+$/"); $replace = array("-","","-","",""); $newName = preg_replace($match,$replace, $name); $newFilename = strtolower($newName.'.'.$extension); //New File name with new Extension $extension = substr(strrchr($filename,'.'),1); $new_file_name = strtolower('main_'.$newName.'.'.$extension); //Put the remove file in current directory of server. file_put_contents($new_file_name, file_get_contents($imageUrl)); |
———————————————————————-
If you wanted to use directories on server you could (remove the last line above and) do something like
1 2 3 4 5 6 7 8 9 10 |
// Set server Path correctly $saveDir = $_ENV["DOCUMENT_ROOT"]."/images/"; //Assign Write Privileges to Directory chmod($saveDir,0777); //If chmod worked, and it is writable then write else fail if(is_writable($saveDir)) { file_put_contents($saveDir . $new_file_nam, file_get_contents($imageUrl)); } else { exit("Failed to write to directory ".$saveDir); } |
This WordPress code tag BS is pissing me off and I will be finding a plugin to allow all this to be copied and pasted easily 🙂