I have tried different metods for file upload system. None of them worked so far, I know that there are lots of similar questions, I checked and tried nearly all of them yet they did not work.
It gives the last echo if I try to upload files. If the files is more than the max size than it gives the wrong echo "Sorry, this filetype is not allowed"
Here is my php code
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$FileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($FileType == "exe" && $FileType == "dll" && $FileType == "zip" ) {
echo "Sorry, this filetype is not allowed";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo " <br>Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
Here is the HTML Code
<html>
<head> <link rel="stylesheet" href="table.css"></head>
<title>Dosya Yükleme</title>
<body>
<form action="fileupload.php" method="post" enctype="multipart/form-data">
Bir dosya seçiniz:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload" name="Yükle">
</form>
</body>
</html>
I simplified code, putting both php and html in same php file and erasing the checking process.
New PHP file which still doesn't work
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$FileType = pathinfo($target_file,PATHINFO_EXTENSION);
if (isset($_POST['upload'])) {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
<html>
<head> <link rel="stylesheet" href="table.css"></head>
<title>Dosya Yükleme</title>
<body>
<form action="fileupload.php" method="post" enctype="multipart/form-data">
Bir dosya seçiniz:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload" name="upload">
</form>
</body>
</html>
Solution chmod -R 777