我有一个上传PHP脚本,在上传文件之前,它会将其重命名为随机名称,因此没有已经存在的文件将被覆盖。问题是它无法正常工作。它在文件名中放入一堆奇怪的符号,当我尝试查看该文件时,它有404错误。在我添加这个随机名称部分之前它正在工作。这是我现在拥有的PHP脚本:
<?php
// Where the file is going to be placed
$target_path = "files/";
/* Rename file with random name and keep extension */
$length = 10;
$characters = ’123456789’;
$string="";
for($p = 0; $p < $length; $p++) {
$string .= $characters[mt_rand(0, strlen($characters))];
}
$pos = strrpos(basename( $_FILES['uploadedfile']['name']), ".");
$ext = str_split(basename( $_FILES['uploadedfile']['name']), $pos);
$target_path = $target_path.$string.$ext[1];
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "<h2>Your file has been uploaded!</h2><br /><p>Your file has been uploaded successfully. <a href='".$target_path."'>Click here</a> to see the file you uploaded.
<br /><br /><br />
<h2>A link to your file</h2><br />The full link to your file is:
<br /><br />
<code>http://www.example.com/upload/".$target_path."</code></p>";
} else{
echo "<span class='error'>There was an error uploading the file, please try again.</span>";
}
?>
出于某种原因,它真的很奇怪。然后,当我在我的托管文件管理器中查看文件夹/ files /时,我尝试删除其中包含奇怪符号的文件,然后我刷新它们都回来了。看一下这个截图:
很奇怪,是吗?
我可能需要一个新的随机命名PHP脚本,但这很烦人,因为我删除了所有这些文件,而且,你知道,当我重新加载它们时会回来!这就像PHP强迫他们不删除或其他东西。
提前感谢您的帮助,
森