如果在上传php期间没有选择文件,则会插入文件名

时间:2016-02-12 23:59:02

标签: php mysql

下面的脚本工作正常,能够将5个文件上传到服务器并将名称插入数据库,但唯一的问题是当用户选择文件失败时,文件名仍然插入数据库

$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')");
}

我想要实现的是当没有选择文件时,该字段应该为空

1 个答案:

答案 0 :(得分:1)

它正在插入:

rand(1000,100000)."-"