dongxibo2095 2013-05-21 15:48
浏览 44
已采纳

即使IF语句为false,也会执行$ _FILES操作

I am trying to add a file upload ability to an internal company ticketing system. The code works fine for the actual upload. The problem I am running into is that if there is no file, the code still executes. I have tried many things, from using the $_FILES error coding to adding a select menu to choose if you want to add a file or not, but the code still runs.

It works like this:

  1. Ticket info is processed and entered into DB = dates, ticket info, email strings, etc
  2. File should then be validated and uploaded, and moved to a new folder (named the ticket number)
  3. File name and directory path are added to the DB.
  4. All info is passed to the notification email which is then sent out.

When this script runs, if there is no file, the DB entry is made, and the info is added to the email, but it links to non-existant directory.

Interesting tidbit: The code to make the new directory does NOT execute without the file, this is the correct action. Confusing thing is it's in the same block of code.

I have spent a day and half trying to figure this out without resolution. Any help is greatly appreciated.

//fileUpload
$fixedFileName = "";
$fileProcessed = 0;

if($_POST['yesFile'] == "yes") { //select option in form - 
    $allowedExts = array("gif", "jpeg", "jpg", "png", "txt", "pdf", "zip", "doc", "docx", "xls", "xlsx", "pcf");
    $varForExt = explode(".", $_FILES["file"]["name"]);
    $extension = end($varForExt);

    if($_FILES["file"]["size"] < 20000000 && in_array($extension, $allowedExts)) {
        if($_FILES["file"]["error"] > 0) {
            //echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
            $fixedFileName = print_r($_FILES);
        } else {
            $fixedFileName = trim(str_replace(' ','_', $_FILES["file"]["name"]));

            $dirToMake = '/var/www/html/eis/uploads/ESIG/';
            $dirToMake .= $_SESSION['lastRID'].'/';
            mkdir($dirToMake);

            if(move_uploaded_file($_FILES["file"]["tmp_name"], $dirToMake.$fixedFileName)) {
                //insert filepath/file into sql
                $fileProcessed = 1;
            }
        }
    }
}
  • 写回答

1条回答 默认 最新

  • duandi2853 2013-05-21 15:55
    关注

    Try turning on error reporting error_reporting(E_ALL); ini_set("display_errors", "on");

    I suspect the files array is empty, and php is seeing the undefined $_FILES["file"]["size"] as 0 which passes your test.

    try adding isset($_FILES["file"]) to your first test

    if(isset($_FILES["file"]) && $_FILES["file"]["size"] < 20000000 && in_array($extension, $allowedExts)) {
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器