phpimagesave-image

All images are saved with 40bytes and php file_put_contents - images are not valid


I am using this php code for grabbing images from url and saving it to my server. File is created and permissions are 755. The process is renaming the file with product name. When I try to access files i get this message in browser: The image cannot be displayed because it contains errors, and all files are saved with 40bytes of size. As far as I can see input data is correct, and I suspect that something with file_put_contents is wrong...

// converting strings that have HR diacritics 
function replaceHRznakove($string){
    $hr_slova=array("Č","Ć","Ž","Š","Đ","č","ć","ž","š","đ", "\\", "/","''",'"',"'","`",',',';',';','&','¸','!','#','$','%','=','<','>','?','*','@','§',"(",")","[","]","{","}");
    $asci_slova=array("C","C","Z","S","D","c","c","z","s","d","-", "-","",'',"","",'','','','','','','','','','','','','','','','','','','','','','');
    $converted_string=str_replace($hr_slova, $asci_slova, $string);
    return $converted_string;
}// end of function replaceHRznakove

// creating file with the product name
function createImage($product_name, $image_url, $image_location,$prod_code,$path_to_shop){
  $img_url = str_replace("https://","http://",$image_url);
  // checking if url returns 200 (exists) 
  if(urlExists($img_url)){
    // change blanks with dash
    $name_frags= explode(" ", $product_name);
    $filename= strtolower(implode("-", $name_frags));
    // calling function to replace strings
    $filename=replaceHRznakove($filename);
    // find file type (extension)
    $format_datoteke=pathinfo($img_url, PATHINFO_EXTENSION);
    $filename=$prod_code."-".$filename.".".$format_datoteke;
    // save file with new name and extension
    // checking if file already exists on server, if not we are creating new
    $file_location=$_SERVER['DOCUMENT_ROOT'].$path_to_shop.$image_location.$filename;
    if(file_exists($file_location)){
      $finfo = finfo_open(FILEINFO_MIME_TYPE);
    }
    else{
      file_put_contents($file_location, $img_url);
      $finfo = finfo_open(FILEINFO_MIME_TYPE);
    }
    $mime_type=finfo_file($finfo, $file_location);
    finfo_close($finfo);    
  }
  else{
    $filename="";
    $mime_type="";
  }
  return array($filename, $mime_type);
} // kraj funkcije createImate


// funkcija koja provjerava da li postoji datoteka iz URL-a
function urlExists($url){
   $headers=get_headers($url);
   return stripos($headers[0],"200 OK")?true:false;
} // kraj funkcije urlExists

Any help is appreciated.


EDIT

As suggested in comment I have looked at what is actually stored in the file and it seems that instead of image data URL of the image is stored in the file. So when file image.jpg is opened in text editor I see this content:

http://www.somedomain.com/image.jpg

Solution

  • That's because you are saving the image url as contents into the file:

    file_put_contents($file_location, $img_url);
    

    Instead you should call to:

    $image_data = file_get_contents($img_url);
    ...
    file_put_contents($file_location, $image_data);