好的,所以我有一个文件上传系统,当你的文件上传完成后它会给你你的文件链接,但是我希望它改变文件名,并给你新的文件名,我希望它是这样的, File_RANDOM字母和数字,以及随机长度但不长,我的大写是STUCK.mp4我是怎么做的?
<?php
$fileName = $_FILES["file1"]["name"];
$fileTmpLoc = $_FILES["file1"]["tmp_name"];
$fileType = $_FILES["file1"]["type"];
$fileSize = $_FILES["file1"]["size"];
$fileErrorMsg = $_FILES["file1"]["error"];
if (!$fileTmpLoc) {
echo "ERROR: Please browse for a file before clicking the upload button.";
exit();
}
if(file_exists('uploads/' . $_FILES['file1']['name'])){
die('File with that name already exists.');
}
if(move_uploaded_file($fileTmpLoc, "uploads/$fileName")){
echo "Your File Link<input value='http://testserver1234.no-ip.org/Upload-System/uploads/". basename( $_FILES['file1']['name']).
"' style='width: 100%'>";
} else {
echo "move_uploaded_file function failed";
}
?>
答案 0 :(得分:1)
使用此函数生成随机字符串。
<?php
function generateRandomString($length) {
$key = '';
$keys = array_merge(range(0, 9), range('a', 'z'));
for ($i = 0; $i < $length; $i++) {
$key .= $keys[array_rand($keys)];
}
return $key;
}
$fileName = $_FILES["file1"]["name"];
$fileRandomString=generateRandomString(50); // Pass Length of the String into function
$newFileName=$fileName."-".$fileRandomString;
?>