未定义的索引:文件

时间:2011-05-23 14:02:38

标签: php oop

我收到未定义的索引错误,因为我第一次来到我的上传表单页面,或者如果我移动到下一页并单击后退按钮,那么我有相同的错误消息。 如果我上传文件,那么它工作正常,错误消息就会消失。

我也试过这个:

global $file;
if (!isset($file)) {
    $file = '';
}

这是我的代码:

<form id="uploadForm" name="upload" enctype="multipart/form-data"/>
  <fieldset>
    <input type="hidden" name="MAX_FILE_SIZE" value="30000" />
    <input type="file" name="file" />
<?php
echo '<pre>';
var_dump($_REQUEST['file']);
echo '</pre>';

$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>";
?>
    <div class="filesize">JPG minimaal 800x60 pixels max. 2Mb</div>
    <a href="" class="submit" title="Upload your own phooto"><span> Upload your own photo </span></a>

upload_inc.php

<?
class upload
{
    var $directory_name;
    var $max_filesize;
    var $error;

    var $user_tmp_name;
    var $user_file_name;
    var $user_file_size;
    var $user_file_type;
    var $user_full_name;

    function set_directory($dir_name =".")
    {
        $this->directory_name = $dir_name;
    }

    function set_max_size($max_file = 2000000)
    {
        $this->max_filesize = $max_file;
    }

    function error()
    {
        return $this->error;
    }

    function is_ok()
    {
        if(isset($this->error))
            return FALSE;
        else
            return TRUE;
    }

    function set_tmp_name($temp_name)
    {
        $this->user_tmp_name = $temp_name;
    }

    function set_file_size($file_size)
    {
         $this->user_file_size = $file_size;
    }

    function set_file_type($file_type)
    {
        $this->user_file_type = $file_type;
    }

    function set_file_name($file)
    {
        $this->user_file_name = $file;
        $this->user_full_name = $this->directory_name."/".$this->user_file_name;
    }

    function start_copy()
    {
        if(!isset($this->user_file_name))
            $this->error = "You must define filename!";
        if ($this->user_file_size <= 0)
            $this->error = 'File size error (0):' . $this->user_file_size . 'KB <br>';
        if ($this->user_file_size > $this->max_filesize)
            $this->error = 'File size error (1):' . $this->user_file_size . 'KB<br>';
        if($this->user_file_type != "image/jpeg")
            $this->error = "the image must be jpeg extension";
        if (!isset($this->error))
        {
            $filename = basename($this->user_file_name);
            if (!empty($this->directory_name))
                $destination = $this->user_full_name;
            else
                $destination = $filename;
            if(!is_uploaded_file($this->user_tmp_name))
                $this->error = "File " . $this->user_tmp_name . " is not uploaded correctly.";
            if (!move_uploaded_file ($this->user_tmp_name,$destination))
                $this->error = "Impossible to copy " . $this->user_file_name . " from your folder to destination directory.";
        }
    }

}
?>

2 个答案:

答案 0 :(得分:5)

"Undefined index"表示您正在尝试读取不存在的数组元素。

您的具体问题似乎是您正在尝试阅读尚不存在的上传数据:当您第一次访问上传表单时,没有$_FILES数组(或者更确切地说,其中没有任何内容) ),因为表格尚未提交。但由于您没有检查表单是否已提交,因此这些行会给您带来错误:

//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']);

他们都试图阅读$_FILES['file']的值,将其传递给$uploaded的方法。

您需要的是事先检查:

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

答案 1 :(得分:1)

错误可能在您的上传课程中。错误消息非常清楚,如果那是你得到的实际消息,那个类中的某个地方可能有一行找到一个名为'fileUpload'的数组键。

只需在代码中搜索'fileUpload',然后添加一些内容以检查密钥是否已设置,即

 if(isset($arraywhatever['fileUpload'])) condition to your code.