php - second image in array can be saved on localhost but first one always get 404 not found
i have an odd problem that i realy cant find out where is that problem..
when i use curl or file get contents or copy function to save an image from some address first image cant be saved on my server but the second one can.
for example when i use this 2 image
http://94.182.168.34/Digikala/Image/HtmlEditor/Review/2014/7/17/g2.jpg
http://94.182.168.34/Digikala/Image/HtmlEditor/Review/2014/7/17/g4.jpg
g4.jpg can be saved on my localhost but not g2 one...when i change it to
http://94.182.168.34/Digikala/Image/HtmlEditor/Review/2014/7/17/g4.jpg
http://94.182.168.34/Digikala/Image/HtmlEditor/Review/2014/7/17/g2.jpg
i can save g2 but i cant save g4...so i think there must be a problem with first one here...but when i put many images like
http://94.182.168.34/Digikala/Image/HtmlEditor/Review/2014/7/17/g1.jpg
http://94.182.168.34/Digikala/Image/HtmlEditor/Review/2014/7/17/g2.jpg
http://94.182.168.34/Digikala/Image/HtmlEditor/Review/2014/7/17/g3.jpg
http://94.182.168.34/Digikala/Image/HtmlEditor/Review/2014/7/17/g4.jpg
some can be saved and some of these images cant
here is my code in php
function putImages($images,$id_product,$name){
$conn = conn();
$images = explode("\n",$images);
$i = 1;
$name = preg_replace('/[^a-zA-Z0-9]+/', '_', $name);
foreach($images as $image){
if(!get_headers($image, 1)){
continue;
}
$header = get_headers($image, 1);
if($header == 'HTTP/1.1 404 Not Found'){
continue;
}
//$id_image is 24 for example
$numbers = str_split($id_image,1);
$add = 'img/p/';
foreach($numbers as $number){
$add .= $number . '/';
//this will make img/p/2/4/ address for example
}
if (!file_exists($add)) {
mkdir($add, 0755, true);
}
try {
//i put this on try block if i can find out an error beside 404 not found
if($ch = curl_init($image)){
// i actualy tryed to skip if no image found and did it without if staement too
$new_address = $add . $id_image . '.jpg';
copy('file/index.php', $add . 'index.php');
$fp = fopen($new_address, 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
}else{
continue;
}
$img = new resize($new_address);
$img -> resizeImage(80, 80,'auto');$img -> saveImage($add . $id_image . '-cart_default' . '.jpg');
$img -> resizeImage(250, 250,'auto');$img -> saveImage($add . $id_image . '-home_default' . '.jpg');
$img -> resizeImage(458, 458,'auto');$img -> saveImage($add . $id_image . '-large_default' . '.jpg');
$img -> resizeImage(125, 125,'auto');$img -> saveImage($add . $id_image . '-medium_default' . '.jpg');
$img -> resizeImage(98, 98,'auto');$img -> saveImage($add . $id_image . '-small_default' . '.jpg');
$img -> resizeImage(800, 800,'auto');$img -> saveImage($add . $id_image . '-thickbox_default' . '.jpg');
} catch(Exception $e) {
echo 'Error: ' . $e->getMessage();
}
$i++;
}
}
i tryed to check if my server cant find that image so skip foreach loop but no chance on that too!!
Answer
Solution:
in this case of problem when i used textarea and typed links in textarea and explode links by "\n" to get links..those links have some whitespaces and you should trim those links. so i just used
and problem solved..do not remember to trim and replace \r because IE add \r\n and other browser add \n to each line breaks