1

The script below works fine and its able to upload 5 files unto the server and insert the names into database but the only problem is when a user fails to select a file, file names are still inserted into the database

$file = rand(1000,100000)."-".$_FILES['file']['name'];
$file2 = rand(1000,100000)."-".$_FILES['file2']['name'];
$file3 = rand(1000,100000)."-".$_FILES['file3']['name'];
$file4 = rand(1000,100000)."-".$_FILES['file4']['name'];
$file5 = rand(1000,100000)."-".$_FILES['file5']['name'];
$file_loc = $_FILES['file']['tmp_name'];
$file_loc2 = $_FILES['file2']['tmp_name'];
$file_loc3 = $_FILES['file3']['tmp_name'];
$file_loc4 = $_FILES['file4']['tmp_name'];
$file_loc5 = $_FILES['file5']['tmp_name'];
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$oname = $_POST['oname'];

$folder="uploads/";
// make file name in lower case
$new_file_name = strtolower($file);
$new_file_name2 = strtolower($file2);
$new_file_name3 = strtolower($file3);
$new_file_name4 = strtolower($file4);
$new_file_name5 = strtolower($file5);
// make file name in lower case

$final_file=str_replace(' ','-',$new_file_name);
$final_file2=str_replace(' ','-',$new_file_name2);
$final_file3=str_replace(' ','-',$new_file_name3);
$final_file4=str_replace(' ','-',$new_file_name4);
$final_file5=str_replace(' ','-',$new_file_name5);

if(move_uploaded_file($file_loc,$folder.$final_file))
    if(move_uploaded_file($file_loc2,$folder.$final_file2))
        if(move_uploaded_file($file_loc3,$folder.$final_file3))
            if(move_uploaded_file($file_loc4,$folder.$final_file4))
                if(move_uploaded_file($file_loc5,$folder.$final_file5))
                    {
                    }
                else
                    {
                    }
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form")) {
    $insertSQL = sprintf("INSERT INTO applicant(fname,lname,oname,file1,file2,file3,file4,file5) VALUES('$fname','$lname','$oname','$final_file','$final_file2','$final_file3','$final_file4','$final_file5')");
}

What i want to achieve is when no file is selected, the field should be null

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • http://php.net/manual/en/function.file-exists.php –  Feb 13 '16 at 00:03
  • 1
    Isn't it a dup of http://stackoverflow.com/questions/2958167/how-to-test-if-a-user-has-selected-a-file-to-upload – kay27 Feb 13 '16 at 00:11
  • @kay27 good call :-) –  Feb 13 '16 at 00:11
  • What name is it inserting into the database for the files they didn't upload? It looks like it should just insert an empty string, since that `$_FILES[xxx]['name']` will be blank. – Barmar Feb 13 '16 at 00:13
  • @Dagon I don't think this is a duplicate of that. He's not asking how to test if a file was uploaded, he's asking how to fix the `INSERT` query to make those columns `NULL`. – Barmar Feb 13 '16 at 00:16
  • In the `INSERT` query, you can use `NULLIF('$final_file' = '', '$final_file')`, and the same for all the other filename variables. – Barmar Feb 13 '16 at 00:17
  • can you phrase it into my script because i place it into my script and that line is highlighted red meaning there's an error – user5915278 Feb 13 '16 at 07:43

1 Answers1

1

It's inserting this:

rand(1000,100000)."-"
kay27
  • 897
  • 7
  • 16