I want change a name of downloaded file from web.
For example file with URL:
http://lazynezumi.com/downloads/LazyNezumiPro_Setup.exe
is saved on local disk with its name:
LazyNezumiPro_Setup.exe
But I have extracted from autrors site, that current version of file is: 22.05.23.1042
So I want to have downloaded file name as:
LazyNezumiPro_Setup v22.05.23.1042.exe
How to do it via PHP 7/8? What’s are possibilities?
Currenty I do something similar, I add a SHA-256 to small files. Here are my steps:
-
url of a file on my blog (localhost) is as this:
http://127.0.0.1/d.php?f=http://www.compulsivecode.com/images/JPEGtoPDF.zip
-
a simplified the code of my PHP 7/8 script on localhost Apache web server: p.php is like this:
<?php
set_time_limit(0);
$ip=false;
if(isset($_SERVER['REMOTE_ADDR'])) {
$ip=($_SERVER['REMOTE_ADDR']=='127.0.0.1')?true:false;
}
if(!$ip) exit;
if(isset($_GET['f'])) {
if(filter_var($_GET['f'], FILTER_VALIDATE_URL)) {
$f = htmlspecialchars($_GET["f"]);
}
}
if(empty($f)) exit;
$search = array(" ","&","$",",","!","@","#","^","(",")","+","=","[","]","<",">");
$replace = array("_","and","S","_","","","","","","","","","","","","");
$f = str_replace($search,$replace,$f);
$ctx = hash_init('sha256');
$n = 0;
$h = fopen($f,"rb");
if($h!==false) {
while(!feof($h)) { $buf = fread($h,32768); $n += strlen($buf); hash_update($ctx, $buf); }
fclose($h);
}
$hash = strtoupper(hash_final($ctx));
$path = pathinfo($f);
$filename = $path['filename']. $ver .' ('.$hash.') ['.trim(number_format($n,0,'.',' ')).'].'.$path['extension'];
header("Cache-control: private");
header("Content-type: application/force-download");
header("Content-transfer-encoding: binaryn");
header("Content-disposition: attachment; filename="$filename"");
header("Content-Length: ".$n);
readfile($f);
exit;
?>
-
then downloaded file on local disk has this name:
JPEGtoPDF (3589D32E4B42A3297109549A6461B3B27BE6405126F0725234262DB522641A14) [209 018].zip
But what about large files, it is the way?