drztpx8735 2015-02-08 04:21
浏览 145
已采纳

使用file_put_contents('test / xxx.jpg',$ url),得到错误的图像

I use php.
I try download a image from a url, my code worked for some url, and others don't work.I wanna my code to work for all url. Or tell me what lead to this.
here is My script:

$imgUrl = 'http://www.inc.com/uploaded_files/image/i-love-me_49961.jpg';
$imageData = file_put_contents('test/xxx.jpg', file_get_contents($imgUrl));

Right now, I can get this image file(xxx.jpg),
but when I open saved file in ACDSee,I get nothing.
however, if I use
"http://www.wired.com/wp-content/uploads/2014/11/faa-drones-ft-660x330.jpg",
My script works.
Please help me.

  • 写回答

1条回答 默认 最新

  • duan41497 2015-02-08 05:22
    关注

    Interesting. This is a case of file_get_contents failing to get the correct image, so to speak, but the best matched SO questions I found would not help you, because they are about different things.

    I shall answer this by laying out how you should solve this type of problems. Problem solving is the simple art of breaking down the problem, and check the smaller pieces one by one until the cause is pinpointed.


    First, did you get anything saved?

    If yes, that means you did get something, and we can exclude all data read write problems including file permissions, network problems, access denials, or lack of curl extension. If you didn't get the saved file, these issues has to be checked one by one.

    In your case, I trust that you did get the file. So the problem is now with the actual data.


    Usually, we first verify that the source is ok. Open it in browser. Save it. Open saved file in ACDSee.

    It works! This is how we confirm the source is working, and ACDSee is working. (And that the OS/browser/network is working, actually.)

    Which leave us the saved data. No programs can open it as jpeg, so we can be pretty certain the saved file is not a jpeg.

    What is it, then?

    If you use a hex editor (e.g. HxD) to open the PHP saved file (not a jpeg) and the manually saved file (confirmed jpeg), you will see that they are simply totally different.

    manually saved image: FF D8 FF E1 ...
    PHP saved image: 1F 8B 08 ...

    If you lookup these first few bytes, called file headers, you will see that the PHP saved file is a gzip file. To confirm this, you can rename the file's extension to .gz. Unzip it, and viola there is the image!

    Note: hex comparison is pretty useful in sorting out the occasional weird problems, such as unwanted bom markers, line break conversion on binary files, or messy server filters. So hex editors are indispensable for a good programmer, even a web programmer.


    At this stage, the question becomes, why did I get a gzipped file? A web programmer should know what is wrong by now, but let's pretend we do not.

    There is not much problem space left. It is either file_put_contents or file_get_contents.

    If you do a little PHP coding to get in between them, you will see that file_put_contents is returning the gzipped data. But where did file_put_contents get its data? From the network, of course!

    Now, let me introduce your a software called Wireshark. These software are called packet sniffers, and they can show you the raw data going through your network cable or wifi.

    Note: Packet sniffers are not easy. You need to know network protocols really well to make sense of anything. They belongs to a class of low level debuggers called system monitors, and are often the last resort. But this final hand is one of the distinctions between an average programmer and an expert.)


    Indeed, with a packet sniffer, we can confirm that the server is responding with gzip encoded content, using Content-Encoding: gzip.

    And so, we now know that the real cause is file_get_contents does not automatically decompress gzip content. With this correct question, stackoverflow already has answers.


    This is how we approach pretty much every programming problems, and why we answer more than we ask.

    Hope you enjoyed the journey, and hope you will become the tour guide one day.

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

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料