MartinRA Posted June 23, 2013 Share Posted June 23, 2013 I'm trying to download an image from a web page to save remotely.The url of the image is http://vintage-british-diecasts.lefo...7/P3270552.jpg(I'm trying to help a friend archive his forum, so am not breaching copyright).Problem is that when I use either PHP to download the image by using file_get_contents or by using CURL, the file that I get is not the .jpg file, but the HTML source code of the page it is sitting on. Could it be that they have configured the .htaccess to read jpg pages as html?I've tried several different methods in PHP and CURL but the result is always the same. The file contents I end up with is: <html><head></head><body><img src="http://vintage-british-diecasts.lefora.com/composition/attachment/6e0d8d1af650b33daea499e0757725d5/507827/P3270552.jpg"></body></html> Can anyone help me to get to the actual image? Quote Link to comment Share on other sites More sharing options...
ginerjm Posted June 23, 2013 Share Posted June 23, 2013 AFAIK, you cannot "download" a file using php or js either. Think of the possibilities Malicious scripts placing files on your local pc? I could be wrong, but I believe I've read of this being taboo. Quote Link to comment Share on other sites More sharing options...
MartinRA Posted June 23, 2013 Author Share Posted June 23, 2013 Maybe download is the wrong word. With file_get_contents() you can normally grab the contents of a file and then save it remotely. For some reason that doesn't work in the above example. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted June 23, 2013 Share Posted June 23, 2013 Download usually means "from the server, to the client", or in today's parlance, "from the cloud to your desk". Upload is therefore: from earth/desk to cloud/server. You want to upload a file? File_get_contents simply reads a file that is already on the server. No uploading at all. You want to do some googling on 'html uploading and php'. Your html will have a type='file' input tag wherein the user chooses the file to be uploaded and then your php will move it to a permanent location and assign a name to it and THEN you can use file_get_contents to read it. And none of this is remote since by the time php begins executing the file already is on the server, thus local. Here's a good example of what you need to code up: http://www.tizag.com/phpT/fileupload.php Quote Link to comment Share on other sites More sharing options...
ginerjm Posted June 23, 2013 Share Posted June 23, 2013 www.tizag.com/phpT/fileupload.php Corrected url - seems to have been condensed in the previous post. Quote Link to comment Share on other sites More sharing options...
MartinRA Posted June 23, 2013 Author Share Posted June 23, 2013 I appreciate your taking the time to answer, but you don't seem to be reading the question. I perfectly understand the difference between uploading and downloading. My problem as explained above is that I can't seem to grab an image from a web page using the normal PHP method (or the normal CURL method come to that). All I get is HTML code, which I wouldn't expect from a .jpg file. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted June 23, 2013 Share Posted June 23, 2013 Maybe I don't understand how you do this either. Can you post the php code that you are using just to grab the jpg from the web page? I'd like to see how it can be done. Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 23, 2013 Share Posted June 23, 2013 This is possible. No sure what ginerjm is referring to. Please show the code you are using There is no reason the output should be an HTML page - unless you are sending it that way by accident. Quote Link to comment Share on other sites More sharing options...
DavidAM Posted June 23, 2013 Share Posted June 23, 2013 @ginerjm -- PHP is not strictly for serving web pages. It is a scripting engine and can be used for various other purposes. I often write scripts on my Linux box to manage certain aspects of that system. These scripts are run from the command-line (without a web-server). The term "download" generally means to retrieve a file from a remote system. The term "upload" generally means to send a file to a remote system. In both cases, the remote system is "acting" as a "server" and the "local" system is acting as a "client". @OP -- As I understand it, you are trying to retrieve ("download") a file from a remote server. This can be done, using either curl or file_get_contents. The file will be retrieved to the system where PHP is running. You can not then directly store it on a different system without some other "magic" (i.e. another script) going on. Ask your "friend" what precautions are in place to prevent direct downloads of images from this site. I tried requesting the file with wget, and received a "403: Forbidden" error from the server. It didn't take long to figure out how to get past that; they do, after all, have to be available for direct retrieval; but I would think someone with a legitimate reason to retrieve them would know what obstacles have been put in place. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted June 23, 2013 Share Posted June 23, 2013 My mistake for assuming (!) that the OP was running his php on a server. Quote Link to comment Share on other sites More sharing options...
Irate Posted June 23, 2013 Share Posted June 23, 2013 (edited) Did you ever try to view the source code an image generates? Or, even, what the image consists of? You can get the contents of the file, yes. If you want only the image link, use some RegExp magic. <?php $fc = file_get_contents("http://vintage-british-diecasts.lefora.com/composition/attachment/6e0d8d1af650b33daea499e0757725d5/507827/P3270552.jpg"); $regex = "/<(?!http:\/\/vintage-british-diecasts.lefora.com\/composition/attachment/6e0d8d1af650b33daea499e0757725d5/507827/P3270552.jpg)(.*)?>/"; preg_replace($regex, "", $fc); ?> Edit: Yeah, the RegExp was written sloppily, but it serves the purpose. Edited June 23, 2013 by Irate Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.