文件上传系统,它给上传的文件一个随机的名字



我有一个文件上传系统,当你的文件上传完成后,它会给你文件链接,但我希望它改变文件名,并给你新的文件名,我希望它是,File_RANDOM字母和数字,和随机长度,但不是太长,我的CAPS is 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";
}
?>

使用此函数生成随机字符串

 <?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; 
?>

相关内容

  • 没有找到相关文章