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

前端之家收集整理的这篇文章主要介绍了使用php上传后如何重命名文件名?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何在上传文件之前或之后重命名文件?我只想重命名文件名,而不是扩展名.
$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相关文章