正在上传图片:未创建临时文件/名称

时间:2017-03-08 14:13:18

标签: php

我遇到了一个讨厌的bug。 我有一个表单系统,从jquery / ajax提交图像到PHP脚本。 此脚本任务是获取临时文件并使用永久名称移动它。

主要问题是它适用于大多数图像,除了没有创建临时文件的单个图像。我还可以在文件数组中看到该文件出现错误'值为1的键。

这是php代码

 public function uploadImageInTempDir(): array
    {

        $imagePath = [];
        $slugify = new Slugify();
        $currentYear = date( 'Y' );
        $currentMonth = date( 'm' );

        # Save currently uploaded file as a concrete file, instead of a temporary file.
        # We iterate to access the file in question because it is the only way to access files in $_FILES .
        # But there always is only 1 file in the array as sent from the frontend.
        foreach ( $this->files as $file ) {
            if ( ! empty( $file[ 'name' ] ) ) {
                $imageName = $slugify->slugify( $file[ 'name' ] );
                $randomString = time();
                $imageType = Utilities::getFileExtension( $file[ 'name' ] );
                $finalImageName = $imageName . '-' . $randomString . '.' . $imageType;
                $saveTargetTmp = Settings::SAVE_IMAGE_TEMP_PATH . $finalImageName;

                move_uploaded_file( $file[ 'tmp_name' ], $saveTargetTmp );
                # We add year/month to filename to store each file in corresponding year/month directory structure.
                $imagePath[ "path" ] = $currentYear . $currentMonth . '/' . $finalImageName;
            }
        }

        # We pass back to the frontend the final path of the image which will be in the form of:
        # /year/month/filename-random.ext . Note that currently image is NOT in its final
        # destination. It will have to be moved there when user finally posts the full form.
        return $imagePath;
    }

我的第一个问题是:你知道如何解释显示1的文件数组中的错误代码吗?它不会抛出我可以全局捕获的任何异常或错误: enter image description here

其次,你知道为什么会发生这种错误吗?

事实:

  • 它适用于所有类似名称的图像。

  • 这不是尺寸问题

  • 脚本接收信息,因为您可以从获得文件名的图像中看到。

这是前端代码,它报告成功上传:

$.ajax( {
                url        : '/blog/upload-image',
                type       : form.attr( 'method' ),
                contentType: false, // obligatoire pour de l'upload
                processData: false, // obligatoire pour de l'upload
                dataType   : 'json', // selon le retour attendu
                data       : data
            } ).done( function( response ) {
                console.log( `Upload image success: ${idOfUploadedImage}` )
                console.log( response )

                // front end tasks

            } ).fail( function( response ) {
                console.log( "Upload image error: ", response );
            } ).always( function() {
                console.log( "Upload image complete" );
            } );

1 个答案:

答案 0 :(得分:0)

上传错误列在here,值1是以下错误:

  

UPLOAD_ERR_INI_SIZE

     

价值:1;上传的文件超过了   php.ini中的upload_max_filesize指令。

因此您的图像文件大于php.ini中设置的最大值。