0

我在 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

不管怎么说,还是要谢谢你

4

1 回答 1

0

下载.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" ;
于 2013-08-23T07:34:27.910 回答