为什么可以';t我上传了一个视频(mp4文件)


Why can't I upload a video (mp4 file)?

我目前正在尝试将文件上传到web应用程序。它下面的代码允许我上传除了视频(例如mp4)文件之外的任何文件。这是使用CakePHP 3.0.11

if (isset($_FILES['uploads']) === true) {
   //put the data into a var
   $file = $_FILES['uploads'];
   for ($i = 0; $i < count($file['name']); $i++) {
        $name = $file['name'][$i];
        $tmp_name = $file['tmp_name'] [$i];
        // upload file and move to img/uploads/itemsImages
        move_uploaded_file($tmp_name, WWW_ROOT . 'img/modulevideos/' . $name);
       // prepare the filename for database entry
       $moduleContent['content'] = 'modulevideos/' . $name ;

       echo $name, '<br>';
       echo $tmp_name, '<br>';
       echo $moduleContent['content'], '<br>';
   }
 }

您可以使用此脚本上载任何文件。

<?php
$target_dir = "/img/uploads/itemsImages";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if (isset($_FILES['uploads'])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
    echo "File is an valid file - " . $check["mime"] . ".";
    $uploadOk = 1;
} else { 
    $uploadOk = 1;
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$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.";
}
}
}
?>

如果您只想允许文件,请在检查uploadOK之前将其包括在内。

// Allow certain file formats
if($imageFileType != "mp4") {
echo "Sorry file type now allowed";
$uploadOk = 0;
}