0

I just started learning PHP in my internee from school My problem is that I haven't learned PHP at school yet. So in my internee they have been giving me some exercises for me to do, but I got totally stuck in one of them.

The exercise is to create a form that allows the user to upload a file (until here it was okay, the problem comes next). Having this 1st part done I have to find a way to only allow text files with the formats of PHP, Javascript and HTML. And here I got stuck, I tried with if's, I tried with arrays, and nothing works.

Please can anyone help me?

I've tried:

if (($_POST['extension'] != $allowed ) && (empty($_POST['name']))) {
    echo "error message";

and

$file_name = $_FILES['file_name']['name'];
$ext = pathinfo($file_name, PATHINFO_EXTENSION);
if (!in_array($ext,$allowed)) {
    echo "error message";

The second one worked as well as the 1st one. I'm using the $_POST method.

Laurenz Albe
  • 209,280
  • 17
  • 206
  • 263
Marta
  • 23
  • 1
  • 10
  • @Marta Please add that to your question along with a bit more code so we call tell for example what `$allowed` is and it's contents. – cOle2 Sep 16 '19 at 16:15
  • Ifs and arrays should work if implemented correctly. Please post some of your code so we can assist you in diagnosis of this issue. – Dimi Sep 16 '19 at 16:15
  • take a look at this, https://stackoverflow.com/questions/10456113/php-check-file-extension-in-upload-form – Umer Abbas Sep 16 '19 at 16:22
  • the $allowed is an array that contains the file types that are supposed to allow to be uploaded. the code of the array is: $allowed = array('html','js' ,'php'); – Marta Sep 18 '19 at 16:37

1 Answers1

1

Here is Some php function you can apply. You can apply changes as per your need. Thankyou.

        function uploadAnyFile($fileReceived,$target_dir)
          {
            if(!is_dir($target_dir) && !mkdir($target_dir))
            {
                echo("Can Not Create Directory");
                return false;
            }
            $MaxFileSize = 500000;
                //$target_dir = "uploads/";
                //$fileReceived = $_FILES["fileToUpload"];
            $target_file = $target_dir . basename($fileReceived["name"]);
            $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
            $baseName = pathinfo($target_file, PATHINFO_FILENAME);
            $uploadOk = 1;


            // Allow certain file formats
                if($imageFileType != "php" && $imageFileType != "js" && $imageFileType != "html" ) {
                     $uploadOk = 0;
                    echo "Sorry, only js, php & html files are allowed.";

            }
        if ($uploadOk == 0) {
            echo "Sorry, your file was not uploaded.";
            return false;
          // if everything is ok, try to upload file
          } else {
               if (move_uploaded_file($fileReceived["tmp_name"], $nameToUpload)) {
             echo "The file ". basename( $fileReceived["name"]). " has been   uploaded.";
            return $nameToUpload;
        } else {
            echo "Sorry, there was an error uploading your file.";
            return false;
        }
    }
  }
Meet Mevada
  • 111
  • 5