使用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.
我使用php。
我尝试从网址下载图片,我的代码适用于某些网址,而其他网站则无法工作。我想让我的代码适用于所有网址。 或者告诉我导致这种情况的原因。
我的脚本:
p>
$ imgUrl ='http://www.inc.com/uploaded_files/ image / i-love-me_49961.jpg';
$ imageData = file_put_contents('test / xxx.jpg',file_get_contents($ imgUrl));
code> pre>
现在,我可以得到这个图像文件(xxx.jpg),但是当我在ACDSee中打开保存的文件时,我什么也得不到。
,但是,如果我使用
“http:/ /www.wired.com/wp-content/uploads/2014/11/faa-drones-ft-660x330.jpg“ code>,
我的脚本有效。
请帮助我。 p >
div>
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.