PHP PDO 未定义索引错误 使用 Fileuploader 类

PHP PDO Undefined index error Using Fileuploader class

提问人: 提问时间:10/16/2018 最后编辑:PersianMan 更新时间:10/16/2018 访问量:198

问:

嗨,我正在做一个我想使用这个类的项目,但我不断收到这个错误:file-uploader

**Notice:** Undefined index: filename in C:\xampp\htdocs\proceskontrol\partials\newtype.php on line 8

知道如何修复它吗?

我有 99,9% 的把握它与我的形式有关,但我看不到它。 这门课不是我的,是从我学校的老师那里得到的。

newtype.php 这是我的表单页面:

<?php
    $fileUploader = new FileUploader("../assets/img/type_img/");

    if (isset($_POST["btn_type_send"]) && !empty($_POST["btn_type_send"])) {
        $newfile = $fileUploader->fileUpload($_FILES['filUpload'], 250, 250);
        $error = [];
        $prodId = $products->newType($_POST, $newfile['filename']);
            if ($newfile['success'] == true){
                $notification->setNewProductNotificationSuccess();
                $success = '<div class="alert success" data-dismiss="alert" id="myAlert">
                                <a href="#" class="close">&times;</a>
                                <i class="glyphicon glyphicon-warning-sign"></i>
                                Product '.$_POST['name'].' is created!
                                </div>';
            } else {
                echo $newfile['msg'];
        }
    }
    ?>

    <div class="page-body">
        <?=@$success?>
        <div class="col-md-8 offset-2">
            <form name="filUpload" action="#" method="post" enctype="multipart/form-data">
                <div class="form-group">
                    <label for="name">Navn</label>
                        <?=@$error['name']?>
                    <input type="text" name="name" id="name" class="form-control" value="<?= @$_POST['name'] ?>">
                </div>
                <!-- Image upload card start -->
                <div class="card">
                    <div class="card-header">
                      <h5>Billede Upload</h5>
                    </div>
                    <div class="card-block">
                        <div class="sub-title">Vælg et billede</div>
                        <input type="file" name="filUpload" id="filer_input" multiple="multiple">
                    </div>
                </div>
                <!-- Image upload card end -->

                <div class="input-group">
                    <input type="submit" style="margin-bottom: 15px; z-index: 5" class="btn btn-success" value="Submit" name="btn_type_send" />
                </div>
            </form>
        </div>
    </div>

FileUploader.php 这是我的类:

<?php

class FileUploader{
    private $_errors = [
        1 => "The uploaded file exceeds the upload_max_filesize directive in php.ini",
        2 => "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form",
        3 => "The uploaded file was only partially uploaded",
        4 => "No file was uploaded",
        6 => "Missing a temporary folder. Introduced in PHP 5.0.3",
        7 => "Failed to write file to disk. Introduced in PHP 5.1.0",
        8 => "A PHP extension stopped the file upload. PHP does not provide a way to ascertain which extension caused the file upload to stop; examining the list of loaded extensions with phpinfo() may help. Introduced in PHP 5.2.0."
    ];
    private $_fileFolder;
    private $_thumbFolder;
    public $mimetype = [
        'image/png',
        'image/jpeg',
        'image/jpg',
        'image/gif'
    ];
    protected $currentFile = NULL;

// try to avoid using 0777
    public function __construct($fileFolder, $thumbFolder = null)
    {
        $this->_fileFolder = $fileFolder;
        if(!file_exists($this->_fileFolder)){
            mkdir($this->_fileFolder, 0777, true);
        }

        if($thumbFolder === null){
            $this->_thumbFolder = $fileFolder . 'thumb/';
        }

        if(!file_exists($this->_thumbFolder)){
            mkdir($this->_thumbFolder, 0777, true);
        }
    }

    /**
     * Undocumented function
     *
     * @param string $fileInput
     * @return array
     */
    public function fileUpload($fileInput, $maxWidth = null, $maxHeight = null, $quality = null)
    {
        if(isset($fileInput)) {
//            echo "<pre>",print_r($_FILES),"</pre>";

            $this->currentFile = $fileInput;
            // checking if there is code errors that match our error messages
            if(array_key_exists($this->currentFile['error'][0], $this->_errors)){
                return [
                    'success' => false,
                    'msg' => $this->_errors[$this->currentFile['error'][0]]
                ];
            }
            // Checking if the mimetype is allowed
            if(!in_array($this->currentFile['type'][0], $this->mimetype)){
                return [
                    'success' => false,
                    'msg' => "The uploaded file type is not allowed!"
                ];
            }
            $newName = time() . '_' . $this->currentFile['name'][0];
            if (move_uploaded_file($this->currentFile["tmp_name"][0], $this->_fileFolder . $newName)) {
                if($maxWidth !== null && $maxHeight !== null){
                    if(($this->currentFile['type'][0] === 'image/jpg') || ($this->currentFile['type'][0] === 'image/jpeg')){
                        if($quality !== null){
                            imagejpeg($this->resizeImage($this->_fileFolder . $newName, $this->currentFile['type'][0], $maxWidth, $maxHeight), $this->_thumbFolder . $newName, $quality);
                        } else {
                            imagejpeg($this->resizeImage($this->_fileFolder . $newName, $this->currentFile['type'][0], $maxWidth, $maxHeight), $this->_thumbFolder . $newName);
                        }
                    }
                    if($this->currentFile['type'][0] === 'image/png'){
                        if($quality !== null){
                            imagepng($this->resizeImage($this->_fileFolder . $newName, $this->currentFile['type'][0], $maxWidth, $maxHeight), $this->_thumbFolder . $newName, $quality);
                        } else {
                            imagepng($this->resizeImage($this->_fileFolder . $newName, $this->currentFile['type'][0], $maxWidth, $maxHeight), $this->_thumbFolder . $newName);
                        }
                    }
                    if($this->currentFile['type'][0] === 'image/gif'){
                        imagegif($this->resizeImage($this->_fileFolder . $newName, $this->currentFile['type'][0], $maxWidth, $maxHeight), $this->_thumbFolder . $newName);
                    }
                }
                return [
                    'success' => true,
                    'msg' => "The file ". basename($this->_fileFolder . $newName). " has been uploaded.",
                    'filename' => $newName
                ];
            } else {
                return [
                    'success' => false,
                    'msg' => "Sorry, there was an error uploading your file."
                ];
            }
        }
    }

