我在HTML文件中包含此代码段。但是我希望如果文件名从1unread.txt打开到1read.txt就会被更改,所以它会进入读取列表。
<h2>Unread</h2>
<?php
foreach (glob("*1unread.txt") as $filename) {
echo "<a href='$filename'>Download {$filename}</a><br>" . "\n" ;
}
?>
<h2>Read</h2>
<?php
foreach (glob("*1read.txt") as $filename) {
echo "<a href='$filename'>Download {$filename}</a><br>" . "\n" ;
}
?>
如果已阅读php.net,我可以用以下内容更改文件名:
bool rename ( string $oldname , string $newname [, resource $context ] )
但是如何在使用下载链接时实现此目的?变量$context
有什么作用?我是PHP的新手,所以不要指望我理解:P
非常感谢
答案 0 :(得分:0)
的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
}
?>
用以下代码替换下载按钮:
echo "<a href='download.php?file=$filename'>Download {$filename}</a><br>" . "\n" ;