please i have this more that i want to use in uploading 5 images at a time, but i don't know how to upload the files into my upload folder; <form action="<?php echo $_SESSION['PHP_SELF']; ?>" method="post" enctype="multipart/form-data" name="form1" id="form1"> <table width="300" border="0" align="center" cellpadding="5" cellspacing="0"> <tr> <td align="center"><label> <input type="file" name="file" /> </label></td> </tr> <tr> <td align="center"><label> <input type="file" name="file2" /> </label></td> </tr> <tr> <td align="center"><label> <input type="file" name="file3" /> </label></td> </tr> <tr> <td align="center"><label> <input type="file" name="file4" /> </label></td> </tr> <tr> <td align="center"><p> <label> <input type="file" name="file5" /> </label> </p> </td> </tr> </table> <p align="center"> <label> <input type="submit" name="Submit" value="Upload" /> </label> </p> </form> i tried using this move to folder code; $file_name = ($_FILES["file"]["name"]); $random_digit=rand(00000,99999); $new_file_name=$random_digit . $imageResized; if (($_FILES["file"]["name"]!="") && file_exists("../upload/" . $new_file_name)) { echo "<span class='style3'>Please reupload the Picture.</span>"; } else { move_uploaded_file($_FILES["file"]["tmp_name"], "../upload/" . $new_file_name); $path2= "../upload/" . $new_file_name; but someone advised i put it in an array but i don't know how to go about that. please ow can i do this or which other less stress way can i do this. regards
There are a couple things wrong with this code. I am going to explain this under the assumption that the $_SESSION['PHP_SELF'] variable is set to the location of your php script that will process everything. Overall, your form looks to be fine from a brief look, but the php code needs some work. One thing you should know about how to handle file uploads in php is the use of the $_FILES global variable. When you pass a file input to a php script, this global variable gets populated as a two layered array. In your code above, you use $_FILES["file"]["name"] != "". This use of the variable will not go through each of your file uploads. It will only go through the one whose name is "file". For example, if you wanted to get the name of the file whose name is "file5", you'd use $_FILES["file5"]["name"]. Here is an example of a script that will check to make sure all the file upload fields are populated before doing anything with them: // go through each of your 5 file inputs. for($i=1; $i<=5; $i++) { // This condition checks to make sure the current file field is not empty. if ($_FILES["file".$i]["size"] > 0) { $new_file_name = rand(00000, 99999); // Make this new name whatever you want. // Move the file from the temporary php storage location to a permanent home. move_uploaded_file($_FILES["file".$i]["tmp_name"], "../upload/".$new_file_name); } else { // No file was input or the file size was zero. echo "<span class='style3'>Please reupload picture {$i}</span>"; } } PHP: You don't need to create your own array for this as the $_FILES global already does that. You just needed to know how to go through each element of the array.
I think the body element can have only one background image, you can't unless you have not updated your flash for long time.
Apart from your bugged code theirs several flaws within your code which I'd like to outline: 1. Don't rely on $_SERVER['PHP_SELF'] (which I'm assuming you meant) as its potentially vulnerable to XSS. 2. Theirs no need to add a new name attritube per input tag, you can simply add [] to append it to the array of files. (This will make it easier for you - server side). 3. Security flaw, never accept any file extensions, since your wanting to only upload images then only accept image extensions! Something like this... <form method="post" enctype="multipart/form-data"> <?php for ($i = 0; $i <= 5; $i++) { ?> <input name="file[]" type="file" /><br /> <?php } ?> <input type="submit" value="Upload" /> </form> <?php //the directory to save the file/s too... $upload_dir = '../upload'; //array of allowed extensions (security purposes) $allowed_exts = array( 'png', 'jpg', 'gif' ); if ($_SERVER['REQUEST_METHOD'] == 'POST') { for ($i = 0; $i < count($_FILES['file']['tmp_name']); $i++) { $ext = pathinfo($_FILES['file']['name'][$i], PATHINFO_EXTENSION); if (in_array($ext, $allowed_exts)) { $filename = "{$upload_dir}/" . uniqid(microtime()) . ".{$ext}"; //save the file with a random filename (security purposes)... if (move_uploaded_file($_FILES['file']['tmp_name'][$i], $filename)) { echo htmlspecialchars($_FILES['file']['name'][$i]) . " -> {$filename} successfully uploaded. <br />"; } else { echo htmlspecialchars($_FILES['file']['name'][$i]) . ' failed. <br />'; } } else { echo htmlspecialchars($_FILES['file']['name'][$i]) . ' failed. <br />'; } } } ?> PHP: