我环顾四周,但在这个话题上找不到任何对我有帮助的东西。
我正在为我的网站“ http://uploadifi.com ”使用来自 W3SCHOOLS 的 PHP 上传代码的修改版本,但遇到了一个小问题;很多使用它的人都有 macbook,所以当他们截取屏幕截图时,它会将其命名为“图片 1”等等。但是,为了阻止他们重命名文件,他们是否可以随机命名或添加日期?我知道你可以使用 Time() 但我不记得你是怎么做到的。
任何帮助表示感谢,我的代码如下供您查看:
<?php
//8 Bits= 1 Byte
//1024 Bytes= 1KB
//1024 KB=1 M8
//1024 MB=1GB
//1024 GB=1TB
$allowedExts = array("gif", "jpeg", "jpg", "PNG", "png","rar","zip");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif") // .gif
|| ($_FILES["file"]["type"] == "image/jpeg") // .jpeg
|| ($_FILES["file"]["type"] == "image/jpg") // .jpg
|| ($_FILES["file"]["type"] == "image/jpg") // .jpg
|| ($_FILES["file"]["type"] == "image/PNG") // .PNG
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "application/x-rar-compressed")
|| ($_FILES["file"]["type"] == "application/zip"))
&& ($_FILES["file"]["size"] < 5242880) //Max size 5MB
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
//echo "Type: " . $_FILES["file"]["type"] . "<br>";//Not Needed For Now
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " KB (Do<b> " . ($_FILES["file"] ["size"] / 1024) . " / 1024</b> **CLOSE BUT NOT ACCURATE**)<br>";
if (file_exists("files/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. Please change file name and try again. ";
}
else
{
$sanitizedFilename = str_replace(" ","_",$_FILES["file"]["name"]);
move_uploaded_file($_FILES["file"]["tmp_name"],
"files/" . $sanitizedFilename);
echo "Stored in: " . "<a href=\ffiles/" . $sanitizedFilename . "> ". $_FILES["file"] ["name"] ." </a>";
echo "<br><b>If the above link does not work; please make sure your uploaded<br>file does not have any spaces in the name.<br> IE 'Hello There' make sure it is 'hellothere'.</b>";
}
}
}
else
{
echo "Invalid file";
}
?>