    /**
     * Undocumented function
     *
     * @param string $fileInput
     * @return array
     */
    public function fileUploadEdit($fileInput, $maxWidth = null, $maxHeight = null, $quality = null)
    {
        if(isset($fileInput)) {
//            echo "<pre>",print_r($_FILES),"</pre>";

            $this->currentFile = $fileInput;
              // checking if there is code errors that match our error messages
            if(array_key_exists($this->currentFile['error'][0], $this->_errors)){
                return [
                    'success' => false,
                    'msg' => $this->_errors[$this->currentFile['error'][0]]
                ];
            }
            // checking if the mime type is allowed
            if(!in_array($this->currentFile['type'][0], $this->mimetype)){
                return [
                    'success' => false,
                    'msg' => "The uploaded file type is not allowed!"
                ];
            }
            $newName = time() . '_' . $this->currentFile['name'][0];
            if (move_uploaded_file($this->currentFile["tmp_name"][0], $this->_fileFolder . $newName)) {
                if($maxWidth !== null && $maxHeight !== null){
                    if(($this->currentFile['type'][0] === 'image/jpg') || ($this->currentFile['type'][0] === 'image/jpeg')){
                        if($quality !== null){
                            imagejpeg($this->resizeImage($this->_fileFolder . $newName, $this->currentFile['type'][0], $maxWidth, $maxHeight), $this->_thumbFolder . $newName, $quality);
                        } else {
                            imagejpeg($this->resizeImage($this->_fileFolder . $newName, $this->currentFile['type'][0], $maxWidth, $maxHeight), $this->_thumbFolder . $newName);
                        }
                    }
                    if($this->currentFile['type'][0] === 'image/png'){
                        if($quality !== null){
                            imagepng($this->resizeImage($this->_fileFolder . $newName, $this->currentFile['type'][0], $maxWidth, $maxHeight), $this->_thumbFolder . $newName, $quality);
                        } else {
                            imagepng($this->resizeImage($this->_fileFolder . $newName, $this->currentFile['type'][0], $maxWidth, $maxHeight), $this->_thumbFolder . $newName);
                        }
                    }
                    if($this->currentFile['type'][0] === 'image/gif'){
                        imagegif($this->resizeImage($this->_fileFolder . $newName, $this->currentFile['type'][0], $maxWidth, $maxHeight), $this->_thumbFolder . $newName);
                    }
                }
                return [
                    'success' => true,
                    'msg' => "The file ". basename($this->_fileFolder . $newName). " has been uploaded.",
                    'filename' => $newName
                ];
            } else {
                return [
                    'success' => false,
                    'msg' => "Sorry, there was an error uploading your file."
                ];
            }
        }
    }

