user3915307
user3915307

Reputation: 31

Check File is zip type before upload

I want people to be able to upload zip files to my server. I have a form for them to upload to and it redirects to an upload page. I can successfully upload pictures (png and jpg) but whenever I try a zip I canot upload it, its not show me any error, Is there a way to accept the zip files?

<?php
function uploadfile()
{
    $allowedExts = array("zip", "rar");
    $split = explode(".",$_FILES["filework"]["name"]);    
    $type = strtolower($split[sizeof($split)-1]); 
    $rname = time().".".$type;

    if (($_FILES["filework"]["type"] == "application/zip") || ($_FILES["filework"]["type"] == "application/x-zip") || ($_FILES["filework"]["type"] == "application/x-zip-compressed") && ($_FILES["filework"]["size"] < 20000000) && in_array($split, $allowedExts)) {
        $destination = "uploads/".$rname;
        $temp_file = $_FILES['filework']['tmp_name'];
        move_uploaded_file($temp_file,$destination);
        return $rname;
    } else {
        return $_FILES["filework"]["error"];}
    }
}
?>

Upvotes: 3

Views: 1847

Answers (1)

19greg96
19greg96

Reputation: 2591

try

in_array($type, $allowedExts)

also, nothing will be returned if your if fails and no actual error is generated, see php documentation

And as DanFromGermany said the process of upload is as follows:

  1. user sends the form with the selected file
  2. server loads the file to a temp folder (that's why you need $temp_file = $_FILES['filework']['tmp_name'];)
  3. server executes php script to render the action page of the form

Because your php is executed last, it cannot check for file extension prior to upload, however you can just ignore the temp file if it doesn't meet criteria.

Upvotes: 1

Related Questions