Solved

curl filetime with dynamic images

Posted on 2011-03-06
4
1,008 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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.  …
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now