Shifter Posted August 20, 2011 Share Posted August 20, 2011 Hi guys ;I had created before a new upload center to my site but i tried to make a multi-upload image Center but all my tries failed This is the code i have tried : <?php if(isset($_POST['submit'])) { for($i=0;$i<count($_FILES['image']);$i++) { $file_name = $_FILES["image"]["name"];$filesize = $_FILES['image']['size'];$filetype = $_FILES['image']['type'];$filetypes = array ('image/jpg','image/jpg','image/GIF','image/JPEG','image/JPG','image/gif','image/jpeg'); $file_name1 = strchr($file_name,'.'); $new_file = time(00000,99999).$file_name1; $path_image = "script/upload/".$new_file; move_uploaded_file($_FILES["image"]["tmp_name"],$path_image);echo "<br /><hr /><img src='http://www.3arabarchive.net/$path_image'/><br><br>Code: <input type='text' size='50' value='[img=http://www.mysite.net/".$path_image."]'>"; } } ?> <html> <body> <form method="post" action="upload_several.php" enctype="multipart/form-data"> <table> <tr><td> <input type="file" name="image[]"> </td></tr> <tr><td> <input type="file" name="image[]"> </td></tr> <tr><td> <input type="file" name="image[]"> </td></tr> <tr><td> <input type="file" name="image[]"> </td></tr> <tr><td> <input type="submit" value="upload"> </td></tr> </table> </form> </body> </html> Any Help !!!Thanks and good bye Link to comment Share on other sites More sharing options...
Sami Posted August 21, 2011 Share Posted August 21, 2011 Hi.Try giving the input fields different names.you can also make it posibille to select multiple files in one field. You might want to try that. <input type='file' name='image' multiple='multiple'> And $_POST["submit"] isn't set by the form.and Im not gay btw:P Link to comment Share on other sites More sharing options...
jeffman Posted August 21, 2011 Share Posted August 21, 2011 Your input naming convention is correct for this sort of thing. But your PHP script needs to follow through.$_FILES["image"]["name"] does not refer to a real element. What you probably want is$_FILES["image"][$i]["name"] Link to comment Share on other sites More sharing options...
Shifter Posted August 21, 2011 Author Share Posted August 21, 2011 Hi.Try giving the input fields different names.you can also make it posibille to select multiple files in one field. You might want to try that.<input type='file' name='image' multiple='multiple'> And $_POST["submit"] isn't set by the form.and Im not gay btw:P Sorry for this my keyboard bad I have tried like what you say and still no upload results >>>> <?php if(isset($_POST['submit'])) { for($i=0;$i<count($_FILES['image']);$i++) { $file_name = $_FILES["image"][$i]["name"];$filesize = $_FILES['image']['size'];$filetype = $_FILES['image']['type'];$filetypes = array ('image/jpg','image/jpg','image/GIF','image/JPEG','image/JPG','image/gif','image/jpeg'); $file_name1 = strchr($file_name,'.'); $new_file = time(00000,99999).$file_name1; $path_image = "script/upload/".$new_file; move_uploaded_file($_FILES["image"]["tmp_name"],$path_image);echo "<br /><hr /><img src='http://www.3arabarchive.net/$path_image'/><br><br>Code: <input type='text' size='50' value='[img=http://www.3arabarchive.net/".$path_image."]'>"; } } ?> <html> <body> <form method="post" action="upload_several.php" enctype="multipart/form-data"> <table> <tr><td> <input type='file' name='image' multiple='multiple'> </td></tr> <tr><td> <input type='file' name='image' multiple='multiple'> </td></tr> <tr><td> <input type='file' name='image' multiple='multiple'> </td></tr> <tr><td> <input type='file' name='image' multiple='multiple'> </td></tr> <tr><td> <input type="submit" value="upload"> </td></tr> </table> </form> </body> </html> Your input naming convention is correct for this sort of thing. But your PHP script needs to follow through.$_FILES["image"]["name"] does not refer to a real element. What you probably want is$_FILES["image"][$i]["name"]I have added this .... but no results Link to comment Share on other sites More sharing options...
jeffman Posted August 21, 2011 Share Posted August 21, 2011 "no results" is not a very helpful explanation. What does happen? Do you see any error codes?To begin debugging, try putting this at the top of your script:var_dump($_POST);Find out what data actually reaches your server. Link to comment Share on other sites More sharing options...
Shifter Posted August 21, 2011 Author Share Posted August 21, 2011 ok try this is the link : http://www.3arabarchive.net/upload_several.php Link to comment Share on other sites More sharing options...
jeffman Posted August 22, 2011 Share Posted August 22, 2011 I think I was wrong twice. Dumping $_POST will tell us nothing. var_dump($_FILES) should be more productive. Also, I think I misplaced the index. Try accessing your files like this instead:$_FILES["image"]["name"][$i]Either way, what you find in the $_FILES array will show you exactly how to access what you need. Link to comment Share on other sites More sharing options...
jeffman Posted August 22, 2011 Share Posted August 22, 2011 Also, be sure to follow the links here. Link to comment Share on other sites More sharing options...
Shifter Posted August 22, 2011 Author Share Posted August 22, 2011 this is the message that appears : array(1) { ["image"]=> array(5) { ["name"]=> string(0) "" ["type"]=> string(0) "" ["tmp_name"]=> string(0) "" ["error"]=> int(4) ["size"]=> int(0) } } Link to comment Share on other sites More sharing options...
Shifter Posted August 22, 2011 Author Share Posted August 22, 2011 Any help ?! Link to comment Share on other sites More sharing options...
jeffman Posted August 22, 2011 Share Posted August 22, 2011 I've just noticed that you changed thisname="image[]"to thisname="image"As I told you in my first post, the first naming convention is correct. The example I linked you to also uses this convention. The empty brackets cause PHP to create an auto-incrementing array of files. Without them, you will only get the last file. Link to comment Share on other sites More sharing options...
Shifter Posted August 22, 2011 Author Share Posted August 22, 2011 That's the code after editing <?php var_dump($_FILES) ?> <?php if(isset($_POST['submit'])) { for($i=0;$i<count($_FILES['image[]']);$i++) { $file_name = $_FILES["image[]"]["name"][$i];$filesize = $_FILES['image[]']['size'];$filetype = $_FILES['image[]']['type'];$filetypes = array ('image/jpg','image/jpg','image/GIF','image/JPEG','image/JPG','image/gif','image/jpeg'); $file_name1 = strchr($file_name,'.'); $new_file = time(00000,99999).$file_name1; $path_image = "script/upload/".$new_file; move_uploaded_file($_FILES["image[]"]["tmp_name"],$path_image);echo "<br /><hr /><img src='http://www.3arabarchive.net/$path_image'/><br><br>Code: <input type='text' size='50' value='[img=http://www.3arabarchive.net/".$path_image."]'>"; } } ?> <html> <body> <form method="post" action="upload_several.php" enctype="multipart/form-data"> <table> <tr><td> <input type='file' name="image[]" multiple='multiple'> </td></tr> <tr><td> <input type='file' name="image[]" multiple='multiple'> </td></tr> <tr><td> <input type='file' name="image[]" multiple='multiple'> </td></tr> <tr><td> <input type='file' name="image[]" multiple='multiple'> </td></tr> <tr><td> <input type="submit" value="upload"> </td></tr> </table> </form> </body> </html> this is the result : array(1) { ["image"]=> array(5) { ["name"]=> array(4) { [0]=> string(5) "1.jpg" [1]=> string(5) "2.JPG" [2]=> string(0) "" [3]=> string(0) "" } ["type"]=> array(4) { [0]=> string(10) "image/jpeg" [1]=> string(10) "image/jpeg" [2]=> string(0) "" [3]=> string(0) "" } ["tmp_name"]=> array(4) { [0]=> string(14) "/tmp/phptLorCX" [1]=> string(14) "/tmp/phpBcpwur" [2]=> string(0) "" [3]=> string(0) "" } ["error"]=> array(4) { [0]=> int(0) [1]=> int(0) [2]=> int(4) [3]=> int(4) } ["size"]=> array(4) { [0]=> int(11162) [1]=> int(23303) [2]=> int(0) [3]=> int(0) } } } Link to comment Share on other sites More sharing options...
jeffman Posted August 22, 2011 Share Posted August 22, 2011 Well, you're uploading all the files now. Good step. The PHP needs to change a little (not the form -- it's working fine). Specifically, change all references to this:$_FILES['image[]']to this$_FILES['image']With luck, that'll clear things up. Link to comment Share on other sites More sharing options...
Shifter Posted August 22, 2011 Author Share Posted August 22, 2011 after editing this appears : array(1) { ["image"]=> array(5) { ["name"]=> array(4) { [0]=> string(5) "1.jpg" [1]=> string(5) "2.JPG" [2]=> string(5) "3.JPG" [3]=> string(5) "4.JPG" } ["type"]=> array(4) { [0]=> string(10) "image/jpeg" [1]=> string(10) "image/jpeg" [2]=> string(10) "image/jpeg" [3]=> string(10) "image/jpeg" } ["tmp_name"]=> array(4) { [0]=> string(14) "/tmp/php6dSM0a" [1]=> string(14) "/tmp/phpnAEsi6" [2]=> string(14) "/tmp/phpsU8R35" [3]=> string(14) "/tmp/php5ReHIu" } ["error"]=> array(4) { [0]=> int(0) [1]=> int(0) [2]=> int(0) [3]=> int(0) } ["size"]=> array(4) { [0]=> int(11162) [1]=> int(23303) [2]=> int(31257) [3]=> int(67294) } } } Link to comment Share on other sites More sharing options...
jeffman Posted August 22, 2011 Share Posted August 22, 2011 I don't think you need to var_dump($_FILES) anymore, so you can delete that line of code.Is there still a problem? Link to comment Share on other sites More sharing options...
Shifter Posted August 22, 2011 Author Share Posted August 22, 2011 Yes no images appears .... i have checked the upload folder to see if there any images uploaded but i found no thing ... Link to comment Share on other sites More sharing options...
jeffman Posted August 22, 2011 Share Posted August 22, 2011 Please post your new PHP code. Link to comment Share on other sites More sharing options...
Shifter Posted August 22, 2011 Author Share Posted August 22, 2011 <?php if(isset($_POST['submit'])) { for($i=0;$i<count($_FILES['image']);$i++) { $file_name = $_FILES['image']["name"][$i];$filesize = $_FILES['image']['size'];$filetype = $_FILES['image']['type'];$filetypes = array ('image/jpg','image/jpg','image/GIF','image/JPEG','image/JPG','image/gif','image/jpeg'); $file_name1 = strchr($file_name,'.'); $new_file = time(00000,99999).$file_name1; $path_image = "script/upload/".$new_file; move_uploaded_file($_FILES['image']["tmp_name"],$path_image);echo "<br /><hr /><img src='http://www.3arabarchive.net/$path_image'/><br><br>Code: <input type='text' size='50' value='[img=http://www.3arabarchive.net/".$path_image."]'>"; } } ?> <html> <body> <form method="post" action="upload_several.php" enctype="multipart/form-data"> <table> <tr><td> <input type='file' name="image[]" multiple='multiple'> </td></tr> <tr><td> <input type='file' name="image[]" multiple='multiple'> </td></tr> <tr><td> <input type='file' name="image[]" multiple='multiple'> </td></tr> <tr><td> <input type='file' name="image[]" multiple='multiple'> </td></tr> <tr><td> <input type="submit" value="upload"> </td></tr> </table> </form> </body> </html> Link to comment Share on other sites More sharing options...
jeffman Posted August 22, 2011 Share Posted August 22, 2011 I should have noticed this too. Your form does not have an input with the name "submit". This means that $_POST['submit'] is not set, so the important part of your code is not executing. The easiest fix is to change this part of your form:<input type="submit" value="upload">to this:<input type="submit" name="submit" value="upload"> Link to comment Share on other sites More sharing options...
justsomeguy Posted August 22, 2011 Share Posted August 22, 2011 You're not doing any error checking at all, and you're not getting the data correctly. The name is fine:$file_name = $_FILES['image']["name"][$i];but none of the others are the same:$filesize = $_FILES['image']['size'];You also define an array called $filetypes, but you never use it for anything.First things first. Add this to the top of your PHP code:ini_set('display_errors', 1);error_reporting(E_ALL);Once that's done, check for errors. This page lists the error codes for file uploads:http://www.php.net/manual/en/features.file-upload.errors.phpThis page has examples for checking errors, although the example works with a single file:http://www.w3schools.com/php/php_file_upload.asp Link to comment Share on other sites More sharing options...
Shifter Posted August 22, 2011 Author Share Posted August 22, 2011 I should have noticed this too. Your form does not have an input with the name "submit". This means that $_POST['submit'] is not set, so the important part of your code is not executing. The easiest fix is to change this part of your form:<input type="submit" value="upload">to this:<input type="submit" name="submit" value="upload">I have treid and the images didn't uploaded but an echo data came over : You're not doing any error checking at all, and you're not getting the data correctly. The name is fine:$file_name = $_FILES['image']["name"][$i];but none of the others are the same:$filesize = $_FILES['image']['size'];You also define an array called $filetypes, but you never use it for anything.First things first. Add this to the top of your PHP code:ini_set('display_errors', 1);error_reporting(E_ALL);Once that's done, check for errors. This page lists the error codes for file uploads:http://www.php.net/manual/en/features.file-upload.errors.phpThis page has examples for checking errors, although the example works with a single file:http://www.w3schools.com/php/php_file_upload.asp I have added the [$i] to all $_FILES super global and work fine but still lettel problem : i have uploaded just 2 images and this appears : Link to comment Share on other sites More sharing options...
justsomeguy Posted August 22, 2011 Share Posted August 22, 2011 I don't understand what the problem is. What's wrong in that picture? Did you turn on error reporting? Are you checking for error codes with the upload like the pages I linked to show? Link to comment Share on other sites More sharing options...
Shifter Posted August 23, 2011 Author Share Posted August 23, 2011 Now we solve all the problems except that when i upload 3 images the script echo the first image and repeat the 3rd image at the place of the 2nd image another exapmle : if i uploaded 4 images the first one appears and the 2nd,3rd.4th images are the same as the last image ...this is the code i use : <?php if(isset($_POST['submit'])) { for($i=0;$i<count($_FILES['image']);$i++) { $file_name = $_FILES['image']["name"][$i];$filesize = $_FILES['image']['size'][$i];$filetype = $_FILES['image']['type'][$i];$filetypes = array ('image/jpg','image/jpg','image/GIF','image/JPEG','image/JPG','image/gif','image/jpeg'); $file_name1 = strchr($file_name,'.'); $new_file = time(00000,99999).$file_name1; $path_image = "script/upload/".$new_file; move_uploaded_file($_FILES['image']["tmp_name"][$i],$path_image);if ($file_name) { echo "<br /><hr /><img src='http://www.3arabarchive.net/$path_image'/><br><br>Code: <input type='text' size='50' value='[img=http://www.3arabarchive.net/".$path_image."]'>"; }else {echo " ";} } } ?> <html> <body> <form method="post" action="upload_several.php" enctype="multipart/form-data"> <table> <tr><td> <input type='file' name="image[]"> </td></tr> <tr><td> <input type='file' name="image[]"> </td></tr> <tr><td> <input type='file' name="image[]"> </td></tr> <tr><td> <input type='file' name="image[]"> </td></tr> <tr><td> <input type="submit" name="submit" value="upload"> </td></tr> </table> </form> </body> </html> Link to comment Share on other sites More sharing options...
jeffman Posted August 23, 2011 Share Posted August 23, 2011 I think the problem is here:time(00000,99999)First, time() does not take any arguments. The "00000,99999" doesn't do anything wrong, it just doesn't do anything.Second, time() returns the current time in seconds. If the images are small enough, your entire process should be complete in less than 1 second. That means that some or even all of your files will have the same name. So some of the files will be erased, and the names that are echoed will mostly be the same.You need a new technique for inventing unique filenames. This might be good enough if you want something very simple: sha1(microtime() )If you read about microtime you should be able to come up with something a little better. The examples are helpful. Link to comment Share on other sites More sharing options...
Shifter Posted August 23, 2011 Author Share Posted August 23, 2011 Works fine my friend thanks a lot you are brilliant Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.