为什么我无法上传视频(mp4文件)?

时间:2015-11-26 00:54:19

标签: php cakephp video file-upload cakephp-3.0

我目前正在尝试将文件上传到网络应用程序。下面的代码允许我上传除视频(例如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>';
   }
 }

1 个答案:

答案 0 :(得分:0)

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

<?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;
}