Undefined index: file

后端 未结 2 1857
轮回少年
轮回少年 2020-12-01 23:07

I am getting the undefined index error as I come for the first time in my upload form page or if I move to next page and click on back button then I have the same error mess

相关标签:
2条回答
  • 2020-12-01 23:16

    "Undefined index" means you're trying to read an array element that doesn't exist.

    Your specific problem seems to be that you're trying to read upload data that doesn't exist yet: When you first visit your upload form, there is no $_FILES array (or rather, there's nothing in it), because the form hasn't been submitted. But since you don't check if the form was submitted, these lines net you an error:

    //Set Temp Name for upload.
    $uploaded->set_tmp_name($_FILES['file']['tmp_name']);
    //Set file size
    $uploaded->set_file_size($_FILES['file']['size']);
    //set file type
    $uploaded->set_file_type($_FILES['file']['type']);
    //set file name
    $uploaded->set_file_name($_FILES['file']['name']);
    

    They're all trying to read the value of $_FILES['file'] to pass them to the methods of $uploaded.

    What you need is a check beforehand:

    if (isset($_FILES['file'])) {
        $uploaded = new upload;
        //set Max Size
        $uploaded->set_max_size(350000);
        //Set Directory
        $uploaded->set_directory("data");
        //Set Temp Name for upload.
        $uploaded->set_tmp_name($_FILES['file']['tmp_name']);
        //Set file size
        $uploaded->set_file_size($_FILES['file']['size']);
        //set file type
        $uploaded->set_file_type($_FILES['file']['type']);
        //set file name
        $uploaded->set_file_name($_FILES['file']['name']);
        //start copy process
        $uploaded->start_copy();
        if($uploaded->is_ok()) 
            echo " upload is doen.";
        else
            $uploaded->error()."<br>";
    }
    
    0 讨论(0)
  • 2020-12-01 23:25

    The error is probably in your upload class. The error message is pretty clear, if that is the actual message you get there is probably a line somewhere in that class that looks for an array key I that is named 'fileUpload'.

    Just do a search in your code for 'fileUpload', and add something to check if the key is set, ie

     if(isset($arraywhatever['fileUpload'])) condition to your code.
    
    0 讨论(0)
提交回复
热议问题