无法将图片上传到子文件夹

时间:2016-10-03 11:03:59

标签: php image-uploading

我正在尝试使用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.";
  }
  }
  ?>

1 个答案:

答案 0 :(得分:0)

确保您的目录和子目录具有正确的文件权限,以便在该路径上执行文件上载

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