I have this snippet in a HTML file. But I want that a file name gets changed if its opened from 1unread.txt to 1read.txt so it will get in the list of read.
<h2>Unread</h2>
<?php
foreach (glob("*1unread.txt") as $filename) {
echo "<a href='$filename'>Download {$filename}</a><br>" . "
" ;
}
?>
<h2>Read</h2>
<?php
foreach (glob("*1read.txt") as $filename) {
echo "<a href='$filename'>Download {$filename}</a><br>" . "
" ;
}
?>
If have read on php.net that I can change a file name with this:
bool rename ( string $oldname , string $newname [, resource $context ] )
but how do I make this happen when the download linked is used? And what does the variable $context
do? I am kinda new to PHP so don't expect much from me to understand :P
Thanks anyway
download.php:
<?php
//Only offer file to download which are having "unread" or "read" in their filename.
if(!strstr($_GET['file'], 'unread') || !strstr($_GET['file'], 'read']))
die('Invalid file.');
//Make sure the file exists.
if(!file_exists($_GET['file']))
die('File not found.');
echo file_get_contents($_GET['file']);
if(strstr($_GET['file'], 'unread')) {
//rename 1unread.txt to 1read.txt
$newFileName = str_replace('unread', 'read', $_GET['file']);
rename($_GET['file'], $newFileName);
echo sprintf("RENAMED '%s' TO '%s'!", $_GET['file'], $newFileName); //Debug
} else {
//rename 1read.txt to 1unread.txt
$newFileName = str_replace('read', 'unread', $_GET['file']);
rename($_GET['file'], $newFileName);
echo sprintf("RENAMED '%s' TO '%s'!", $_GET['file'], $newFileName); //Debug
}
?>
Replace the download button with this:
echo "<a href='download.php?file=$filename'>Download {$filename}</a><br>" . "
" ;