如何在 PHP 中限制文件名 [已关闭]

问题描述 投票:0回答:0

除了限制文件名之外,我对这个文件的所有功能都有限制。

<?php
$target = $_POST["id"];
$target_dir = $target . "/" . "pdf/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$FileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));



// Check if file already exists
if (file_exists($target_file)) {
  echo "Sorry, file already exists.";
  $uploadOk = 0;
}
echo"<br>";



/**Limit File Size*/
if ($_FILES["fileToUpload"]["size"] > 512000) {
  echo "Sorry, your file is too large.";
  $uploadOk = 0;
}
echo"<br>";



/**Limit File Type*/
if($FileType != "jpg" && $FileType != "png" && $FileType != "jpeg"  && $FileType != "pdf") {
  echo "Sorry, only PDF, JPG, JPEG and PNG files are allowed.";
  $uploadOk = 0;
}
echo"<br>";



  
/** Limit File Name */
if ($name != "m1l1") {
  echo "Sorry file name must be (m1l1)";
  $uploadOk = 0;
}
echo "<br>";




//Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
  echo "Sorry, your file was not uploaded.";
  echo "<br>";
// id everything is ok, try to upload file
} else {
  if (move_uploaded_file($_FILES["fileToUpload"] ["tmp_name"], $target_file)) {
    echo "The file ". htmlspecialchars( basename($_FILES["fileToUpload"]["name"])). " has been uploaded."; echo"<br>";
  } else {
      echo "Sorry, there was an error uploading your file.";
    }
  }

因此,在“限制文件名”中,我尝试限制上传的名称,但是,我尝试了许多其他解决方案,但我只是遇到文件被停止上传的问题,即使它的文件名为 m1l1。

php file-upload restriction
© www.soinside.com 2019 - 2024. All rights reserved.