Sorry. Right track to getting the result I described above, getting each image name in 1 comma delimited string. I have tried to implode the result of each loop but using this $db_names[] already put them in an array.
I always get 500 error.
Here is the code of the page that works before any modification. I can get the image names by accessing $db_names[0], $db_names[1], $db_names[2] etc.
I want to get all image names in 1 comma delimited string like $db_names[0],$db_names[1],$db_names[2],etc,etc to insert in 1 field instead of each possible image getting its own field in the row in the database.
The number of uploaded images may be different each time.
This may help.
foreach ($_FILES["userfile"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
// Grabs everything before and after the last dot and turn it into the new filename.
$imgfile = $_FILES['userfile']['tmp_name'][$key];
$imgfile_name = $_FILES['userfile']['name'][$key];
$imgfile_size = $_FILES['userfile']['size'][$key];
$imgfile_type = $_FILES['userfile']['type'][$key];
$dir="/html/inventory/cars/";
srand((double)microtime()*1000000);
$unique_str = md5(rand(0,9999999));
$p = strrpos($imgfile_name, "." );
$old_name = substr($imgfile_name , 0 , $p ) . $unique_str . substr($imgfile_name , $p , ( strlen($imgfile_name ) - $p ) );
/*== upload directory where the file will be stored
relative to where script is run ==*/
$uploaddir = "/html/inventory/cars";
/*== get file extension (fn at bottom of script) ==*/
$pext = getFileExtension($imgfile_name);
$pext = strtolower($pext);
//-- RE-SIZING UPLOADED IMAGE
$src = imagecreatefromjpeg($imgfile);
// Capture the original size of the uploaded image
list($width,$height)=getimagesize($imgfile);
$newwidth=640;
$diff = $width / $newwidth;
$newheight= $height / $diff;
$tmp=imagecreatetruecolor($newwidth,$newheight);
// this line actually does the image resizing, copying from the original
// image into the $tmp image
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
/*== setup final file location and name ==*/
/*== change spaces to underscores in filename ==*/
$new_name = str_replace(" ","_",$old_name);
$newfile = $dir . "/$new_name";
/*==make array of image names for database ==*/
$db_names[] = $new_name;
/*== do extra security check to prevent malicious abuse==*/
/*== move file to proper directory ==*/
if (!imagejpeg($tmp,$newfile,100))
{
/*== if an error occurs the file could not
be written, read or possibly does not exist ==*/
print "Error Uploading File.";
exit();
}
/*== delete the temporary uploaded file ==*/
unlink($imgfile);
imagedestroy($tmp);
imagedestroy($src);
}
}
/*== FUNCTIONS ==*/
function getFileExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}