普通网友 2015-02-18 15:18
浏览 14
已采纳

PHP - 已上载检查文件,但其余信息为空

I have a form which asks for an image uploaded, title, description etc.

An error appears if no image is selected like this:

if ($_FILES["target_file"]==''){

echo 'Please upload a suitable photograph!<br>';

}

But if i select an image but don't fill in the rest of the form, it will upload it to my database.

I've tried something like this:

if (!empty($_FILES['target_file']) && (empty ($_POST['photo-title'])) && (empty ($_POST['photo-name'])) && (empty ($_POST['uploaded'])) && (empty ($_POST['genreID'])) && (empty ($_POST['description'])))
{
   echo 'Please select a photograph!<br>';
}

But this doesn't work.

Any one any ideas on how i can make it display an error after selecting an image but not filling the rest of the form?

  • 写回答

1条回答 默认 最新

  • duanqian6982 2015-02-18 15:23
    关注

    Try this:

    if(!isset($_FILES['target_file']) || !is_uploaded_file($_FILES['target_file']['tmp_name'])){
        echo 'Please upload an image!<br>';
    } else if(empty($_POST['photo-title']) || empty($_POST['photo-name']) || empty($_POST['uploaded']) || empty($_POST['genreID']) || empty($_POST['description'])){
        echo 'Please fill in all the input fields!<br>';
    } else {
        //upload everything
    }
    

    In your code, you used &&, which means that the error would only echo if all the fields were empty. Also, the exclamation mark in front of empty($_FILES['target_file']) means that you will get an error if there is a file, and no error when there isn't.

    Also, it's not necessary to enclose every empty(/*post field*/) part in brackets

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥200 csgo2的viewmatrix值是否还有别的获取方式
  • ¥15 Stable Diffusion,用Ebsynth utility在视频选帧图重绘,第一步报错,蒙版和帧图没法生成,怎么处理啊
  • ¥15 请把下列每一行代码完整地读懂并注释出来
  • ¥15 pycharm运行main文件,显示没有conda环境
  • ¥15 寻找公式识别开发,自动识别整页文档、图像公式的软件
  • ¥15 为什么eclipse不能再下载了?
  • ¥15 编辑cmake lists 明明写了project项目名,但是还是报错怎么回事
  • ¥15 关于#计算机视觉#的问题:求一份高质量桥梁多病害数据集
  • ¥15 特定网页无法访问,已排除网页问题
  • ¥50 如何将脑的图像投影到颅骨上