    public function fileUploadEditUser($fileInput, $maxWidth = null, $maxHeight = null, $quality = null)
    {
        if(isset($fileInput)) {
//            echo "<pre>",print_r($_FILES),"</pre>";

            $this->currentFile = $fileInput;
            // checking if there is code errors that match our error messages
            if(array_key_exists($this->currentFile['error'], $this->_errors)){
                return [
                    'success' => false,
                    'msg' => $this->_errors[$this->currentFile['error']]
                ];
            }
            // checking if the mimetype is allowed
            if(!in_array($this->currentFile['type'], $this->mimetype)){
                return [
                    'success' => false,
                    'msg' => "The uploaded file type is not allowed!"
                ];
            }
            $newName = time() . '_' . $this->currentFile['name'];
            if (move_uploaded_file($this->currentFile["tmp_name"], $this->_fileFolder . $newName)) {
                if($maxWidth !== null && $maxHeight !== null){
                    if(($this->currentFile['type'] === 'image/jpg') || ($this->currentFile['type'] === 'image/jpeg')){
                        if($quality !== null){
                            imagejpeg($this->resizeImage($this->_fileFolder . $newName, $this->currentFile['type'], $maxWidth, $maxHeight), $this->_thumbFolder . $newName, $quality);
                        } else {
                            imagejpeg($this->resizeImage($this->_fileFolder . $newName, $this->currentFile['type'], $maxWidth, $maxHeight), $this->_thumbFolder . $newName);
                        }
                    }
                    if($this->currentFile['type'] === 'image/png'){
                        if($quality !== null){
                            imagepng($this->resizeImage($this->_fileFolder . $newName, $this->currentFile['type'], $maxWidth, $maxHeight), $this->_thumbFolder . $newName, $quality);
                        } else {
                            imagepng($this->resizeImage($this->_fileFolder . $newName, $this->currentFile['type'], $maxWidth, $maxHeight), $this->_thumbFolder . $newName);
                        }
                    }
                    if($this->currentFile['type'] === 'image/gif'){
                        imagegif($this->resizeImage($this->_fileFolder . $newName, $this->currentFile['type'], $maxWidth, $maxHeight), $this->_thumbFolder . $newName);
                    }
                }
                return [
                    'success' => true,
                    'msg' => "The file ". basename($this->_fileFolder . $newName). " has been uploaded.",
                    'filename' => $newName
                ];
            } else {
                return [
                    'success' => false,
                    'msg' => "Sorry, there was an error uploading your file."
                ];
            }
        }
    }


    /**
     * @param $filename
     * @param $mime
     * @param $max_width
     * @param $max_height
     * @return resource
     */
    private function resizeImage($filename, $mime, $max_width, $max_height)
    {
        list($orig_width, $orig_height) = getimagesize($filename);
        $width = $orig_width;
        $height = $orig_height;
        # taller
        if ($height > $max_height) {
            $width = ($max_height / $height) * $width;
            $height = $max_height;
        }
        # wider
        if ($width > $max_width) {
            $height = ($max_width / $width) * $height;
            $width = $max_width;
        }
        $image_p = imagecreatetruecolor($width, $height);
        imagealphablending($image_p, false);
        imagesavealpha($image_p, true);
        $transparent = imagecolorallocatealpha($image_p, 255, 255, 255, 127);
        imagefilledrectangle($image_p, 0, 0, $width, $height, $transparent);
        if(($mime === 'image/jpeg') || ($mime === 'image/jpg')){
            $image = imagecreatefromjpeg($filename);
        }
        if($mime === 'image/png'){
            $image = imagecreatefrompng($filename);
        }
        if($mime === 'image/gif'){
            $image = imagecreatefromgif($filename);
        }
        imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $orig_width, $orig_height);
        return $image_p;
    }
}
php pdo 未定义索引

评论

1赞 AnTrakS 10/16/2018
使用 PHP 的 “Notice: Undefined variable”, “Notice: Undefined index” 和 “Notice: Undefined offset” 的可能重复
0赞 Royal Wares 10/16/2018
您是否在$newfile上尝试过var_dump?要查看变量的结构是什么样子的?
0赞 10/16/2018
它说这个通知:未定义的变量:C:\xampp\htdocs\proceskontrol\partials\newtype.php 中的新文件 第 20 行 NULL
0赞 10/16/2018
这是否意味着我在编写 PHP :P时没有$newfile sry 我
0赞 PersianMan 10/16/2018
1-在浏览器中按 F12 并转到“网络”选项卡,然后提交表单并查看 http 参数 ...2- 在 PHP var_dump变量中$_FILES

答:

1赞 Gonzalo 10/16/2018 #1

看看你的代码,特别是这部分:

        }
        return [
            'success' => true,
            'msg' => "The file ". basename($this->_fileFolder . $newName). " has been uploaded.",
            'filename' => $newName
        ];
    } else {
        return [
            'success' => false,
            'msg' => "Sorry, there was an error uploading your file."
        ];
    }

当您的文件上传失败时,您不会返回任何索引,因此,您的上传可能失败,并且您在访问 .filenamefilename

您应该在访问 之前检查错误,或者返回一个空索引,即:filenamefilename

$newfile = $fileUploader->fileUpload($_FILES['filUpload'], 250, 250);
$error = [];
    if ($newfile['success'] == true){
        $prodId = $products->newType($_POST, $newfile['filename']);
        // ...
    } else {
        echo $newfile['msg'];
    }

评论

0赞 10/16/2018
我将数据输入到我的数据库中“类型名称”,但图像没有跟随。
0赞 Gonzalo 10/16/2018
我编辑了我的答案,您正在尝试访问不存在的索引数组。
0赞 10/16/2018
现在它只说不允许上传的文件类型!
0赞 10/16/2018
Sry 它实际上没有用,我所做的是我移动了$prodId = ...行到 if 中,正如您向我展示的那样,它删除了未定义的索引错误,但它现在不会将任何数据插入数据库,这意味着我想创建带有和不带有图像的“类型”使用该类。
0赞 Gonzalo 10/16/2018
唯一改变的是你没有打电话.我无法猜测该调用的含义,并且您传递和无效值的位置,在检查它是否正确之前,您不应该访问。$products->newType()$newfile['filename']filename