Hello.

Ive got this small script which caches a web page. It names the file with a unique ID which is all fine but I need it to name it with the unique ID but with a HTML extention.

How would I do this?

Heres my script...

<?php
$page = file_get_contents('http://www.google.com');

$num = md5(uniqid());

$filename = $num;

file_put_contents($filename,$page);

?>

Recommended Answers

All 4 Replies

<?php
$page = file_get_contents('http://www.google.com');
$num = md5(uniqid());
$filename = $num;
$filename .=".html";
$handle=fopen($filename,"x+");
fwrite($handle,$page);
?>
commented: Thanks for your help - !Unreal +2

Thank you. I managed to solve the problem but you did pretty much the same thing.

Thanks for the reply :)

Why not replace the random meaningless filename of

$num = md5(uniqid());

with a unix timestamp which will record the files in chronological order and still be unique:

$num = date("Ymd his");
$filename = $num." ".md5(uniqid(rand(), true)).".html";

?
This way you would know exactly when each file was written.

Seems like a better idea. Atleast I wont have any risk of getting two identical file names.

Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.