您好我想通过添加3个随机字母来更改文件名。
$target_dir = "/var/www/html/uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$kiekis = htmlspecialchars($_POST['laikotarpis']);
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
$rpavadinimas = htmlspecialchars($_POST['rpavadinimas']);
$datanuo = $datanuo = date("Y-m-d");
$time = strtotime($datanuo);
$dataiki = date("Y-m-d", strtotime("+$kiekis month", $time));
$Failo_vardas = htmlspecialchars(basename($_FILES["fileToUpload"]["name"]));
if ($imageFileType != "mp4" && $imageFileType != "avi" && $imageFileType != "mov" && $imageFileType != "3gp" && $imageFileType != "mpeg"){
$error_message = "<span style='color:red';><b>Failo ". basename( $_FILES["fileToUpload"]["name"]). " Netinkamas failo formatas</b></span>";
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)){
echo "success";
}
}
作为示例,我想上传文件“test.mp4”,但我想将其另存为“testA2Q.mp4” 任何解决方案如何解决?
编辑:我想用更改的名称保存它
答案 0 :(得分:0)
你可以做这样的事情
$info = pathinfo($_FILES['userFile']['name']);
$ext = $info['extension']; // get the extension of the file
$newname = "newname.".rand(0,999).$ext;
$target = 'images/'.$newname;
move_uploaded_file( $_FILES['userFile']['tmp_name'], $target);
下面
$newname = "newname.".rand(0,999).$ext;
rand(0,999)
将使用0-999之间的三个随机数命名。
我会帮助你。
答案 1 :(得分:-1)
试试这种方式
$target_dir = "/var/www/html/uploads/";
$old_file = 'test.mp4'; //sample
$new_name = 'testA2Q'; //sample
$type = substr(strrchr($old_file, "."), 1);
$new_file_name = $new_name.'.'.$type;
rename ($target_dir.$old_file, $target_dir.$new_file_name); //rename a file