1

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

  • I found them all nothing seems to be different but solutions did not work on it. If you found an aswer that can really solve it please send me the link. – fahri önder Dec 13 '15 at 20:33
  • The dublicate tag was unnecessary since the solution was unrelated. I found the solution on ubuntu forum. The problem was file permission. Once there was a post about file permission but the code was wrong. On that topic the terminal code was chmod 0751 -R folder but the working code is chmod -R 777 folder – fahri önder Dec 13 '15 at 21:39
  • 1
    Please post your solution as an answer, instead of editing it into the question. Thanks! – Rob Dec 13 '15 at 23:08

3 Answers3

1

Change && to || in the type conditional branch:

if($FileType == "exe" || $FileType == "dll" || $FileType == "zip"  ) {
diafol
  • 58
  • 10
  • Thanks but it still did not work.. at least we got rid of the second error I guess. But more importantly it still doesn't upload the file – fahri önder Dec 13 '15 at 19:39
  • Do debugging: Add `var_dump($variable)` in your code for every variable that is being used, and compare the contents of the variable with your assumption. – Sven Dec 13 '15 at 20:23
  • The dublicate tag was unnecessary since the solution was unrelated. I found the solution on ubuntu forum. The problem was file permission. Once there was a post about file permission but the code was wrong. On that topic the terminal code was chmod 0751 -R folder but the working code is chmod -R 777 folder – fahri önder Dec 13 '15 at 21:38
1

I found the solution on ubuntu forum. The problem was file permission. Once there was a post about file permission but the code was wrong. On that topic the terminal code was chmod 0751 -R folder but the working code is chmod -R 777 folder

  • Closed as not reproducible to the extent that others will not have the same setup. That said, it is useful to know. – Drew Dec 22 '15 at 21:18
0

I think an issue is with the submit button and trying to check its value with if (isset($_POST['upload'])) {. It seems that the name of the button needs to be submit.

Have a look at Send value of submit button when form gets posted.

Community
  • 1
  • 1
Scotty Waggoner
  • 3,083
  • 2
  • 26
  • 40