Reputation: 311
Im about to start on a PHP script to import csv database.
The csv has a column with urls to product images.
What I need to be able to do is get the image, check what type of file it is (jpg, png etc), change the name, save the file to a folder on the server and then insert the file name into the database.
The inserting into the database bit I can do, its the renaming of the file im confused with.
Is it possible to grab the info like you would when uploading a file, for example:
Uploading a file using a file input in a html form
$_FILES['file']['name'];
or
$_FILES['file']['type'];
If downloading a file, could this be possible
$downloaded_image['name'];
or
$downloaded_image['type'];
or is that totally off the mark?
I have never done this before, and most of the answers on stackoverflow don't quite answer my question so was hoping someone could point me in the right direction on how to do this.
EDITED/UPDATED:
Would something like this work to get the file attributes...
$image_id = '123456';
$the_image = file_get_contents($downloaded_image);
$image_name = $the_image['name'];
$image_type = $the_image['type'];
$new_name = $image_id . '.' . $image_type;
$img_path = '/images/';
$save_image = file_put_contents($img_path, $new_name);
if($save_image) {
echo 'image saved';
} else {
echo 'Not Saved';
}
Hopefully im making some sense.
UPDATE: here is the script as it is (still needs tidying up)
define('CSV_PATH','E:/xampp/htdocs/aff/csv-import/');
// path where your CSV file is located
$csv_file = CSV_PATH . "infotuts.csv"; // Name of your CSV file
$csvfile = fopen($csv_file, 'r');
$theData = fgets($csvfile);
$i = 0;
while (!feof($csvfile)) {
$csv_data[] = fgets($csvfile, 1024);
$csv_array = explode(",", $csv_data[$i]);
$insert_csv = array();
$insert_csv['test_id'] = $csv_array[0];
// $insert_csv['test_aw_id'] = $csv_array[1];
// $insert_csv['test_name'] = $csv_array[2];
$image_id = $csv_array[1];
$download_image = $csv_array[2];
// Store the original filename
$original_name = basename($download_image);
// Original extension by string manipulation
$original_extension = substr($original_name, strrpos($original_name, '.')); // ".jpg"
// An array to match mime types from finfo_file() with extensions
// Use of finfo_file() is recommended if you can't trust the input
// filename's extension
$types = array('image/jpeg' => '.jpg','image/png' => '.png','image/gif' => '.gif');
// Get the file and save it
$img = file_get_contents($download_image);
$stored_name = 'images/' . $image_id . $original_extension;
if ($img) {
file_put_contents($stored_name);
// Get the filesize if needed
$size = filesize($stored_name);
// If you don't care about validating the mime type, skip all of this...
// Check the file information
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mimetype = finfo_file($finfo, $stored_name);
// Lookup the type in your array to get the extension
if (isset($types[$mimetype])) {
// if the reported type doesn't match the original extension, rename the file
if ($types[$mimetype] != $original_extension) {
rename($stored_name, 'images/' . $image_id . $types[$mimetype]);
}
}
else {
// unknown type, handle accordingly...
}
finfo_close($finfo);
$query = "INSERT INTO test(test_id, test_aw_id, test_name) VALUES ('', '$image_id', '$stored_name')";
$n=mysqli_query($con, $query);
$i++;
}
else {
echo 'Could not get file';
}
}
fclose($csvfile);
Upvotes: 1
Views: 6708
Reputation: 311
<?php
include_once('includes/functions.php');
define('CSV_PATH','E:/xampp/htdocs/aff/csv-import/');
$csv_file = CSV_PATH . "infotuts.csv";
$csvfile = fopen($csv_file, 'r');
$theData = fgets($csvfile);
$i = 0;
while (!feof($csvfile)) {
$csv_data[] = fgets($csvfile, 1024);
$csv_array = explode(",", $csv_data[$i]);
$insert_csv = array();
$insert_csv['test_id'] = $csv_array[0];
$insert_csv['test_aw_id'] = $csv_array[1];
$insert_csv['test_name'] = $csv_array[2];
$image_id = $insert_csv['test_aw_id'];
$download_image = $insert_csv['test_name'];
$original_name = basename($download_image);
$original_extension = substr($original_name, strrpos($original_name, '.')); // ".jpg"
$types = array('image/jpeg' => '.jpg','image/png' => '.png','image/gif' => '.gif');
$img = file_get_contents($download_image);
$stored_name = $image_id . $original_extension;
$stored_name = trim($stored_name);
if ($img) {
file_put_contents($stored_name, $img);
//$size = filesize($stored_name);
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mimetype = finfo_file($finfo, $stored_name);
if (isset($types[$mimetype])) {
if ($types[$mimetype] != $original_extension) {
rename($stored_name, 'E:/xampp/htdocs/aff/images/products/' . $stored_name);
}
}
else {
}
finfo_close($finfo);
$query = "INSERT INTO test(test_id, test_aw_id, test_name) VALUES ('', '$image_id', '$stored_name')";
$n=mysqli_query($con, $query);
$i++;
}
else {
echo 'Could not get file';
}
}
fclose($csvfile);
echo "File data successfully imported to database!!";
mysqli_close($con);
?>
Upvotes: 0
Reputation: 270617
By retrieving the file with file_get_contents()
, you won't get any particularly useful information about its format. It carries no metadata similar to that found in $_FILES
for upload.
If the image URLs are expected to be a full filename with an extension, and you trust the extension to be correct, you may use that as your type. However, finfo_file()
with the FILEINFO_MIME_TYPE
option will probe the file to return its mime type, as in image/jpeg
or image/png
.
So your workflow will be to:
file_get_contents()
finfo_file()
to retrieve its mime typeExample:
// Assume this URL for $download_image from your CSV
$download_image = 'http://example.com/images/img1.jpg';
$image_id = 12345;
// Store the original filename
$original_name = basename($download_image); // "img1.jpg"
// Original extension by string manipulation
$original_extension = substr($original_name, strrpos($original_name, '.')); // ".jpg"
// An array to match mime types from finfo_file() with extensions
// Use of finfo_file() is recommended if you can't trust the input
// filename's extension
$types = array(
'image/jpeg' => '.jpg',
'image/png' => '.png',
'image/gif' => '.gif'
// Other types as needed...
);
// Get the file and save it
$img = file_get_contents($download_image);
$stored_name = 'images/' . $image_id . $original_extension;
if ($img) {
file_put_contents($stored_name, $img);
// Get the filesize if needed
$size = filesize($stored_name);
// If you don't care about validating the mime type, skip all of this...
// Check the file information
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mimetype = finfo_file($finfo, $stored_name);
// Lookup the type in your array to get the extension
if (isset($types[$mimetype])) {
// if the reported type doesn't match the original extension, rename the file
if ($types[$mimetype] != $original_extension) {
rename($stored_name, 'images/' . $image_id . $types[$mimetype]);
}
}
else {
// unknown type, handle accordingly...
}
finfo_close($finfo);
// Now save all the extra info you retrieved into your database however you normally would
// $mimetype, $original_name, $original_extension, $filesize
}
else {
// Error, couldn't get file
}
If you want to get the mimetype string from the extension you already have, and aren't validating the type with finfo, you can flip the $types
to swap keys with values.
if (in_array($original_extension), $types) {
$mimetype = array_flip($types)[$original_extension];
}
Upvotes: 4