Vikrant
Vikrant

Reputation: 444

How to upload mp3, mp4, 3gp files using php?

I'm making a online storage and want user to upload file with category,I have successfully done with images,but I'm not able to perform it with audio & video formats ,any help?

<?php 

$allowedExts = array("gif", "jpeg", "jpg", "png"); 
$extension = end(explode(".", $_FILES["file"]["name"]));
if((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/  jpg") || 
    ($_FILES["file"]["type"] == "image/pjpeg") || ($_FILES["file"]["type"] == "image/x-png") || ($_FILES["file"]["type"] == "image/png"))
    && ($_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("upload/" . $_FILES["file"]["name"])) 
                { 
                    echo $_FILES["file"]["name"] . " already exists. "; 
                } else {  
                        move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]);
                        echo "Stored in: " . "upload/" . $_FILES["file"]["name"]; 
                        } 
                }
} else { 
    echo "Invalid file"; 
}

?>

Upvotes: 1

Views: 8137

Answers (3)

Jesika
Jesika

Reputation: 143

for my opinion, change php.ini file like.. 'upload_max_filesize= 500M' , 'post_max_size=500M', 'memory_limit=250M', 'max_input_time=45000' and 'max_execution_time=30000'.

I edited this php.ini file, and i can able to upload audio and video file.

Upvotes: 1

Xeoncross
Xeoncross

Reputation: 57184

Rather than checking the extension .[a-z] of the file (which can be changed) - why not check the file itself? (even $_FILES["file"]["type"] can lie)

function get_mime($file) {
  if (function_exists("finfo_file")) {
    $finfo = finfo_open(FILEINFO_MIME_TYPE); // return mime type ala mimetype extension
    $mime = finfo_file($finfo, $file);
    finfo_close($finfo);
    return $mime;
  } else if (function_exists("mime_content_type")) {
    return mime_content_type($file);
  } else if (!stristr(ini_get("disable_functions"), "shell_exec")) {
    // https://stackoverflow.com/a/134930/1593459
    $file = escapeshellarg($file);
    $mime = shell_exec("file -bi " . $file);
    return $mime;
  } else {
    return false;
  }
}

https://stackoverflow.com/a/12191939/99923

Upvotes: 2

user149341
user149341

Reputation:

Your code is already broken. $_FILES[$file]["type"] is meaningless. It is supplied by the browser uploading the file, not by the server, and as such is frequently missing or wrong. It should be ignored.

Changing the initial condition to simply:

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

will fix that. You will need to increase the size limit (currently 20000 = 20 KB), though, as that will obviously be too little for a video file. :)

Upvotes: 3

Related Questions