I have a for loop at that takes up to 5 uploaded images from a form and resizes them, adds watermark and uploads them to directory. Works perfect. I then want to find a way to get the indivdual names of each one of the filenames and do some other things with the filenames. I cant seem to find a way to get those names in some sort of array. The script creates the names because the uploaded names are correct.
The filenames are a variable called $new_name. You will see it towards the bottom of the script. I want to get all of the "$new_name" filenames from this script
Here is the script.
foreach ($_FILES["userfile"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$imgfile = $_FILES['userfile']['tmp_name'][$key];
$imgfile_name = $_FILES['userfile']['name'][$key];
$imgfile_size = $_FILES['userfile']['size'][$key];
$imgfile_type = $_FILES['userfile']['type'][$key];
// Grabs everything before and after the last dot and turn it into the new filename.
$dir="/home/u2/seaera/html/upload_pics/";
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 = "/upload_pics";
/*== get file extension (fn at bottom of script) ==*/
$pext = getFileExtension($imgfile_name);
$pext = strtolower($pext);
//-- RE-SIZING UPLOADED IMAGE
/*== only resize if the image is larger than 50 x 50 ==*/
$imgsize = GetImageSize($imgfile);
/*== check size 0=width, 1=height ==*/
if (($imgsize[0] > 50) || ($imgsize[1] > 50))
{
/*== temp image file -- use "tempnam()" to generate the temp
file name. This is done so if multiple people access the
script at once they won't ruin each other's temp file ==*/
$tmpimg = tempnam("/tmp", "MKUP");
/*== RESIZE PROCESS
1. decompress jpeg image to pnm file (a raw image type)
2. scale pnm image
3. compress pnm file to jpeg image
==*/
/*== Step 1: djpeg decompresses jpeg to pnm ==*/
system("djpeg $imgfile >$tmpimg");
/*== Steps 2&3: scale image using pnmscale and then
pipe into cjpeg to output jpeg file ==*/
system("pnmscale -xy 400 400 $tmpimg | cjpeg -smoo 10 -qual 75 >$imgfile");
/*== remove temp image ==*/
unlink($tmpimg);
}
/*== setup final file location and name ==*/
/*== change spaces to underscores in filename ==*/
$new_name = str_replace(" ","_",$old_name);
$newfile = $dir . "/$new_name";
/*== do extra security check to prevent malicious abuse==*/
if (is_uploaded_file($imgfile))
{
/*== move file to proper directory ==*/
if (!copy($imgfile,"$newfile"))
{
/*== 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);
/*== Writes watermark on image ==*/
include_once("imagefunctions.php");
$size = getimagesize("/home/u2/seaera/html/upload_pics/$new_name");
$source = imagecreatefromjpeg("/home/u2/seaera/html/upload_pics/$new_name");
$photo_height = $size[1];
$photo_width = $size[0];
$image = imagecreatetruecolor($photo_width, $photo_height);
$copy = imagecopy($image, $source, 0,0,0,0,$photo_width,$photo_height);
imagedestroy($source);
$white = imagecolorclosest($image, 255,255,255);
$blue = imagecolorclosest($image,96,129,247);
imagettftextoutline($image, 12, 0, 3, 15, $white,$blue,"/home/u2/seaera/html/admin/font.ttf", "www.seaeracharters.com",1);
imagejpeg($image,$dir."$new_name",100);
imagedestroy($image);
}
}
/*== FUNCTIONS ==*/
function getFileExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
Thanks