Shockdot Posted March 12, 2013 Share Posted March 12, 2013 Hey, I'm trying to create a multiple image upload function for my website... However, it keeps saying this... ( ! ) Notice: Undefined index: file in C:\wamp\www\VAF\admin\uploadpic.php on line 3 Call Stack # Time Memory Function Location 1 0.0004 679336 {main}( ) ..\uploadpic.php:0 Here's my code. <?php ob_start(); session_start(); include("Config.php"); if(isset($_SESSION['username'])) { $username = $_SESSION['username']; $permission = $_SESSION['permission']; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link type="text/css" rel="stylesheet" href="CSS/CSS.css" /> <title><?php echo($title); ?></title> </head> <body> <div class="container"> <div class="loginContainer"> <img src="Images/BGTop.jpg" alt="VAF Studio" /> <?php if(isset($username)) { ?> Welcome <?php echo($username); ?>, please select what you would like to do from the list provided below... <ul> <li><a href="<?php echo($addPics); ?>">Add Pictures</a></li> <li><a href="<?php echo($addVids); ?>">Add Videos</a></li> <li><a href="<?php echo($changePrices); ?>">Change Prices</a></li> <li><a href="logout.php">Logout</a></li> </ul> <form action="uploadpic.php" method="post" enctype="multipart/form-data"> <label for="file">Filename: </label> <input type="hidden" name="MAX_FILE_SIZE" value="90000" /> <input type="file" name="file[]" id="file" multiple="true" /> <input type="submit" name="submit" value="Upload" /> </form> <?php } else { ?> <form action="checklogin.php" method="post" class="loginForm"> <label for="username">Username: </label> <input type="text" name="username" id="username" /> <br /> <label for="password">Password: </label> <input type="password" name="password" id="password" /> <br /> <input type="submit" value="Login" class="loginSubmit" name="loginsubmit" id="loginsubmit" /> </form> <?php if(isset($_GET['state'])) { if($_GET['state'] = "invalid") { ?> <div class="clear"></div> <div class="loginError"> Invalid Username or Password. </div> <?php } } } ?> </div> </div> </body> </html> <?php $files = array(); $fdata = $_FILES['file']; if(is_array($fdata['name'])) { for($i = 0; $i < count($fdata['name']); $i++) { $files[] = array( "name" => $fdata['name'][$i], "type" => $fdata['type'][$i], "tmp_name" => $fdata['tmp_name'][$i], "error" => $fdata['error'][$i], "size" => $fdata['size'][$i]); echo($files[$i]); } } else { $files[] = $fdata; } ?> Help would be greatly appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/275551-multiple-file-uploading-error/ Share on other sites More sharing options...
AyKay47 Posted March 12, 2013 Share Posted March 12, 2013 (edited) Check if the $_FILES element isset before assigning its value to a variable, this will take care of the notice. A couple other things as well: 1. Use the comparison operator ( == ) instead of the assignment operator ( = ) in conditional statements. 2. More then one file input is necessary inside of a form in order for a user to be able to upload multiple files. 3. You do not need to load an array with the values from $_FILES, as $_FILES already contains the information you need. For instance in the for loop that you have written, you can write $_FILES['file']['name'][$i] which would retrieve the name of each uploaded file. Edited March 12, 2013 by AyKay47 Quote Link to comment https://forums.phpfreaks.com/topic/275551-multiple-file-uploading-error/#findComment-1418199 Share on other sites More sharing options...
Shockdot Posted March 12, 2013 Author Share Posted March 12, 2013 Check if the $_FILES element isset before assigning its value to a variable, this will take care of the notice. A couple other things as well: 1. Use the comparison operator ( == ) instead of the assignment operator ( = ) in conditional statements. 2. More then one file input is necessary inside of a form in order for a user to be able to upload multiple files. 3. You do not need to load an array with the values from $_FILES, as $_FILES already contains the information you need. For instance in the for loop that you have written, you can write $_FILES['file']['name'][$i] which would retrieve the name of each uploaded file. I changed the PHP code to the below. However, it's saying that $_FILE['file'] is not set.... <?php if(isset($_FILES['file'])) { for($i = 0; $i < count($_FILES['file']['name']); $i++) { echo($_FILES['file']['name'][$i]); echo($_FILES['file']['type'][$i]); echo($_FILES['file']['tmp_name'][$i]); echo($_FILES['file']['error'][$i]); echo($_FILES['file']['size'][$i]); } } else { echo("Not Set"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/275551-multiple-file-uploading-error/#findComment-1418205 Share on other sites More sharing options...
AyKay47 Posted March 12, 2013 Share Posted March 12, 2013 The else condition is not needed. $_FILES will only be populated when the form has been submitted. With the else condition present, every time the script is executed without the form being submitted, it will output the text in the else block (e.g every time the page is refreshed) Quote Link to comment https://forums.phpfreaks.com/topic/275551-multiple-file-uploading-error/#findComment-1418215 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.