bryanptcs Posted December 18, 2006 Share Posted December 18, 2006 I have the below code that will allow me to upload 1 file. Is there a way to loop it, so I can have multiple file uploads?[code]$dir = "artwork/".$business;$rights = 0777; if (is_dir("$dir")) { echo "A directory with your busines name ".$business." already exists and your files have been uploaded to that directory. If this has been done in error, please contact the website administrator. Thank you.<br><br>"; } else { mkdir ($dir, $rights); echo "A directory named ".$business." has just been created. Thank you.<br><br>"; } $allowed_ext = "jpg, gif, png, pdf, txt, psd, ai, doc, zip, rar, gz, jpeg"; // These are the allowed extensions of the files that are uploaded$max_size = "50000"; // 50000 is the same as 50kb$print_size = ($max_size/1000)."kb";//$max_height = "100"; // This is in pixels - Leave this field empty if you don't want to upload images//$max_width = "100"; // This is in pixels - Leave this field empty if you don't want to upload images // Check Entension$extension = pathinfo($_FILES['file']['name']);$extension = $extension[extension];$allowed_paths = explode(", ", $allowed_ext);for($i = 0; $i < count($allowed_paths); $i++) { if ($allowed_paths[$i] == "$extension") { $ok = "1"; }}// Check File Sizeif ($ok == "1") {if($_FILES['file']['size'] > $max_size){print "<font color='#FF0000'>Error:</font> File size is too large. It must be ".$print_size;print "<br><br><a href='procedures.html'>back</a>";exit;}// Check Height & Width//if ($max_width && $max_height) {//list($width, $height, $type, $w) = getimagesize($_FILES['file']['tmp_name']);//if($width > $max_width || $height > $max_height)//{//print "File height and/or width are too big!";//exit;//}//}//check if the file existsif(file_exists($dir.'/'.$_FILES['file']['name'])){ print "<font color='#FF0000'>Error:</font> File: (".$_FILES['file']['name'].") already exists.<br><br>"; print "<a href='procedures.html'>back</a>"; exit; }// The Upload Partif(is_uploaded_file($_FILES['file']['tmp_name'])){move_uploaded_file($_FILES['file']['tmp_name'],$dir.'/'.$_FILES['file']['name']);}print "Your file has been uploaded successfully. Thank you.<br><br>";print "<a href='procedures.html'>back</a>";} else {print "<font color='#FF0000'>Error:</font> Incorrect file extension. File must be one of the following:<br>".$allowed_ext;print "<br><br><a href='procedures.html'>back</a>";}[/code] Link to comment https://forums.phpfreaks.com/topic/31138-looping-statements-for-multiple-file-upload/ Share on other sites More sharing options...
craygo Posted December 18, 2006 Share Posted December 18, 2006 I just had this for someone else. Modify it to your liking. This will copy a full size image to the folder you want below and create a thumbnail image in the folder you list below. If you do not want that you can take the function out and take out the call to the function in the loop and just uncomment the copy line in the loop[code]<?php$absolute_path = "/path/to/your/full/size/image/folder"; //Absolute path to where files are uploaded$thumb_path = "/path/to/thumbnail/folder"; //Absolute path to where thumbs are to be stored if you want this$size_limit = "yes"; //do you want a size limit yes or no.$limit_size = "600000"; //How big do you want size limit to be in bytes$limit_ext = "yes"; //do you want to limit the extensions of files uploaded$ext_count = "1"; //total number of extensions in array below$extensions = array(".jpg"); //List extensions you want files uploaded to befunction resampimagejpg($forcedwidth, $forcedheight, $sourcefile, $destfile, $imgcomp) { $g_imgcomp=100-$imgcomp; $g_srcfile=$sourcefile; $g_dstfile=$destfile; $g_fw=$forcedwidth; $g_fh=$forcedheight; if(file_exists($g_srcfile)) { $g_is=getimagesize($g_srcfile); if(($g_is[0]-$g_fw)>=($g_is[1]-$g_fh)) { $g_iw=$g_fw; $g_ih=($g_fw/$g_is[0])*$g_is[1]; } else { $g_ih=$g_fh; $g_iw=($g_ih/$g_is[1])*$g_is[0]; } $img_src=imagecreatefromjpeg($g_srcfile); $img_dst=imagecreatetruecolor($g_iw,$g_ih); imagecopyresampled($img_dst, $img_src, 0, 0, 0, 0, $g_iw, $g_ih, $g_is[0], $g_is[1]); imagejpeg($img_dst, $g_dstfile, $g_imgcomp); imagedestroy($img_dst); return true; } else return false; }if(!isset($_POST['submit'])){ if (($extensions == "") or ($extensions == " ") or ($ext_count == "0") or ($ext_count == "") or ($limit_ext != "yes") or ($limit_ext == "")) { $extens = "any extension"; } else { $ext_count2 = $ext_count+1; for($counter=0; $counter<$ext_count; $counter++) { $extens = " $extensions[$counter]"; } } if (($limit_size == "") or ($size_limit != "yes")) { $limit_size = "any size"; } else { $limit_size .= " bytes"; $mb_size = ($limit_size/1000000); } $pichead = "<li><font size=\"2\" color=660000>File extension must be $extens<b>"; $pichead .="</b></font> <li><font size=\"2\" color=660000>Maximum file size is $limit_size ($mb_size MB)</font></li> <li><font size=\"2\" color=660000>No spaces in the filename</font></li>";?><html><head><title>HTML Form for uploading image to server</title></head><body><form action="" method="post" enctype="multipart/form-data"><html><title>Add Vehicle Form</title><body><form action="<?=$_SERVER['PHP_SELF']?>" method="post"><p>Pictures:<br />1 <input type="file" name="pictures[]" /><br />2 <input type="file" name="pictures[]" /><br />3 <input type="file" name="pictures[]" /><br />4 <input type="file" name="pictures[]" /><br />5 <input type="file" name="pictures[]" /><br />6 <input type="file" name="pictures[]" /><br /><input type="submit" name=submit value="Send" /></p></form><?php} else {$i=0;$photoarray = array(); foreach ($_FILES["pictures"]["error"] as $key => $error) { $file_name = $_FILES["pictures"]['name'][$i]; // can call this anything you like this will take the original name $file = $_FILES["pictures"]['tmp_name'][$i]; $photoarray[$i+1]= $file_name; $endresult = "<font size=\"4\" color=990000>$file_name uploaded successfully</font>"; if ($file_name == "") { $pic = $i+1; $endresult = "<font size=\"4\" color=990000>Pic#$pic Not selected</font>"; }else{ if(file_exists("$absolute_path/$file_name")) { $endresult = "<font size=\"4\" color=990000>File Already Existed</font>"; } else { if (($size_limit == "yes") && ($limit_size < $file_size)) { $endresult = "<font size=\"4\" color=990000>File was to big</font>"; } else { $ext = strrchr($file_name,'.'); if (($limit_ext == "yes") && (!in_array($ext,$extensions))) { $endresult = "<font size=\"4\" color=990000>File is wrong type</font>"; }else{ // Save full size image with max width/height resampimagejpg(1000,1000,$file,"$absolute_path/$file_name",0); // Save thumb image with max width/height of 200 resampimagejpg(200,200,$file,"$thumb_path/$file_name",0); //copy($file, "$absolute_path/$file_name") or $endresult = "<font size=\"4\" color=990000>Couldn't Copy File To Server</font>"; } } } } $i++; echo $endresult."<br>"; }}?></body></html>[/code]Ray Link to comment https://forums.phpfreaks.com/topic/31138-looping-statements-for-multiple-file-upload/#findComment-143840 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.