-2

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
Oktarin
  • 57
  • 2
  • 11
  • 2
    How about you download and open one of those “images” with a text editor, and check what the actually contain. – 04FS Jan 20 '20 at 08:30
  • @04FS Thank you, great idea, I have edited the question with content :) – Oktarin Jan 20 '20 at 16:50

1 Answers1

1

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);
user1039663
  • 1,230
  • 1
  • 9
  • 15
  • Yes, about 10minutes after my last edit I figured out that I am missing `file_get_contents`- after I have implemented it works great :). – Oktarin Jan 20 '20 at 21:53