Solved

curl filetime with dynamic images

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

733 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