Page 1 of 1
get name of the remote image
Posted: Tue Sep 13, 2011 5:41 am
by vaalsh
Hi,
Is it possible to get name of remote image before loading the image?
URL in form: 'domain.com/some.php?ID=1234'
Thanks,
Val.
Re: get name of the remote image
Posted: Tue Sep 13, 2011 6:01 am
by greip
Could you expand a bit on what you are looking for? You are writing a PHP script, and want to find a way to get the URL of an image on another host which should be shown on the page you are generating from the PHP script? Sorry, I don't quite understand what you are looking for?

Re: get name of the remote image
Posted: Tue Sep 13, 2011 6:16 am
by vaalsh
Thanks for quick replay, greip!
I have list of links to remote site:
'domain.com/some.php?ID=1234'
'domain.com/some.php?ID=1235'
...
But i dont want to download all images (they are big) - only images with spcific names,
so I have to check name of every image before loading.
Val.
Re: get name of the remote image
Posted: Tue Sep 13, 2011 6:36 am
by twinedev
What method are you using to grab images / pages from those URLs?
-Greg
Re: get name of the remote image
Posted: Tue Sep 13, 2011 7:17 am
by vaalsh
I dont use any methods - if in browser(just found it) I put the URL: 'domain.com/some.php?ID=1235' it will show name of the image and ask me where to save the file.
Same with next number 1234, 1233...
Script has to check name of every file before loading few hundreds of them
Val.
Re: get name of the remote image
Posted: Tue Sep 13, 2011 7:47 pm
by califdon
Since you are talking about URLs that are PHP scripts, there is no way to know what that PHP script is going to send to a browser (where you could find the image filename in the source code) unless you request the script to run and send you the results, such as by using cURL. But that would take essentially the same time as just loading them anyway, so I don't think there is any way to do that. If I'm wrong, surely someone will correct me.
Re: get name of the remote image
Posted: Wed Sep 14, 2011 1:52 am
by greip
It sound like you are looking to automate a process you today run manually. When you type the URL manually into the browser, is the image name shown in the save-file dialogue something else than the URL you keyed in? If so, where does the image name come from?
Re: get name of the remote image
Posted: Wed Sep 14, 2011 1:17 pm
by vaalsh
I see file name in save-fle dialog only. I think php script process ID number and send name and file.
Re: get name of the remote image
Posted: Thu Sep 15, 2011 5:29 pm
by Eric!
You could use cURL to fetch the page, follow the redirects and parse the resulting html for IMG links manually with regex or use php DOM tools.
If you provide a link to an example of a page and an image we could help you choose a method. But cURL + DOM is the most robust way to scrape sites.
Re: get name of the remote image
Posted: Fri Sep 16, 2011 8:00 am
by vaalsh
Yes, I know this technick, I just thought that if I go through all links directly, it would be easier.
If save dialog in browser show me name of jpg, should it be somewhere in a header?
Thanks,
Val.
Re: get name of the remote image
Posted: Sat Sep 17, 2011 9:08 am
by McInfo
If the browser generates a save dialog, the server must have sent a Content-Disposition HTTP header. That header is where the browser finds the name of the file. For example,
Code: Select all
Content-Disposition: attachment; filename="image.png"
Ordinarily, the server responds to either a GET or POST request sent by the browser. With those two types of requests, the server replies with a response containing both a head and a body. However, there is another type of request, HEAD, which tells the server to return only the head and not the body. You can use this to get the image file name without downloading the image data.
With cURL, a HEAD request is performed by enabling the CURLOPT_NOBODY option. Also enable CURLOPT_HEADER to get access to the head string.
Re: get name of the remote image
Posted: Sun Sep 18, 2011 12:23 pm
by vaalsh
Thanks for this hint!
Can you refere to some example of using CURLOPT_NOBODY option ?
Val.
Re: get name of the remote image
Posted: Sun Sep 18, 2011 12:59 pm
by Eric!
Second example in the manual
http://php.net/manual/en/book.curl.php
All options are here
http://www.php.net/manual/en/function.curl-setopt.php
FYI cURL does "go through the links directly" so to speak.
Re: get name of the remote image
Posted: Wed Sep 21, 2011 5:16 am
by vaalsh
Thanks a lot!