image upload? |
image upload? |
xxkasperxx |
Apr 6 2012, 10:55 AM
Post
#1
|
Serious Coder Group: Members Posts: 261 Joined: 30-April 11 Member No.: 14,449 |
How do i create a image uploaing page?
I want the user to be able to select multiple pictures with just one window (ctlr click on pics, or drag for selected ones.) And then upload them all to the server.. I could only find out how to get 1 picture working.. imageupload.php CODE <html> <head> <title>HTML Form for uploading image to server</title> </head> <body> <form action="upload_file.php" method="post" enctype="multipart/form-data"> <p>Pictures: <input type="file" name="file" /> <input type="submit" value="Send" /> </p> </form> </body> </html> upload_file.php CODE <?php /* foreach ($_FILES['file']['name'] as $i => $name) { if ($_FILES['file']['error'][$i] == 4) { continue; } if ($_FILES['file']['error'][$i] == 0) { if ($_FILES['file']['size'][$i] > 99439443) { $message[] = "$name exceeded file limit."; continue; } $target_path = getenv("DOCUMENT_ROOT")."/Civic Association/upload/"; echo $target_path; $target_path = $target_path .basename( $name); if(move_uploaded_file($i, $target_path)) { echo "The file ". $name. " has been uploaded"; } else{ echo "<br><b>There was an error uploading the file, please try again!</b>"; } echo "<br>"; $uploaded++; } } echo $uploaded . ' files uploaded.'; foreach ($message as $error) { echo $error; } */ /*foreach ($_FILES['file'] as $position => $file) { // should output array with indices name, type, tmp_name, error, size var_dump($file); */ if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/png") || ($_FILES["file"]["type"] == "image/pjpeg")) && ($_FILES["file"]["size"] < 200000000000000000000000000000)) { if ($_FILES["file"]["error"] > 0) { echo "Return Code: " . $_FILES["file"]["error"] . "<br />"; } else { echo "Upload: " . $_FILES["file"]["name"] . "<br />"; echo "Type: " . $_FILES["file"]["type"] . "<br />"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />"; echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />"; if (file_exists("upload/" . $_FILES["file"]["name"])) { echo $_FILES["file"]["name"] . " already exists. "; } else { move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]); echo "Stored in: " . "upload/" . $_FILES["file"]["name"]; } } } else { echo "Invalid file"; } ?> |
Christian J |
Apr 6 2012, 02:02 PM
Post
#2
|
. Group: WDG Moderators Posts: 9,661 Joined: 10-August 06 Member No.: 7 |
Note sure about HTML4. HTML5 apparently allows multiple files, but only together with the MULTIPLE attribute, and I have no idea about browser support.
QUOTE ctlr click on pics, or drag for selected ones. That might require that the files where in the same directory, which may not always be the case. How about using several file INPUT elements instead? |
xxkasperxx |
Apr 7 2012, 12:07 AM
Post
#3
|
Serious Coder Group: Members Posts: 261 Joined: 30-April 11 Member No.: 14,449 |
I have tried the several inputs. But I have no clue how to get them to upload. I can get one using the code posted before. I just can't get anymore.
|
Christian J |
Apr 7 2012, 03:36 AM
Post
#4
|
. Group: WDG Moderators Posts: 9,661 Joined: 10-August 06 Member No.: 7 |
Do you use "[]" in the form field name(s), as explained here:
http://www.php.net/manual/en/faq.html.php#faq.html.arrays (and the following FAQ entry)? |
xxkasperxx |
Apr 7 2012, 11:59 AM
Post
#5
|
Serious Coder Group: Members Posts: 261 Joined: 30-April 11 Member No.: 14,449 |
I did before. But it still did not work.. im going to take a look at your link!
|
xxkasperxx |
Apr 7 2012, 12:01 PM
Post
#6
|
Serious Coder Group: Members Posts: 261 Joined: 30-April 11 Member No.: 14,449 |
I still do not get how to get the array inputs through posting to the php page.
|
xxkasperxx |
Apr 11 2012, 08:39 AM
Post
#7
|
Serious Coder Group: Members Posts: 261 Joined: 30-April 11 Member No.: 14,449 |
Any ideas on how to upload multiple images with the image array?
|
Christian J |
Apr 11 2012, 09:10 AM
Post
#8
|
. Group: WDG Moderators Posts: 9,661 Joined: 10-August 06 Member No.: 7 |
Haven't done this for a long time, but this seems to work for me:
CODE <?php header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past ?> <!doctype html> <title>multiple file upload test</title> <?php if( isset($_FILES['file']) && $_FILES['file']['error']!=4 // error 4: no file was uploaded && $_FILES['file']['size']>0 ) { echo '<pre>'; print_r($_FILES); echo '</pre>'; } ?> <form action="" method="post" enctype="multipart/form-data"> <input type="file" name="file[]"><br> <input type="file" name="file[]"><br> <input type="submit" value="Upload"> </form> The manual mentions multiple file uploads here: http://www.php.net/manual/en/features.file...ad.multiple.php |
Lo-Fi Version | Time is now: 26th April 2024 - 07:54 PM |