how does a csv file contain an image?
if it contain a path to an image, make sure this is a full file path, or a full url
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I have a function to convert images to wepb format which is working as expected when the image is submitted by form. However, now I need to get the image from a url using file_get_contents(), but the function that converts the image doesn't recognize it as an image file.
This is my function:
public function importar()
{
function csv_to_array($file_name) {
$data = $header = array();
$i = 0;
$file = fopen($file_name, 'r');
while (($line = fgetcsv($file)) !== FALSE) {
if( $i==0 ) {
$header = $line;
} else {
$data[] = $line;
}
$i++;
}
fclose($file);
foreach ($data as $key => $_value) {
$new_item = array();
foreach ($_value as $key => $value) {
$new_item[ $header[$key] ] =$value;
}
$_data[] = $new_item;
}
return $_data;
}
$csvFile = 'pets.csv';
$csv = csv_to_array($csvFile);
$ignoreError = stream_context_create(array(
'http' => array('ignore_errors' => true),
));
foreach ($csv as $key => $pet){
$this->webpConvert2(file_get_contents($pet['imagem']), false, $ignoreError);
$novo = new Pet;
$novo->nome_bichinho = $pet['titulo'];
$novo->sexo = $pet['sexo'];
$novo->save();
}
}
When it is submitted by form I do it like this and it works as expected: $this->webpConvert2($request->file('image'));
But using file_get_contents() I get false as the function that converts it doesn't understand how an image file.
How can I resolve this?
Sure it will contain a bunch of "encrypted code". It is an image, which is binary encoded, stored on a string, so if you echo it, it will show as a bunch of "encrypted code".
Also the returned string is stored in memory, so is_file(), or file_exists() will both return false, if you didn't store the file locally.
What did you expected it to occur? Were you expecting file_get_contents() to download the image, save it as a file, and return the saved file path?
If you want to save the image, try saving the contents (the response string) on a path you provide. You can use file_put_contents().
<?php
$ignoreError = stream_context_create(array(
'http' => array('ignore_errors' => true),
));
foreach ($csv as $key => $pet) {
// @ is to suppress any posible errors
// file_get_contents will store the "contents"
// into memory
$response = @file_get_contents($pet['imagem'], false, $ignoreError);
if ($response === false) {
// image download failed
// check $http_response_header
// and log or do something when
// the download fails
} else {
$filename = basename($pet['imagem']);
$filepath = '/path/to/a/writeable/directory/' . $filename;
// If you are using Laravel, you can use this:
// $filepath = storage_path('app/' . $filename);
// $response is a string here
// file_put_contents will save
// the "contents" to a file
file_put_contents($filepath, $response);
$this->webpConvert2($filepath);
}
$novo = new Pet();
$novo->nome_bichinho = $pet['titulo'];
$novo->sexo = $pet['sexo'];
$novo->save();
}
Please or to participate in this conversation.