重命名文件上传 PHP

Rename File Upload PHP

我正在使用 PHP 的 W3Schools 文件上传示例,我知道它们在编码方面不是最好的,因为它可能已经过时和糟糕等等,但我不需要任何东西太花哨了。但是我想知道如何在上传时更改文件名。

W3Schools 代码(略有编辑):

<?php

$username = $_POST['username'];

$target_dir = '../forum/uploads/'.strtolower($username).'/';
$target_file = $target_dir.'profile'.basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        $uploadOk = 1;
    } else {
        $uploadOk = 0;
    }
}
// Check if file already exists
if (file_exists($target_file)) {
    unlink($target_file);
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) {
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 1) {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo "Profile picture updated";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}
?>

我想将名称更改为个人资料,现在当我使用此代码上传时,它将上传文件作为个人资料后跟文件名,就像这样,profilexxxx.jpeg。

像这样

$target_file = $target_dir.'profile'.basename($_FILES["fileToUpload"]["name"]);

移除

  $target_file = $target_dir.'profile'.strrchr($_FILES["fileToUpload"]["name"],'.');

但你真的应该自己试试。

这个位 strrchr($_FILES["fileToUpload"]["name"],'.'); returns 来自文件名的扩展名。所以如果它是 file.jpg,它就是 returns .jpg,因此 profile.jpg。我应该只提一下 returns 最后一个扩展名。所以如果你有 file.php.jpg 它只有 returns .jpg

更新

我没看到你已经解析了扩展名,所以鉴于此,你可以稍微移动一下。

 $imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
 $target_file = $target_dir.'profile'.'.'.$imageFileType;

不需要重复两次。

Pathinfo - 可能是“正确”的方式。大多数沙箱都关闭了它,所以我有点懒惰 google 它看看它是否保留了 . 就像 jpg.jpg 一样(我总是忘了)。

PS 沙盒是这么说的....

Warning: pathinfo() has been disabled for security reasons in [...][...] on line 7

我用它来上传文件,效果很好。

<?php

if(isset($_POST['submit'])){
  // Retrieve file from post method
  $file = $_FILES['file'];

  // Get file properties
  $fileName = $file['name'];
  $fileTmpName = $file['tmp_name'];
  $fileSize = $file['size'];
  $fileError = $file['error'];
  $fileType = $file['type'];

  //Separate name and file extension
  $fileExt = explode('.', $fileName);
  //Set to always lowercase
  $fileActualExt = strtolower(end($fileExt));

  //Set any extension allowed
  $allowed = array('jpg','jpeg','png');

  //Check whether file extension is allowed
  if(in_array($fileActualExt, $allowed)){
      if($fileError === 0){
          //Check file size criteria
          if($fileSize <= 150000){
              $NewName = "MyNewName"; //define new file name
              //Define your custom file name
              $fileNameNew = $NewName.".".$fileActualExt;
              //Define file destination
              $fileDestination = '../images/'.$fileNameNew;
              //php uploading files
              move_uploaded_file($fileTmpName, $fileDestination);

          } else{
              echo "file is too big";
          }
      } else{
          echo "upload error";
      }
  } else{
      echo "your extension is not allowed";
  }
}
?>