Setting up a file to upload to your site is fairly simple, getting it to work with multiple threw me at first. this is how yer do it.
Fisrt of start your form, make sure to set the encoding type or the upload will fail straight of the bat.
|
1 |
<form method=”post” action=”process.php” enctype=”multipart/form-data”> |
You can set the max uplaod size via html but, this can be overridden, its still good to keep it in, as it will reject a larger file straight away, saves users uploading it and then being told its too large later on. (eg 5mb)
|
1 |
<input type=”hidden” name=”MAX_FILE_SIZE” value=”5000000″/> |
Now whatever the name is set to is important for the next bit, here i’ve used userfile[]
|
1 |
<input name=”userfile[]“ type=”file”/> |
Add any other bits to your form and you should have something like this
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<form method=”post” action=”process.php” enctype=”multipart/form-data”> <label> </label><h3>Add Photo</h3> <label> </label><p>File Size Limit is 4MB.</p> <label> </label><p>File Type Limited to JPG/JPEG.</p> <input type=”hidden” name=”MAX_FILE_SIZE” value=”5000000″/> <label> </label><input name=”userfile[]“ type=”file”/> <br /> <label> </label> <input name=”userfile[]“ type=”file”/> <br /> <label> </label> <input name=”userfile[]“ type=”file”/> <br /><label> </label><input type=”submit” value=”Add”/> </form> |
now we have our form we need to set up a handler for our files. Now with multiple data files we handle the userfile like an array.
|
1 2 3 |
foreach ($_FILES["userfile"]["error"] as $key=>$error) { stuff here } |
each time we need to reference the file like this
|
1 |
$_FILES["userfile"]["name"][$key]; |
in the full example below I call a database which logs the file and selects the highest id. Adding one and using that value after to keep a steady file id going.
getting to each file, first thing is check if it uploaded. if yes then get the temporary and actual filenames.
then some error checking, firstly checking file-size and then file-type. if both seem okay we move the file into our upload directory.
after we can then move elsewhere if we wish.
we add the info the the mysql database. up the number ready for the next file, and some error messages at the end.
simples.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
<? $sql=“SELECT id FROM table ORDER BY id DESC LIMIT 1“; $mysql_result=mysql_query($sql3); if (!$mysql_result3) { die(‘Invalid query: ‘.mysql_error()); }; $highest_number=mysql_result($mysql_result,0, id); $current_number=$highest_number+1; foreach ($_FILES["userfile"]["error"] as$key=>$error) { if ($error== UPLOAD_ERR_OK) { $tmp_name=$_FILES["userfile"]["tmp_name"][$key]; $name=$_FILES["userfile"]["name"][$key]; if($_FILES['userfile']['size'][$key] <4000000) { if($_FILES['userfile']['type'][$key] ==‘image/jpeg‘) { move_uploaded_file($tmp_name,“upload/$name“); rename (“upload/$name“,“folder/tree/somwhere/$name“; $sql=“INSERT INTO databse.table (id, filename) VALUES ($current_number, ‘$name‘);“; $mysql_result=mysql_query($sql); if (!$mysql_result) { die(‘Invalid query: ‘.mysql_error()); }; $current_number++; echo“SUCCESS <br/>“;} else { echo“INCORRECT FILE TYPE – JPEG ONLY<br/>“; }; } else { echo“FILE TOO LARGE – LIMIT 4MB <br/>“; };} else { echo“UNKNOWN ERROR <br/>“; }; } ?> |