仅当填写了所有字段时,文件上载才有效

时间:2015-09-07 17:06:47

标签: php upload

我有一个表单,用户可以选择上传最多4个文件。他们不必全部上传。

如果他们上传4个文件我的脚本工作正常。如果他们上传的数量少于此值,我会收到错误"注意:未定义的索引:扩展名"在filenamefetch部分中的行开始 $ fileName2 = uniqid()。 '' 。 $ fileData2 ["延伸"];对于每个空文件。

如何隐藏此错误消息?脚本的其余部分按照我的预期运行。

<?php
require_once("connect_db.php");
$ajax_result = "error";
$employeeid=$_POST['employeeid'];
$file1type=$_POST['file1type'];
$file1date=$_POST['file1date'];
$file2type=$_POST['file2type'];
$file2date=$_POST['file2date'];
$file3type=$_POST['file3type'];
$file3date=$_POST['file3date'];
$file4type=$_POST['file4type'];
$file4date=$_POST['file4date'];

//file data fetch
$fileData1 = pathinfo(basename($_FILES["file1"]["name"]));
$fileName1 = uniqid() . '.' . $fileData1["extension"];
$target_path1 = ("uploads/" . $fileName1);
$fileData2 = pathinfo(basename($_FILES["file2"]["name"]));
$fileName2 = uniqid() . '.' . $fileData2["extension"];
$target_path2 = ("uploads/" . $fileName2);
$fileData3 = pathinfo(basename($_FILES["file3"]["name"]));
$fileName3 = uniqid() . '.' . $fileData3['extension'];
$target_path3 = ("uploads/" . $fileName3);
$fileData4 = pathinfo(basename($_FILES["file4"]["name"]));
$fileName4 = uniqid() . '.' . $fileData4['extension'];
$target_path4 = ("uploads/" . $fileName4);


//image 1
while(file_exists($target_path1))
{
    $fileName1 = uniqid() . '.' . $fileData1['extension'];
    $target_path1 = ("uploads/" . $fileName1);
}

if (move_uploaded_file($_FILES['file1']['tmp_name'], $target_path1))
{    // The file is in the images/gallery folder. Insert record into database by
     $q5="INSERT INTO uploadedfiles (filetype, employeeid, filename, filedate) VALUES('$file1type', '$employeeid','$fileName1', '$file1date')";
     $r5 = mysqli_query($dbc, $q5) or die(mysqli_error($dbc));
    echo "file 1 uploaded";} else  {echo "";}

    //image 2
while(file_exists($target_path2))
{
    $fileName2 = uniqid() . '.' . $fileData2['extension'];
    $target_path2 = ("uploads/" . $fileName2);
}

if (move_uploaded_file($_FILES['file2']['tmp_name'], $target_path3))
{    // The file is in the images/gallery folder. Insert record into database by
     $q6="INSERT INTO uploadedfiles (filetype, employeeid, filename, filedate) VALUES('$file2type', '$employeeid','$fileName2', '$file2date')";
     $r6 = mysqli_query($dbc, $q6);
    echo "";} else  {echo "";}

    //image 3
while(file_exists($target_path3))
{
    $fileName3 = uniqid() . '.' . $fileData3['extension'];
    $target_path3 = ("uploads/" . $fileName3);
}

if (move_uploaded_file($_FILES['file3']['tmp_name'], $target_path3))
{    // The file is in the images/gallery folder. Insert record into database by
     $q7="INSERT INTO uploadedfiles (filetype, employeeid, filename, filedate) VALUES('$file3type', '$employeeid','$fileName3', '$file3date')";
     $r7 = mysqli_query($dbc, $q7);
    echo "";} else  {echo "";}

        //image 4
while(file_exists($target_path4))
{
    $fileName4 = uniqid() . '.' . $fileData4['extension'];
    $target_path4 = ("uploads/" . $fileName4);
}

if (move_uploaded_file($_FILES['file4']['tmp_name'], $target_path4))
{    // The file is in the images/gallery folder. Insert record into database by
     $q8="INSERT INTO uploadedfiles (filetype, employeeid, filename, filedate) VALUES('$file4type', '$employeeid','$fileName4', '$file4date')";
     $r8 = mysqli_query($dbc, $q8);
    echo "";} else  {echo "";}

0 个答案:

没有答案