未定义索引:文件 [英] Undefined index: file

查看:75
本文介绍了未定义索引:文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在上载表单页面中第一次遇到未定义的索引错误,或者如果我移至下一页并单击后退"按钮,那么我将收到相同的错误消息. 如果我上传文件,则可以正常工作,并且错误消息消失了.

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 message. If I upload a file then it works fine and the error message gets away.

我也尝试过这个:

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

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

}
?>

推荐答案

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

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

您的特定问题似乎是您正在尝试读取尚不存在的上传数据:当您首次访问上传表单时,没有$_FILES数组(或者说其中没有任何内容),因为尚未提交表单.但是,由于您不检查表单是否已提交,因此以下几行会给您带来错误:

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

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

They're all trying to read the value of $_FILES['file'] to pass them to the methods of $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>";
}

这篇关于未定义索引:文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