thanks taking time read this. have form 35 file input boxes part of cms customer upload 35 pictures of each products. breakdown of 7 pictures of black version, 7 pictures of blue, 7 pictures of grey, 7 pictures of red, , 7 pictures of white version of each product. that's 35 total pictures needs upload. additionally, each of files uploads, smaller "thumbnail" sized picture needs made. have file upload script use works beautifully - when there's 1 file upload. i'm not sure how apply in case 35 files. each input box has unique name (black1, black2...black7, blue1, blue2...blue7, etc) so, technically repeat upload code 35 times unique name of each file input box this, obvoiusly extremely inefficient. i'm hoping here can me out better solution.
an additional requirement names of files stored in database. of filenames of black pictures should put string, separated commas, , stored in blackpics column of database. of filenames of blue pictures should put string, separated commas, , stored in bluepics columns of database. , on grey, red, , white pictures.
here code have upload 1 file. gets file input box "file", checks it's of right extension (an image file), checks filesize, creates random file name random number , timestamp, creates thumbnail (448px x 298px - big thumbnail, know), checks original image uploaded of right dimensions (873px x 581px), , if okay, end big file saved in ../images/store/big/ , thumb saved in ../images/store/small/. both have same filename, they're stored in different directories. temporary files deleted , that, , if there errors, files deleted. said, works great 1 file.
so need modify code of input box "black1", "black2"..."black7", saves filenames string (black1.jpg,black2.jpg,black3.jpg,black4.jpg,black5.jpg,black6.jpg,black7.jpg) can store in 'blackpics' column of database. same blue, grey, red, , white. don't need database part. i'm thinking need create function containing file upload script returns filename. call function 35 times, 1 each of input boxes. wrong.
if offer me assistance, appreciate it. here code upload script:
<?php $filename = $_files["file"]["name"]; $file_basename = substr($filename, 0, strripos($filename, '.')); // file extention $file_ext = substr($filename, strripos($filename, '.')); // file name $filesize = $_files["file"]["size"]; $allowed_file_types = array('.jpg','.gif','.png', '.jpg'); if (in_array($file_ext,$allowed_file_types) && ($filesize < 1024000)) { // rename file $rand = rand(1,100000000); $time = time(); $newfilename = $rand . $time . $file_ext; if (file_exists("../images/store/big/" . $newfilename)) { // file exists error $err[] = "you have uploaded file."; } else { move_uploaded_file($_files["file"]["tmp_name"], "../images/store/big/" . $newfilename); $pathtoimage = '../images/store/big/' . $newfilename; $pathtothumb = '../images/store/small/' . $newfilename; $last4 = substr($pathtoimage, -4); switch(strtolower($last4)) { case '.jpeg': $img = imagecreatefromjpeg($pathtoimage); break; case '.jpg': $img = imagecreatefromjpeg($pathtoimage); break; case '.png': $img = imagecreatefrompng($pathtoimage); break; case '.gif': $img = imagecreatefromgif($pathtoimage); break; default: exit('unsupported type: '. $pathtoimage); } $max_width = 448; $max_height = 298; // current dimensions $old_width = imagesx($img); $old_height = imagesy($img); // calculate scaling need fit image inside our frame $scale = min($max_width/$old_width, $max_height/$old_height); // new dimensions $new_width = ceil($scale*$old_width); $new_height = ceil($scale*$old_height); $tmp_img = imagecreatetruecolor($new_width, $new_height); imagecopyresampled($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $old_width, $old_height); switch(strtolower($last4)) { case '.jpeg': imagejpeg($tmp_img, $pathtothumb); break; case '.jpg': imagejpeg($tmp_img, $pathtothumb); break; case '.png': imagepng($tmp_img, $pathtothumb); break; case '.gif': imagegif($tmp_img, $pathtothumb); break; default: exit('unsupported type: '. $pathtoimage); } imagedestroy($tmp_img); imagedestroy($img); } } elseif (empty($file_basename)) { $err[] = "select file upload"; } elseif ($filesize > 1024000) { $err[] = "file size limit exceeded"; } else { $err[] = "file type not allowed"; unlink($_files["file"]["tmp_name"]); } list($width, $height) = getimagesize("../images/store/big/$newfilename"); if ($width != "873" || $height != "581") { $err[] = "file dimensions error"; unlink("../images/store/big/$newfilename"); unlink("../images/store/small/$newfilename"); } ?> and in body have file upload fields so...
<input type="file" name="black1" disabled="1"> <input type="file" name="black2" disabled="1"> ... <input type="file" name="black7" disabled="1"> <input type="file" name="blue1" disabled="1"> <input type="file" name="blue2" disabled="1"> ... <input type="file" name="blue7" disabled="1"> and on grey, red, , white.
like said, if can me out, appreciate it. , if made way down here, again taking time read of this.
first don't use dimensions images. dimensions not size of image. , size matters displaying image on website, not dimensions.
second why not use multipart uploading form? see here. , client select images colourwise , upload them 1 selection, reduce clicks 35 seven. or if trust client more tech-savvy: use 1 input field , instruct him name files in specific way. "b_[name of file].[extension]" black image. use favourite string searching method - example regex - identify images classes.
Comments
Post a Comment