Undefined index: file

"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>";
}

Leave a Comment