piznac Posted August 26, 2006 Share Posted August 26, 2006 It seems like Ive done this before,.. But I cant quite remember:I have an upload script with ten upload fields,.. I have a script to upload the first file, thumbnail it, & put both paths in a database. Now I need to do the same thing to each upload field. I could type the code out manual,.. but that would such a pain and not very efficient. here is the code I have:[code]//validate file 1function filecheck($filenamea){ $ext = strrchr($filenamea,'.'); $FileType = array (".jpg", ".jpeg", ".JPG", ".JPEG"); if ( in_array ($ext, $FileType) ) return TRUE; else return FALSE;}if (filecheck($filenamea) == FALSE ) die ('Image 1 is not of an accepted type'); //if the temp file1 is there copy it to the server if (@is_uploaded_file($_FILES["file"]["tmp_name"])) { copy($_FILES["file"]["tmp_name"], "/home/rack/public_html/project/jobs/$dir2/return_images/" . $_FILES["file"]["name"]); echo "<br>"; } //get image size and name$im_file_name = "/home/rack/public_html/project/jobs/$dir2/return_images/" . $filenamea;$image_attribs = getimagesize($im_file_name); $im_old = imageCreateFromJpeg($im_file_name); //declare thumb$th_max_width = 100; $th_max_height = 100; $ratio = ($width > $height) ? $th_max_width/$image_attribs[0] : $th_max_height/$image_attribs[1]; $th_width = $image_attribs[0] * $ratio; $th_height = $image_attribs[1] * $ratio; $im_new = imagecreatetruecolor($th_width,$th_height); imageAntiAlias($im_new,true); //resize image$th_file_name = "/home/rack/public_html/project/jobs/$dir2/return_images/thumbs/" . $filenamea;imageCopyResampled($im_new,$im_old,0,0,0,0,$th_width,$th_height, $image_attribs[0], $image_attribs[1]); imageJpeg($im_new,$th_file_name,100);$mysql = "my database";mysql_select_db($mysql);$sql = "INSERT INTO `returnedfiles` (`imagepath`, `thumbpath`, `jobnum`, `pname`, `num`, `autojobnum`) VALUES ('$fullpath', '$thumbnailpath', '$job', '$direc', NULL, '1');";mysql_query($sql) or die('Error, insert query failed');[/code]Im thinking I ned to set the upload fields as an array,. and then do some kinda foreach loop. But Im pretty lost. If this not enough of the code please tell me. And I do some wierd things in my code, so dont blast me to much. Any help would be great,.. thanksBTW,.. about 3 weeks back I posted a question about uploading files via a Windows based smart phone. It turned out the problem was a security setting on the server. So if you have any problems with that,.. look there first....Thanks Quote Link to comment Share on other sites More sharing options...
piznac Posted August 27, 2006 Author Share Posted August 27, 2006 bump Quote Link to comment Share on other sites More sharing options...
drkstr Posted August 27, 2006 Share Posted August 27, 2006 [quote]Im thinking I ned to set the upload fields as an array,. and then do some kinda foreach loop.[/quote]That's how you would do it. what part were you neading help with? Quote Link to comment Share on other sites More sharing options...
piznac Posted August 27, 2006 Author Share Posted August 27, 2006 well the order I suppose,.. I have started on the script,..but depending on how I arrange it,.. I can upload 1 file,.. then 2.. its wierd,.. this is what I have so far:[code]foreach($_POST['textfield'] as $value) { $check_msg .= "Checked: $value\n"; }echo $check_msg;foreach($_FILES['file']['name'] as $value2) {$filename .= "Image Name: $value2\n";}echo $filename;function filecheck($value2){ $ext = strrchr($value2,'.'); $FileType = array (".jpg", ".jpeg", ".JPG", ".JPEG"); if ( in_array ($ext, $FileType) ) return TRUE; else return FALSE;}foreach($_FILES["file"]["tmp_name"] as $value){//validate file 1if (filecheck($value2) == FALSE ) die ('Image 1 is not of an accepted type'); //if the temp file1 is there copy it to the server if (@is_uploaded_file($value)) { copy($value, "/home/rack/public_html/project/jobs/test_job/" .$value2); echo "<br>success"; }else {echo "<br>Not Gonna Happen<br>";//get image size and name$im_file_name = "/home/rack/public_html/project/jobs/test_job/" . $value2;$image_attribs = getimagesize($im_file_name); $im_old = imageCreateFromJpeg($im_file_name); //declare thumb$th_max_width = 100; $th_max_height = 100; $ratio = ($width > $height) ? $th_max_width/$image_attribs[0] : $th_max_height/$image_attribs[1]; $th_width = $image_attribs[0] * $ratio; $th_height = $image_attribs[1] * $ratio; $im_new = imagecreatetruecolor($th_width,$th_height); imageAntiAlias($im_new,true); //resize image$th_file_name = "/home/rack/public_html/project/jobs/test_job/thumb/" . $value2;imageCopyResampled($im_new,$im_old,0,0,0,0,$th_width,$th_height, $image_attribs[0], $image_attribs[1]); imageJpeg($im_new,$th_file_name,100);} }foreach ($_FILES['file']['name'] as $value3) {$pathname .= "<br>$path$value3<br>";}echo $pathname;[/code]with this being my form:[code]<form action="fortest2.php" method="post" enctype="multipart/form-data" name="form1"> <table width="247" border="0" cellspacing="0" cellpadding="0"> <tr> <th width="247" scope="col"><input name="file[]" type="file" id="file[]"></th> </tr> <tr> <td><input name="file[]" type="file" id="file[]"></td> </tr> <tr> <td><input name="file[]" type="file" id="file[]"></td> </tr> <tr> <td><input name="textfield[]" type="text" id="textfield[]"></td> </tr> <tr> <td><input name="textfield[]" type="text" id="textfield[]"></td> </tr> <tr> <td><div align="center"> <input type="submit" name="Submit" value="Submit"> </div></td> </tr> </table></form>[/code]this is just a test script for me learn,.. but what its doing now is skipping all the file uploads except the last. But reporting them all as a success. And since the first to are not uploaded its giving m an error on the thumbnails,.. Quote Link to comment Share on other sites More sharing options...
Barand Posted August 27, 2006 Share Posted August 27, 2006 [code]<?phpforeach ($_FILES['file']['name'] as $k => $filename) { if ($name != '') { $tmpfile = $_FILES['file']['tmp_name][$k]; // process temp file }}[/code] Quote Link to comment Share on other sites More sharing options...
piznac Posted August 28, 2006 Author Share Posted August 28, 2006 Not sure I understand this Barand. Could you explain?..please Quote Link to comment Share on other sites More sharing options...
piznac Posted August 28, 2006 Author Share Posted August 28, 2006 Ok I tried this,.. but Im still only able to upload the last picture,.. but strangly now Im not getting any error form the thumbnail process. So does that mean the other two files were there at on point. God Im confused :-\ Quote Link to comment Share on other sites More sharing options...
piznac Posted August 28, 2006 Author Share Posted August 28, 2006 Ok I think I got it,.. but now I have 10 upload fields. If only two files are uploaded then it gives me errors on the other 7 and so on. How would I now check to see if each field has a value. I would think by using isset,.. but where would I put it so it loops through all files and stops when there is no value.[code]//get array of filenamesforeach($_FILES['file']['name'] as $value2) {$filename .= "Image Name: $value2\n";}echo $filename;echo $value2;//validate file 1function filecheck($value2){ $ext = strrchr($value2,'.'); $FileType = array (".jpg", ".jpeg", ".JPG", ".JPEG"); if ( in_array ($ext, $FileType) ) return TRUE; else return FALSE;}foreach ($_FILES['file']['name'] as $k => $filename) { if ($filename != '') { $tmpfile = $_FILES['file']['tmp_name'][$k]; }else{ echo "error"; } //validate file 1 //if the temp file1 is there copy it to the server if (@is_uploaded_file($tmpfile)) { copy($tmpfile, "/home/rack/public_html/project/jobs/$dir2/return_images/" .$filename); echo "<br>success"; }else {echo "<br>Not Gonna Happen<br>";}//get image size and name$im_file_name = "/home/rack/public_html/project/jobs/$dir2/return_images/" . $filename;$image_attribs = getimagesize($im_file_name); $im_old = imageCreateFromJpeg($im_file_name); //declare thumb$th_max_width = 100; $th_max_height = 100; $ratio = ($width > $height) ? $th_max_width/$image_attribs[0] : $th_max_height/$image_attribs[1]; $th_width = $image_attribs[0] * $ratio; $th_height = $image_attribs[1] * $ratio; $im_new = imagecreatetruecolor($th_width,$th_height); imageAntiAlias($im_new,true); //resize image$th_file_name = "/home/rack/public_html/project/jobs/$dir2/return_images/thumbs/" . $filename;imageCopyResampled($im_new,$im_old,0,0,0,0,$th_width,$th_height, $image_attribs[0], $image_attribs[1]); imageJpeg($im_new,$th_file_name,100); }foreach ($_FILES['file']['name'] as $value3) {$pathname .= "$basepath$value3";$thumbpath .= "$thumbpath$value3";$mysql = "rack_pm";mysql_select_db($mysql);$sql = "INSERT INTO `returnedfiles` (`imagepath`, `thumbpath`, `jobnum`, `pname`, `num`, `autojobnum`) VALUES ('$pathname', '$thumbpath', '$job', '$direc', NULL, '1');";mysql_query($sql) or die('Error, insert query failed');}echo $pathname;echo $thumbpath;[/code] Quote Link to comment Share on other sites More sharing options...
Barand Posted August 28, 2006 Share Posted August 28, 2006 Why use 3 foreach loops to process the files.Just process each uploaded file where I put "// process temp file" save original file create thumbnail file store both names to the db table Quote Link to comment Share on other sites More sharing options...
piznac Posted August 28, 2006 Author Share Posted August 28, 2006 Ok good point. But now the insert into is getting some strang results. It appears to be combing the files names on the 2nd instance of the array.. Quote Link to comment Share on other sites More sharing options...
Barand Posted August 28, 2006 Share Posted August 28, 2006 post current code Quote Link to comment Share on other sites More sharing options...
piznac Posted August 29, 2006 Author Share Posted August 29, 2006 got it all figured out,.. thanks man Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.