【问题标题】:Why my json_encode get corrupted为什么我的 json_encode 损坏了
【发布时间】:2012-09-30 08:53:27
【问题描述】:
            $model = new XUploadForm;
            $model->file = CUploadedFile::getInstance( $model, 'file' );
            //We check that the file was successfully uploaded
            if( $model->file !== null ) {
                //Grab some data
                $model->mime_type = $model->file->getType( );
                $model->size = $model->file->getSize( );
                $model->name = $model->file->getName( );
                $file_extention = $model->file->getExtensionName( );
                //(optional) Generate a random name for our file
                $file_tem_name = md5(Yii::app( )->user->id.microtime( ).$model->name);
                $file_thumb_name = $file_tem_name.'_thumb.'.$file_extention;
                $file_image_name = $file_tem_name.".".$file_extention;
                if( $model->validate( ) ) {
                    //Move our file to our temporary dir
                    $model->file->saveAs( $path.$file_image_name );
                    if(chmod($path.$file_image_name, 0777 )){

     //                 Yii::import("ext.EPhpThumb.EPhpThumb");
                    // $thumb_=new EPhpThumb();
                 //      $thumb_->init();
                    // $thumb_->create($path.$file_image_name)
                    //  ->resize(110,80)
                    //  ->save($path.$file_thumb_name); 


                    }
                    //here you can also generate the image versions you need 
                    //using something like PHPThumb





                    //Now we need to save this path to the user's session
                    if( Yii::app( )->user->hasState( 'images' ) ) {
                        $userImages = Yii::app( )->user->getState( 'images' );
                    } else {
                        $userImages = array();
                    }
                     $userImages[] = array(
                          "filename" => $file_image_name,
                        'size' => $model->size,
                        'mime' => $model->mime_type,
                        "path" => $path.$file_image_name,
                       // "thumb" => $path.$file_thumb_name,
                    );
                    Yii::app( )->user->setState('images', $userImages);      
                    //Now we need to tell our widget that the upload was succesfull
                    //We do so, using the json structure defined in
                    // https://github.com/blueimp/jQuery-File-Upload/wiki/Setup
                    echo json_encode( array( array(
                            "type" => $model->mime_type,
                            "size" => $model->size,
                            "url" => $publicPath.$file_image_name,
                            //"thumbnail_url" => $publicPath.$file_thumb_name,
                            //"thumbnail_url" => $publicPath."thumbs/$filename",
                            "delete_url" => $this->createUrl( "upload", array(
                                "_method" => "delete",
                                "file" => $file_image_name
                            ) ),
                            "delete_type" => "POST"
                        ) ) );

上面的代码给了我正确的回应, [{"type":"image/jpeg","size":2266,"url":"/uploads/tmp/0b00cbaee07c6410241428c74aae1dca.jpeg","delete_url":"/api/imageUpload/upload?_method=delete&file=0b00cbaee07c6410241428c74aae1dca .jpeg","delete_type":"POST"}]

但如果我取消以下注释

         //     Yii::import("ext.EPhpThumb.EPhpThumb");
                // $thumb_=new EPhpThumb();
             //      $thumb_->init();
                // $thumb_->create($path.$file_image_name)
                //  ->resize(110,80)
                //  ->save($path.$file_thumb_name);   

它给了我错误的响应:

Mac OS X 2��ATTR�dA��Y�Ycom.apple.quarantine0001;50655994;Google\x20Chrome.app;2599ECF9-69C5-4386-B3D9-9F5CC7E0EE1D|com.google.Chrome此资源分支故意留空 ��[{"type":"image/jpeg","size":1941,"url":"/uploads/tmp/409c5921c6d20944e1a81f32b12fc380.jpeg","delete_url":"/api/imageUpload/upload?_method=delete&file =409c5921c6d20944e1a81f32b12fc380.jpeg","delete_type":"POST"}]

【问题讨论】:

  • 朋友们,请帮助我,我几乎被这个问题所困扰
  • 因为在图像之前在屏幕上打印了一些内容,很可能是您刚刚取消注释的代码...检查每条新取消注释的行的作用。
  • 感谢 DCoder。我还是不知道。实际上,注释代码用于生成缩略图。我确实看到文件生成成功。你的意思是那些代码在某处“回显/打印”?
  • 嗨约阿希姆,你太棒了。我的 Mac 是问题的根源。我没有费心在 Mac 中删除隔离区,但我从 Windows 中删除并读取了插件。神奇的事情发生了,它解​​决了问题。太感谢了。如果您在下面提供答案,我可以接受您的回答。

标签: yii json ajax-upload phpthumb


【解决方案1】:

我猜 MacOS 已经隔离了您对 ext.EPhpTHumb.EPhpThumb 或其库之一的下载。

如果您知道插件的安装位置,请尝试从终端使用xattr -d com.apple.quarantine <filename> 将其删除,或通过谷歌搜索remove mac os x quarantine status 以获取其他方法。

【讨论】:

  • 谢谢。我的 Mac 在开发网站期间给了我很多问题。 ;)
猜你喜欢
  • 2016-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-09
  • 1970-01-01
  • 2014-05-01
相关资源
最近更新 更多