0
我正嘗試使用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
我已經使用此代碼 – HebleV
@devpro我不確定是否必須使用條件才能使用2個不同路徑,一個用於文件夾,另一個用於子文件夾。 – HebleV