【问题标题】:Unable to upload images to subfolders无法将图像上传到子文件夹
【发布时间】:2016-10-03 11:03:59
【问题描述】:

我正在尝试使用 PHP 将图像上传到其各自的文件夹中。 .共有6个文件夹,2个文件夹有子文件夹。我创建了一个下拉列表,其中包含表单中的文件夹和子文件夹列表,并且在选择特定文件夹后,可以将图像上传到所选文件夹中。但是当我选择一个子文件夹时,图像没有上传。

以下是上传的代码。 here is the link 用于下拉菜单(使用 html 的文件夹和子文件夹)。

if(isset($_POST["submit"])) {

$target_dir = "images/website/". $_POST["path"]."/";
}
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = 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) {
    echo '<script language="javascript">';
    echo 'alert("File is an image - " . $check["mime"] . ".")';
    echo '</script>';
    $uploadOk = 1;
} else {
    echo "File is not an image.";
    $uploadOk = 0;
}
}
// Check if file already exists
if (file_exists($target_file)) {
echo ("<SCRIPT LANGUAGE='JavaScript'>
window.alert('Sorry ! Image already exists!')
window.location.href='upload.php';
</SCRIPT>");
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 5000000) {
echo ("<SCRIPT LANGUAGE='JavaScript'>
window.alert('Sorry ! Image file size is too large.')
window.location.href='upload.php';
</SCRIPT>");
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType !=  "jpeg"
&& $imageFileType != "gif" && $imageFileType != "JPG" && $imageFileType !=    "PNG" && $imageFileType != "JPEG" && $imageFileType != "GIF") {
echo ("<SCRIPT LANGUAGE='JavaScript'>
window.alert('Sorry ! only JPG, JPEG, PNG & GIF files are allowed')
window.location.href='upload.php';
</SCRIPT>");
echo ".";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo ("<SCRIPT LANGUAGE='JavaScript'>
window.alert('Sorry ! Image was not uploaded')
window.location.href='upload.php';
</SCRIPT>");
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
    // echo '<script language="javascript">';
    // echo 'alert("Image has been uploaded successfully!")';
    // echo '</script>';
 echo ("<SCRIPT LANGUAGE='JavaScript'>
 window.alert('Congrats ! Image has been successfully uploaded.')
 window.location.href='upload.php';
 </SCRIPT>");

    //header("Location:upload.php") ;
  //  echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has    been uploaded.";
   } else {
    echo ("<SCRIPT LANGUAGE='JavaScript'>
  window.alert('Oops Sorry ! There was some error.')
  window.location.href='upload.php';
  </SCRIPT>");

   // echo "Sorry, there was an error uploading your file.";
  }
  }
  ?>

【问题讨论】:

  • 试试$target_dir = "/images/website/". $_POST["path"]."/";
  • 这个代码我已经用过了
  • @devpro 我不确定是否必须使用条件来使用 2 个不同的路径,一个用于文件夹,另一个用于子文件夹。
  • 分享包含文件夹和子文件夹列表的下拉列表。
  • @Hakan Sonmez 这是 dropwodn codepen.io/anon/pen/BLwXPv的代码笔

标签: php image-uploading


【解决方案1】:

确保您的目录和子目录具有适当的文件权限以在该路径上执行文件上传

确保 .htaccess 文件中没有设置限制子目录中的文件上传的限制选项。

【讨论】:

  • 请阅读代码并提出合适的答案。我只需要一个条件来为不同的选择选择不同的路径。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-05-28
  • 1970-01-01
  • 1970-01-01
  • 2015-06-17
  • 1970-01-01
  • 2014-11-09
  • 2017-04-30
相关资源
最近更新 更多