上传时试图重命名文件,但无法正常工作


Trying to rename a file when uploading and it wont work

嘿,我正试图在上传时更改文件名(如果存在),但似乎有问题。这是代码。。

$tempfilename = $filename["name"]; 
while(file_exists($location . $tempfilename))
{
    $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; 
    $shuffled = str_shuffle($chars);
    $stringa = substr($shuffled,0,7);
    move_uploaded_file($filename["tmp_name"], $location . $filename["name"]);
    $tempfilename = $stringA.$filename["name"];
}
move_uploaded_file($filename["tmp_name"], $location.$stringa.$filename["name"]);
clearstatcache();

这似乎不起作用,

不知道我错过了什么。

PHP变量名区分大小写。

$stringA !== $stringa

然而,与其说你在搅乱琴弦等,你有没有考虑过只使用计数器?

$tempfilename = $filename["name"];
for ($i = 0; file_exists($location . $tempfilename); $i++) {
    $tempfilename = $i . $filename["name"];
}
move_uploaded_file($filename["tmp_name"], $location . $tempfilename);