Sandesh
Sandesh

Reputation: 1222

Uploading Video in php not working

I want to upload video in php this is my code

<?php

if(isset($_POST['submit']))
{

    $allowedExts = array("jpg", "jpeg", "gif", "png", "mp3", "mp4", "wma");
    $extension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);

    if ((($_FILES["file"]["type"] == "video/mp4")
    || ($_FILES["file"]["type"] == "audio/mp3")
    || ($_FILES["file"]["type"] == "audio/wma")
    || ($_FILES["file"]["type"] == "image/pjpeg")
    || ($_FILES["file"]["type"] == "image/gif")
    || ($_FILES["file"]["type"] == "image/jpeg"))

    && ($_FILES["file"]["size"] < 20000)
    && in_array($extension, $allowedExts))

      {
      if ($_FILES["file"]["error"] > 0)
        {
        echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
        }
      else
        {
        echo "Upload: " . $_FILES["file"]["name"] . "<br />";
        echo "Type: " . $_FILES["file"]["type"] . "<br />";
        echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
        echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

        if (file_exists("store/" . $_FILES["file"]["name"]))
          {
          echo $_FILES["file"]["name"] . " already exists. ";
          }
        else
          {
          move_uploaded_file($_FILES["file"]["tmp_name"],
          "store/" . $_FILES["file"]["name"]);
          echo "Stored in: " . "store/" . $_FILES["file"]["name"];
          }
        }
      }

      else
      {
        echo "Invalid file";
      }
}     
?>

HTML CODE is:

<!DOCTYPE html>

<head>
<title></title>
</head>

<body>

<form action="upload.php" method="post" enctype="multipart/form-data">
<label for="file"><span>Filename:</span></label>
<input type="file" name="file" id="file" /> 
<br />
<input type="submit" name="submit" value="Submit" />
</form>

</body>
</html>

when I upload .mp4 file it showing the message : Invalid file

Give me solution plz

Upvotes: 1

Views: 3155

Answers (3)

harry
harry

Reputation: 1007

Change it:
($_FILES["file"]["size"] < 20000)

To:
($_FILES["file"]["size"] < 2000000)

Upvotes: 6

Pupil
Pupil

Reputation: 23948

Because your code:

$extension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);

is returning only extension after dot. e.g. mp4, mp3

And you expect it to be:

video/mp4 OR audio/mp3

I think you need mime type of the file.

mime_content_type($_FILES["file"]["name"]);

Solution:

Change

$extension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);

To

$extension = mime_content_type($_FILES["file"]["name"]);

EDIT:

Change:

if ((($_FILES["file"]["type"] == "video/mp4")
    || ($_FILES["file"]["type"] == "audio/mp3")
    || ($_FILES["file"]["type"] == "audio/wma")
    || ($_FILES["file"]["type"] == "image/pjpeg")
    || ($_FILES["file"]["type"] == "image/gif")
    || ($_FILES["file"]["type"] == "image/jpeg"))

To:

if (in_array($extension, $allowedExts))

Upvotes: 4

Sandesh
Sandesh

Reputation: 1222

This is my final code that is working I have change my php code like this

<?php


$target_dir = "store/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = 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"] > 500000000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "mp3" && $imageFileType != "m4v" ) {
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "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.";
    }
}

?>

With this I can upload mp3 and Mp4 files. I have change the validation for file formate and size of file in the code...

Upvotes: 2

Related Questions