Why my json_encode get corrupted
Posted
by
Cullen SUN
on Stack Overflow
See other posts from Stack Overflow
or by Cullen SUN
Published on 2012-09-30T08:53:27Z
Indexed on
2012/09/30
9:37 UTC
Read the original article
Hit count: 265
$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"
) ) );
Above code give me correct response, [{"type":"image/jpeg","size":2266,"url":"/uploads/tmp/0b00cbaee07c6410241428c74aae1dca.jpeg","delete_url":"/api/imageUpload/upload?_method=delete&file=0b00cbaee07c6410241428c74aae1dca.jpeg","delete_type":"POST"}]
but if I uncomment the following
// Yii::import("ext.EPhpThumb.EPhpThumb");
// $thumb_=new EPhpThumb();
// $thumb_->init();
// $thumb_->create($path.$file_image_name)
// ->resize(110,80)
// ->save($path.$file_thumb_name);
it gave me corrupted response:
Mac OS X 2??ATTR?dA??Y?Ycom.apple.quarantine0001;50655994;Google\x20Chrome.app;2599ECF9-69C5-4386-B3D9-9F5CC7E0EE1D|com.google.ChromeThis resource fork intentionally left blank ??[{"type":"image/jpeg","size":1941,"url":"/uploads/tmp/409c5921c6d20944e1a81f32b12fc380.jpeg","delete_url":"/api/imageUpload/upload?_method=delete&file=409c5921c6d20944e1a81f32b12fc380.jpeg","delete_type":"POST"}]
© Stack Overflow or respective owner