Hi,
I have a extension in my opencart store that uses a .csv file to import data into products. The .csv file is a stock inventory list from a tyre manufacturer. The extension uses a string value of the image filepath that get inserted into the database.
The issue I have is that the image name in the inventory doesn't include the file's extension. There are around 5000 products and I have all the images ready uploaded in a directory on my server.
I could go through and add them into the csv file manually but the stock inventory will be sent across weekly and this would become time consuming and costly for the client (and tedious for me).
As there are only 2 file extensions used (.GIF and .jpg) I have created an if statement that will search for the file in that directory with that extension then return it with that extension attached if it exists, using the is_file() function. A similair version of this has worked for me, although the below snippet of code has had variables like the $file changed to match the function I need to work with.
$file = $this->params['images_dir'].$image;
if (is_file(DIR_IMAGE . $file.'.GIF')) {
$file = $file.'.GIF';
echo "The file $file exists in gif format <br />";
} elseif (is_file(DIR_IMAGE . $file.'.jpg')) {
$file = $file.'.jpg';
echo "The file $file exists in jpg format<br />";
} else {
$this->lastError = "File not found " . DIR_IMAGE . $file ." extension <br />";
return false;
}
Speaking to the developer of the extension they said that the check has to be added to the getImageFile() function within one file of the code. This is where I get stuck.
Here is the original function in question:
protected function getImageFile($image) {
$this->lastError = '';
if (empty($image))
return false;
$image = trim($image);
$file = '';
if ($this->isUrl($image)) {
// download file and parse the path
//
$image = htmlspecialchars_decode($image);
$tmp = str_replace(array(' '), array('%20'), $image);
$content = $this->getFileContentsByUrl($tmp);
if (empty($content)) {
$this->lastError = "File content is empty for $tmp";
return false;
}
$url_info = @parse_url($image);
if (empty($url_info)) {
$this->lastError = "Invalid URL data $url";
return false;
}
//get relative image directory to $images_dir
$fullname = '';
$images_dir = str_replace("\\", '/', $this->params['incoming_images_dir']);
if (!empty($url_info['path'])) {
$url_info['path'] = urldecode($url_info['path']);
$path_info = pathinfo($url_info['path']);
$path_info['dirname'] = $this->strip($path_info['dirname'], array("\\","/"));
if (!empty($path_info['dirname'])) {
$images_dir = $images_dir . $path_info['dirname'] . '/';
if (!file_exists(DIR_IMAGE . $images_dir)) {
if (!mkdir(DIR_IMAGE . $images_dir, 0755, true)) {
$this->lastError = "Script cannot create directory: $images_dir";
return false;
}
}
}
}
// get file name to $filename
$tmp_file = tempnam(DIR_IMAGE . $images_dir, "tmp");
$size = file_put_contents($tmp_file, $content);
if (empty($size)) {
$this->lastError = "Cannot save new image file: $tmp_file";
return false;
}
$image_info = getimagesize($tmp_file);
if (empty($image_info)) {
$this->lastError = "getimagesize returned empty info for the file: $image";
return false;
}
if (!empty($url_info['query'])) {
$filename = '';
if (!empty($path_info['basename'])) {
$filename = $path_info['basename'];
}
$query = $this->normalizeFilename($url_info['query']);
$filename = $filename . $query . image_type_to_extension($image_info[2]);
} else {
$filename = $path_info['basename'];
if (empty($path_info['extension'])) {
$filename = $filename . image_type_to_extension($image_info[2]);
}
}
if (is_file(DIR_IMAGE.$images_dir.$filename)) {
@unlink(DIR_IMAGE.$images_dir.$filename);
}
if (!is_file(DIR_IMAGE.$images_dir.$filename)) {
if (!rename($tmp_file, DIR_IMAGE.$images_dir.$filename)) {
$this->lastError = "rename operation failed. from $tmp_file to " . DIR_IMAGE.$images_dir.$filename;
return false;
}
if (!chmod(DIR_IMAGE . $images_dir . $filename, 0644)) {
$this->lastError = "chmod failed for file: $filename";
return false;
}
}
$file = $images_dir . $filename;
} else {
//
// if the image is a regular file
//
$file = $this->params['images_dir'].$image;
if (!is_file(DIR_IMAGE . $file)) {
$this->lastError = "File not found " . DIR_IMAGE . $file;
return false;
}
}
return $file;
}
Now looking through it I believe my check has to be added in the last else statement (to check if the image is a regular file) but im struggling with this.
Here is what I have come up with so far (for the else statement) but it doesnt work. The extensions isnt added onto the $file variable and so causes a file not found error when running the uploader.
} else {
//
// if the image is a regular file
//
$file = $this->params['images_dir'].$image;
/* reece chnages start */
if (is_file(DIR_IMAGE . $file.'.GIF')) {
$file = $file.'.GIF';
echo "The file $file exists in gif format <br />";
} elseif (is_file(DIR_IMAGE . $file.'.jpg')) {
$file = $file.'.jpg';
echo "The file $file exists in jpg format<br />";
} else {
$this->lastError = "File not found " . DIR_IMAGE . $file ." extension <br />";
return false;
}
/* reece chnages end */
//if (!is_file(DIR_IMAGE . $file)) {
// $this->lastError = "File not found " . DIR_IMAGE . $file;
// return false;
//}
}
return $file;
For all I know I may be placing the check in the wrong place, I am really not sure.
Could anyone please point me in the right direction? I know a little bit about PHP but am no means an expert so please bare with me if I ask questions that might seem simple.
I can send you the whole php file if you want to look at it.
All help is greatly appreciated, thank you.