douyan8267 2014-11-14 09:03
浏览 122
已采纳

使用UploadedFile上传文件类型问题

I am using UploadedFile class in symfony2 to upload image files. I have allowed images with mime type image/jpg and image/png. When I upload a png file from my system to server its mime type is changed automatically. I know its changed because when I fire this command:

file --mime-type /home/mysuer/img.png

it gives me this:

home/myuser/img.png: image/png

but when I upload file and print UploadedFile object it gives following output:

Array
(
    [media] => Symfony\Component\HttpFoundation\File\UploadedFile Object
        (
            [test:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 
            [originalName:Symfony\Component\HttpFoundation\File\UploadedFile:private] => img.png
            [mimeType:Symfony\Component\HttpFoundation\File\UploadedFile:private] => application/octet-stream
            [size:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 1246
            [error:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 0
            [pathName:SplFileInfo:private] => /tmp/phpivgnne
            [fileName:SplFileInfo:private] => phpivgnne
        )

)

Can any one suggest what's wrong?

EDIT

Here is my Code:

public function postFileAction(Request $request)
{
    print_r($request->files->all());//Prints All files


    $image = $request->files->get('media');
    echo $image->getClientMimeType();exit; //Mime Type Information

    if (empty($image)) {
        throw new FileException('Invalid File, Please Select a Valid File.');
    }

    $uploader = $this->get('application.file_uploader');

    if (!$this->container->get('security.context')->isGranted('IS_AUTHENTICATED_FULLY')) {
        //authenticated (NON anonymous users)
        throw new InvalidParameterException('Invalid User, Please use valid credentials');
    }

    return $uploader->upload($image, $this->get('security.context')->getToken()->getUser()->getId());
}

PHPUnit Test Code

public function testPostFile()
{
    $file    = tempnam(sys_get_temp_dir(), 'upl'); // create file
    imagepng(imagecreatetruecolor(10, 10), $file); // create and write image/png to it
    $image   = new UploadedFile($file, 'new_image.png', 'image/png', 10, UPLOAD_ERR_OK);
    $crawler = $this->client->request(
            'POST', '/api/v1/files.json', array('submit' => 'Submit Form'), array('media' => $image)
    );

    $response = $this->client->getResponse();

    $this->assertJsonResponse($response);
    $this->assertContains('filename', $response->getContent());

    print_r($response->getContent());exit;

    $fileToken = json_decode($response->getContent());
    $this->assertNotEmpty($fileToken->filename);

    unlink($file);
    unset($file, $image, $crawler, $response);

    return $fileToken->filename;
}

I am using curl from command prompt to test my REST web service file upload like this:

curl -X POST myapi.com/app_dev.php/api/v1/files.json --form "media=@/home/myuser/img.png"
  • 写回答

1条回答 默认 最新

  • douyi9447 2014-11-14 11:41
    关注

    Finally I got the solutions to my problem will post the answer for problem in both for core-php and for symfony2 also.

    Core PHP(found on http://php.net/manual/en/features.file-upload.php):

    // DO NOT TRUST $_FILES['upfile']['mime'] VALUE !!
    // Check MIME Type by yourself.
    $finfo = new finfo(FILEINFO_MIME_TYPE);
    if (false === $ext = array_search(
        $finfo->file($_FILES['upfile']['tmp_name']),
        array(
            'jpg' => 'image/jpeg',
            'png' => 'image/png',
            'gif' => 'image/gif',
        ),
        true
    )) {
        throw new RuntimeException('Invalid file format.');
    }
    

    So Never trust $_FILES['upfile']['mime'] always use fileinfo and this have given me the clue.

    for Symfony2:

    So I started looking into my code and found that I am using $file->getClientMimeType(), which is not good we have to use $file->getMimeType() which uses fileinfo to fetch mime type and will give the correct mime type.

    In getMimeType() The mime type is guessed using a MimeTypeGuesser instance, which uses finfo(), mime_content_type() and the system binary "file" (in this order), depending on which of those are available.

    Thanks Everyone for the Help :D

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染