Solved

curl filetime with dynamic images

Posted on 2011-03-06
4
1,021 Views
Last Modified: 2012-05-11
I have a script that uses curl to check the modified time of a file on a remote server.
<?

$curl = curl_init("http://perfectweb.com/experts/curl_time/oh_noes.jpg");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_NOBODY, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FILETIME, true);
$result = curl_exec($curl);

if ($result === false) {
	die (curl_error($curl)); 
}

if (curl_error($curl))  echo curl_error($curl);
		
$timestamp = curl_getinfo($curl, CURLINFO_FILETIME);
if ($timestamp != -1) { //otherwise unknown
	echo date("Y-m-d H:i:s", $timestamp); //etc
}

?>

Open in new window


It works great if I point it to a jpg on my server, as shown above. Now I want to combine it with a dynamic image script that looks like this:
<?

## eventually this script will deliver different images, but one image is hard coded for now
$filename = 'oh_noes.jpg';

header("Content-type: image/jpg");
header("Content-Disposition: attachment; filename=\"".basename($filename)."\"");
readfile("$filename") or display_error('The file could not be found.');
exit;

?>

Open in new window

(I have this script saved with a ".jpg" extension and forced to run as PHP with htaccess.)

The image script works great when called directly, but now I want to combine the two. If I try to use the file time script with the dynamic image script, it doesn't work. How can I make CURLOPT_FILETIME work with a jpg delivered by a PHP script? Is there a way to detect an incoming CURLOPT_FILETIME request and send the right headers to make curl happy?
0
Comment
Question by:merwetta1
  • 2
4 Comments
 
LVL 16

Accepted Solution

by:
sjklein42 earned 450 total points
ID: 35052614
You want to return a header like this, after getting the modified date of the local file:

Last-Modified: Sat, 05 Mar 2011 03:35:19 GMT

Open in new window

0
 
LVL 10

Assisted Solution

by:honestman31
honestman31 earned 50 total points
ID: 35052637
try this  header
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
0
 
LVL 16

Assisted Solution

by:sjklein42
sjklein42 earned 450 total points
ID: 35052817
Also, within the "jpg" server

$_SERVER['REQUEST_METHOD']

Open in new window

can be tested for 'GET' vs 'HEAD' to know whether to return the file contents.
0
 
LVL 6

Author Closing Comment

by:merwetta1
ID: 35054691
I'm now using $_SERVER['REQUEST_METHOD'] to detect CURL FILETIME requests and return the date modified header and exit. It all works. Thank you.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

766 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question