Otávio Barreto
Otávio Barreto

Reputation: 1558

PHP validate upload and convert files based on file type

I have to following code to upload files with php:

<!DOCTYPE html>
<html>
<head>
  <title>Upload your files</title>
</head>
<body>
  <form enctype="multipart/form-data" action="upload.php" method="POST">
    <p>Upload your file</p>
    <input type="file" name="uploaded_file"></input><br />
    <input type="submit" value="Upload"></input>
  </form>
</body>
</html>
<?PHP
  if(!empty($_FILES['uploaded_file']))
  {
    $path = "uploads/";
    $path = $path . basename( $_FILES['uploaded_file']['name']);

    if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $path)) {
      echo "The file ".  basename( $_FILES['uploaded_file']['name']). 
      " has been uploaded";
    } else{
        echo "There was an error uploading the file, please try again!";
    }
  }
?>

Its working, but fine my question is how to add/implement a secure validation based on file type and also convert files?

All images files should be converted to jpg type and all videos files should be converted to mp4 type.

In the upload process I also should add randon() name to the file.

IMPORTANT: after validation, all images should go to upload/images/ and all videos to upload/videos/

Upvotes: 1

Views: 328

Answers (2)

LF-DevJourney
LF-DevJourney

Reputation: 28529

You can use $_FILES["file"]["type"] to get the upload file type, but this may not accurate. You can use below code to get the file type.

$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime_type = finfo_file($finfo, $temp_name);
finfo_close($finfo);

Then you can check if $mime_type is in something like,

'image/jpeg' => FILE_TYPE_JPG,
'image/jpg' => FILE_TYPE_JPG,
'image/png' => FILE_TYPE_PNG,
'image/gif' => FILE_TYPE_GIF,
'video/mp4' => FILE_TYPE_MP4,
'video/webm' => FILE_TYPE_WEBM,

From the mimetype of the uploaded file,you can filter you wanted images and videos and drop unwanted files. Then move image and video to different directory.

To transfer video to mp4 you can use ffmpeg -i example.mov example.mp4

To transfer image to jpg you can use convert 0000.png 0000.jpg

For a random name you can use md5_file($file_name) as file name.

Note, there may be more parameters for ffmpeg and imagemagick convert to adjust when you convert your videos and images.

Upvotes: 1

user11070192
user11070192

Reputation:

You will have to write a switch statement that determents the original file type, and then use multiple libraries to convert the original file type to the preferred file type.

switch ($originalfiletype){
case '.png':
//Process image through a .png to .jpg library
break;
case '.pdf':
//Process image through a .pdf to .jpg library
break;
case '.webm':
//Process video through .webm to .mp4 library
break;
default:
echo "Your file type is not supported";
break;
}

This process is very inconvenient and tedious, but unfortunately, there is no other way. PHP simply doesn't have a built-in library that converts every file type.

Upvotes: 1

Related Questions