get name of the remote image

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
vaalsh
Forum Newbie
Posts: 9
Joined: Thu Oct 14, 2010 6:54 am

get name of the remote image

Post 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.
greip
Forum Commoner
Posts: 39
Joined: Tue Aug 23, 2011 8:23 am
Location: Oslo, Norway

Re: get name of the remote image

Post 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? :)
vaalsh
Forum Newbie
Posts: 9
Joined: Thu Oct 14, 2010 6:54 am

Re: get name of the remote image

Post 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.
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: get name of the remote image

Post by twinedev »

What method are you using to grab images / pages from those URLs?

-Greg
vaalsh
Forum Newbie
Posts: 9
Joined: Thu Oct 14, 2010 6:54 am

Re: get name of the remote image

Post 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.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: get name of the remote image

Post 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.
greip
Forum Commoner
Posts: 39
Joined: Tue Aug 23, 2011 8:23 am
Location: Oslo, Norway

Re: get name of the remote image

Post 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?
vaalsh
Forum Newbie
Posts: 9
Joined: Thu Oct 14, 2010 6:54 am

Re: get name of the remote image

Post by vaalsh »

I see file name in save-fle dialog only. I think php script process ID number and send name and file.
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: get name of the remote image

Post 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.
vaalsh
Forum Newbie
Posts: 9
Joined: Thu Oct 14, 2010 6:54 am

Re: get name of the remote image

Post 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.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: get name of the remote image

Post 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.
vaalsh
Forum Newbie
Posts: 9
Joined: Thu Oct 14, 2010 6:54 am

Re: get name of the remote image

Post by vaalsh »

Thanks for this hint!
Can you refere to some example of using CURLOPT_NOBODY option ?

Val.
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: get name of the remote image

Post 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.
vaalsh
Forum Newbie
Posts: 9
Joined: Thu Oct 14, 2010 6:54 am

Re: get name of the remote image

Post by vaalsh »

Thanks a lot!
Post Reply