代码之家  ›  专栏  ›  技术社区  ›  TheWelshManc

重命名文件上载PHP

  •  -2
  • TheWelshManc  · 技术社区  · 8 年前

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

    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.";
        }
    }
    ?>
    

    我想将名称改为profile,现在当我使用此代码上载时,它会将文件上载为profile,后跟文件名,如profilexxxx。jpeg。

    2 回复  |  直到 8 年前
        1
  •  1
  •   Community Mohan Dere    5 年前

    这样地

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

    去除

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

    但你真的应该试试自己。

    这个钻头 strrchr($_FILES["fileToUpload"]["name"],'.'); 返回文件名的扩展名。所以如果是 file.jpg 它返回 .jpg ,因此 profile.jpg . 我应该提到,它只返回最后一个分机。所以如果你有 file.php.jpg 它只返回 .jpg公司

    更新

    我没有看到您已经解析出了扩展,所以考虑到这一点,您可以稍微移动一下。

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

    不需要做两次。

    Pathinfo-可能是“正确的”方式。只是大多数沙盒都关闭了,所以我有点懒得用谷歌搜索它,看看它是否保留了 . 如中所示 jpg vs公司 .jpg公司 (我总是忘记)。

    PS这就是沙盒所说的。。。。

    警告 :由于安全原因,已在中禁用pathinfo() [...][...] 在线 7.

        2
  •  0
  •   Achmad Yogi Prakoso    8 年前

    我用这个上传文件,效果很好。

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