dongxibo2095
dongxibo2095
2013-05-21 15:48

即使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 duandi2853 8年前

    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)) {
    
    点赞 评论 复制链接分享

相关推荐