上传图片后如何重命名?
随机字母或数字
示例:abcd 或 123456
我认为更改 javascript 中的 html 属性比使用 Sharepoint 后端更容易。这样,当我上传文件时,它会更改文件的名称(而不是数据)
<?php
$currentDirectory = getcwd();
$uploadDirectory = "/uploads/";
$errors = []; // Store errors here
$fileExtensionsAllowed = ['jpeg','jpg','png'];
$fileName = $_FILES['the_file']['name'];
$fileSize = $_FILES['the_file']['size'];
$fileTmpName = $_FILES['the_file']['tmp_name'];
$fileType = $_FILES['the_file']['type'];
$tmp = explode('.', $fileName);
$fileExtension = end($tmp);
$uploadPath = $currentDirectory . $uploadDirectory . basename($fileName);
if (isset($_POST['submit'])) {
if (! in_array($fileExtension,$fileExtensionsAllowed)) {
$errors[] = "This file extension is not allowed. Please upload a JPEG or PNG file";
}
if ($fileSize > 4000000) {
$errors[] = "File exceeds maximum size (4MB)";
}
if (empty($errors)) {
$didUpload = move_uploaded_file($fileTmpName, $uploadPath);
if ($didUpload) {
echo "The file " . basename($fileName) . " has been uploaded";
} else {
echo "An error occurred. Please contact the administrator.";
}
} else {
foreach ($errors as $error) {
echo $error . "These are the errors" . "\n";
}
}
}
?>
非常感谢您!...
move_uploaded_file($fileTmpName, $uploadPath);
的第二个参数是上传文件的目的地,还包括它的文件名。
您只需更改 $uploadPath 变量以包含您想要的随机名称。
例如:
$uploadPath = $currentDirectory . $uploadDirectory . uniqid() . '.' . $fileExtension;