user38271 Posted November 28, 2014 Share Posted November 28, 2014 I need to check whether the user has uploaded a file when submitting a form using isset. If they have, the file path is recorded to the database. If they haven't nothing needs to occur, the file path field will just be NULL.Here is my issue:When I test it with uploading no file I'm still getting "images/" from my $ filelocation variable recorded to the database.How can I fix this?<?php if(isset($_FILES['userfile'])) { $fileupload = $_FILES['userfile']['name']; $filetype = $_FILES['userfile']['type']; $filesize = $_FILES['userfile']['size']; $tempname = $_FILES['userfile']['tmp_name']; $filelocation = "images/$fileupload"; } else { $filelocation = NULL; } if (!move_uploaded_file($tempname,$filelocation)) { switch ($_FILES['userfile']['error']) { case UPLOAD_ERR_INI_SIZE: echo "<p>Error: File exceeds the maximum size limit set by the server</p>" ; break; case UPLOAD_ERR_FORM_SIZE: echo "<p>Error: File exceeds the maximum size limit set by the browser</p>" ; break; default: echo "<p>File could not be uploaded </p>" ; } } if ($_POST["productName"] == "") { header("Location:getProductDetails.php"); exit(); } elseif ($_POST["productDescription"] == "") { header("Location:getProductDetails.php"); exit(); } else { $conn = @mysqli_connect("localhost", "root", ""); if (!$conn) { echo "The connection has failed: " . mysqli_error($conn); } else { //echo "Successfully connected to mySQL!"; $query = "CREATE DATABASE IF NOT EXISTS bazaar"; $dbName =""; if (!mysqli_query($conn,$query)) { echo "<p>Could not open the database: " . mysqli_error($conn)."</p>"; } else { //echo "<p>Database successfully created</p>"; if (!mysqli_select_db($conn,"bazaar")) { echo "<p>Could not open the database: " . mysqli_error($conn)."</p>"; } else { //echo "<p>Database selection successful</p>"; $query = "CREATE TABLE IF NOT EXISTS products(productid int primary key not null auto_increment,productname varchar(50), productdesc text, colour varchar(25), price decimal(5,2) not null,imagepath varchar(250));"; if (!mysqli_query($conn,$query)) { echo "table query failed1: " . mysqli_error($conn); } else { //echo "<p>table query successful</p>"; $insert = "INSERT INTO products (productname, productdesc, colour, price, imagepath) VALUES ('$_POST[productName]','$_POST[productDescription]','$_POST[colour]','$_POST[price]','$filelocation');"; if (mysqli_query($conn,$insert)) { $customerId = mysqli_insert_id($conn); } else { echo "table query failed: " . mysqli_error($conn); } } } } } mysqli_close($conn); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/292764-isset_files-question/ Share on other sites More sharing options...
bsmither Posted November 28, 2014 Share Posted November 28, 2014 I like to use: if(!empty($_FILES)){ foreach($_FILES as $file){ // figure out what the array $file contains } } Quote Link to comment https://forums.phpfreaks.com/topic/292764-isset_files-question/#findComment-1497912 Share on other sites More sharing options...
mac_gyver Posted November 28, 2014 Share Posted November 28, 2014 the $_FILES['userfile']['error'] element will be a UPLOAD_ERR_NO_FILE Value: 4, when no file was uploaded with the form. Quote Link to comment https://forums.phpfreaks.com/topic/292764-isset_files-question/#findComment-1497913 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.