dqm7854 2015-09-07 17:06
浏览 26

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

I have a form where users have the option to upload up to 4 files. They do not have to upload all 4.

If they upload 4 files my script works fine. If they upload less than this I get an error "Notice: Undefined index: extension" on the lines in the filenamefetch section that start $fileName2 = uniqid() . '.' . $fileData2["extension"]; for each of the empty files.

How can I hide this error message? The rest of the script operates as I would expect.

<?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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
    • ¥20 怎么用dlib库的算法识别小麦病虫害
    • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
    • ¥15 java写代码遇到问题,求帮助
    • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
    • ¥15 关于#Java#的问题,如何解决?