stublackett Posted November 3, 2008 Share Posted November 3, 2008 Hi Guys, I've got an Image Upload Script setup, But it just wont work The code is as follows <?php // create flag(s) for validation errors - $errors = array(); // an array is generally used for this, where the index name would relate each error element to the field it corresponds to (should you want to individually output errors next to the field) // check if the form has been submitted if(isset($_POST['submit'])) { //Collect Form Vars extract($_POST); $img = $_FILES['image1']['name']; //Check Forms' Post Vars if (!empty($_POST['carname'])) { $carname = $_POST['carname']; }else{ $carname = NULL; $errors['carname'] = '<p><font color="red">You need to enter the cars name</font></p>'; } if (!empty($_POST['carsprice'])) { $price = $_POST['carsprice']; }else{ $price = NULL; $errors['carsprice'] = '<p><font color="red">You need to enter the cars price</font></p>'; } //Handle the Image //Set Images Upload Directory $uploaddir = "../carpictures"; // Upload Part if(is_uploaded_file($_FILES['image1']['tmp_name'])) { move_uploaded_file($_FILES['image1']['tmp_name'],$uploaddir.'/'.$_FILES['image1']['name']); } // If everything is filled out print the message. if(empty($errors)) { // If all is ok, Insert into DB $sql = "INSERT INTO $db_table(carname, description, price, feature1, feature2, feature3, feature4, image1) values ('$carname','$description','$price','$feature1','$feature2','$feature3','$feature4','$img')"; ($result = mysql_query($sql ,$db) or die(mysql_error())); echo "Thank you! The Car <b>$carname</b> has been added to the site"; exit; } } // if the form was not submitted or there were validation errors, display the form - if(!isset($_POST['submit']) || !empty($errors)) { ?> But the key errors are on lines 32 Line 32s' Error is : Undefined index: image1 in bla bla $img = $_FILES['image1']['name']; and the other error is line 54 Error being : Undefined index: image1 in bla bla if(is_uploaded_file($_FILES['image1']['tmp_name'])) If anyone has any suggestions why this is happening, Please fire away, Or if they have a way to maybe to improve the code etc, Then fire away Thanks Quote Link to comment https://forums.phpfreaks.com/topic/131182-image-upload/ Share on other sites More sharing options...
JonnoTheDev Posted November 3, 2008 Share Posted November 3, 2008 Do you have the following in your form: enctype="multipart/form-data" i.e. <form action="upload.php" method="post" enctype="multipart/form-data"> Quote Link to comment https://forums.phpfreaks.com/topic/131182-image-upload/#findComment-681064 Share on other sites More sharing options...
stublackett Posted November 3, 2008 Author Share Posted November 3, 2008 Yeah, I have that in <form action="addcar.php" method="post" enctype="multipart/form-data"> Quote Link to comment https://forums.phpfreaks.com/topic/131182-image-upload/#findComment-681066 Share on other sites More sharing options...
JasonLewis Posted November 3, 2008 Share Posted November 3, 2008 Are you sure that 'image1' is what your file upload field is called? Quote Link to comment https://forums.phpfreaks.com/topic/131182-image-upload/#findComment-681077 Share on other sites More sharing options...
PFMaBiSmAd Posted November 3, 2008 Share Posted November 3, 2008 For uploads to work, your form must be correct and uploads must be enabled in php.ini. Posting your form would be the quickest way to determine if it is correct and a phpinfo() statement would tell you if uploads are enabled. Quote Link to comment https://forums.phpfreaks.com/topic/131182-image-upload/#findComment-681095 Share on other sites More sharing options...
stublackett Posted November 3, 2008 Author Share Posted November 3, 2008 Here is my HTML Form <form action="addcar.php" method="post" enctype="multipart/form-data"> <h2>Add a new car</h2> <ul> <li><label>Car Name : </label><input type="text" name="carname" tabindex="1" /></li> <li><label>Cars' Description : </label><textarea name="description" cols="40" rows="5" tabindex="2"></textarea></li> <li><label>Price : </label><input name="carsprice" type="text" tabindex="3" id="carsprice" /> <li> <li><label>Feature 1 : </label><input name="feature1" type="text" tabindex="4" /><em>Car feature i.e ABS</em></li> <li><label>Feature 2 : </label><input name="feature2" type="text" tabindex="5" /><em>Car feature i.e ABS</em></li> <li><label>Feature 3 : </label><input name="feature3" type="text" tabindex="6" /><em>Car feature i.e ABS</em></li> <li><label>Feature 4 : </label><input name="feature4" type="text" tabindex="7" /><em>Car feature i.e ABS</em></li> <li><label>Feature 5 : </label><input name="feature5" type="text" tabindex="8" /><em>Car feature i.e ABS</em></li> <li><label>Image 1 : </label><input type="file" name="image1" /><em>* This is the main image that will appear on the site</em></li> <li><label>Image 2 : </label><input type="file" name="image2" /></li> <li><label>Image 3 : </label><input type="file" name="image3" /></li> <li><label>Image 4 : </label><input type="file" name="image4" /></li> <br /><br /> <li> <input type="submit" name="submit" id="submit" value="Submit" /> <input type="reset" name="reset" id="reset" value="Reset" /> </li> </ul> </form> Quote Link to comment https://forums.phpfreaks.com/topic/131182-image-upload/#findComment-681100 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.