Jump to content

Problem downloading an image


Recommended Posts

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?


Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

@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.

Link to comment
Share on other sites

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.

$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 by Irate
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.