gpt4 book ai didi

php - 使用php上传后如何重命名文件名?

转载 作者:可可西里 更新时间:2023-11-01 12:40:51 27 4
gpt4 key购买 nike

如何在上传文件之前或之后重命名文件?我只想重命名文件名,而不是扩展名。

$changeTXT = $_SESSION['username'];
$uploaderName = strtolower($changeTXT);
$changeTXT = strtolower($changeTXT);
$changeTXT = ucfirst($changeTXT);
$filelocation = $_POST['userfile'];
$filename = $_POST['filename'];
$max_size = $_POST['MAX_FILE_SIZE'];

$file = $_FILES['userfile'];

$allowedExtensions = array("wma", "mp3", "wav");

function isAllowedExtension($fileName) {
global $allowedExtensions;

return in_array(end(explode(".", $fileName)), $allowedExtensions);
}

if($file['error'] == UPLOAD_ERR_OK) {
if(isAllowedExtension($file['name'])) {

$uploaddir = "uploads/".$uploaderName."/";

$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {

echo "Thank you for uploading your music!<br /><br />";

} else {

echo "Your file did not upload.<br /><br />";

}

echo "\n";

echo "<a href='index.php'>Return</a> to index.<br /><br />$uploaddir";

} else {

echo "You have tried to upload an invalid file type.<br /><br />";

}

} else die("Cannot upload");

最佳答案

使用 move_uploaded_file 时,您可以选择文件名,因此您可以选择任何您想要的。

当您上传文件时,它会以临时名称放入临时目录,move_uploaded_file() 允许您移动该文件,并且您还需要设置文件的名称.

关于php - 使用php上传后如何重命名文件名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/701230/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com