drztpx8735 2015-02-07 20: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-07 21: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.

    展开全部

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

报告相同问题?

悬赏问题

  • ¥15 加热反应炉PLC控制系统设计(相关搜索:梯形图)
  • ¥15 python 用Dorc包报错,我的写法和网上教的是一样的但是它显示无效参数,是什么问题
  • ¥15 经过滑动平均后的一维信号还原用什么结构好呢?
  • ¥15 指定IP电脑的访问设置
  • ¥30 matlab ode45 未发现警告,但是运行出错
  • ¥15 为什么devc++编译项目会失败啊
  • ¥15 vscode platformio
  • ¥15 代写uni代码,app唤醒
  • ¥15 全志t113i启动qt应用程序提示internal error
  • ¥15 ensp可以看看嘛.
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部