mysql - filenames get inserted if no file is selected during upload php

825

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

149

Answer

Solution:

It's inserting this:

rand(1000,100000)."-"

People are also looking for solutions to the problem: php - htaccess conflict between web route and public directory

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.