特定文件类型上传不起作用

Posted

技术标签:

【中文标题】特定文件类型上传不起作用【英文标题】:Specific file types upload not working 【发布时间】:2015-01-29 17:26:27 【问题描述】:

我正在创建一个脚本,其中只能上传某些视频类型,但目前卡住了。

这是我的代码 -

<?php

$title = isset($_POST['title']) ? $_POST['title'] : null;
$desc = nl2br(isset($_POST['description'])) ? $_POST['description'] : null;
$name = isset($_POST['fullname']) ? $_POST['fullname'] : null;
$email = isset($_POST['email']) ? $_POST['email'] : null;
$country = isset($_POST['country']) ? $_POST['country'] : null;

$video = isset($_FILES['video']);
$videoname = isset($_FILES['video']['name']);
$videotmp = isset($_FILES['video']['tmp_name']);
$videosize = isset($_FILES['video']['size']);
$videotype = isset($_FILES['video']['type']);
$videoacceptable = array(
        "video/mp4",
        "video/ogg",
        "video/quicktime",
);

$videopath = "/videos/";
$videofile = $videopath . $video;

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

//ERROR MESSAGES / VALIDATION

if(empty($title)) 
    $errors[] = "A title is required"; 
    echo "<style type=\"text/css\"> #title  background-color:#F5A9A9;border:1px solid #DF0101;  </style>";
 elseif(strlen($title) > 80) 
    $errors[] = "Your title can only be 80 characters long"; 
    echo "<style type=\"text/css\"> #title  background-color:#F5A9A9;border:1px solid #DF0101;  </style>";

if(empty($desc)) 
    $errors[] = "A description is required";
    echo "<style type=\"text/css\"> #description  background-color:#F5A9A9;border:1px solid #DF0101;  </style>";

if(empty($name)) 
    $errors[] = "Please enter your full name";
    echo "<style type=\"text/css\"> #fullname  background-color:#F5A9A9;border:1px solid #DF0101;  </style>";
 elseif(strlen($name) > 32) 
    $errors[] = "Your name can only be 32 characters long";
    echo "<style type=\"text/css\"> #fullname  background-color:#F5A9A9;border:1px solid #DF0101;  </style>";

if(empty($email)) 
    $errors[] = "Please enter your email address";
    echo "<style type=\"text/css\"> #email  background-color:#F5A9A9;border:1px solid #DF0101;  </style>";
 elseif(strlen($email) > 50) 
    $errors[] = "Your email addess can only be 50 characters long";
    echo "<style type=\"text/css\"> #email  background-color:#F5A9A9;border:1px solid #DF0101;  </style>";
 elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) 
    $errors[] = "Please enter a valid email address";
    echo "<style type=\"text/css\"> #email  background-color:#F5A9A9;border:1px solid #DF0101;  </style>";

if($videosize = 0) 
    $errors[] = "You forgot to upload a video";
 elseif($videosize >= 20000000) 
    $errors[] = "Your video size is too large, 20mb max";
 elseif(!in_array($videotype, $videoacceptable)) 
    $errors[] = "The file type is not allowed, only allowed .mp4, .ogg and .mov";


if(count($errors) === 0) 
    $connect = mysqli_connect("localhost","username","password");
        if(!$connect) 
            header("Location:"); // ADD ERROR LINK
        
    $dbselect = mysqli_select_db("database");
        if(!$dbselect) 
            header("Location:"); // ADD ERROR LINK
        

    $query = mysqli_query("INSERT INTO cover_videos(title, desc, name, email, country, videotmp, videotype, videosize, videopath) VALUES('$title','$desc','$name','$email','$country','$videotmp','$videotype','$videosize','$videopath')");
    move_uploaded_file($videotmp, $videofile);

    //SEND AN EMAIL TO THE USER     
    $to = $email;
    $subject = "Thank's for your upload";

    $message = '
        <html>
            <head><title>We have received your video</title></head>
            <body>
                <h3>Good News!</h3>
                <p>We have recieved your video and is awaiting approval.</p>
            </body>
        </html>
    ';

    $headers = 'FROM: no-replyk' . "\r\n";
    $headers = 'MIME-Version: 1.0' . "\r\n";
    $headers = 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    $headers = 'X-Mailer: PHP/' . phpversion();

    mail($to, $subject, $message, $headers);

    header("Location:"); //SUCCESSFUL UPLOAD PAGE

    



?>

所以发生的事情是,当我上传文件时,我正在上传 .mov 文件,但弹出的错误消息是“文件类型不允许,仅允许 .mp4、.ogg 和 .mov”,但是.mov mime 在 vi​​deoacceptable 数组中,所以我现在有点卡住了,知道是什么问题吗?

【问题讨论】:

【参考方案1】:
$videoacceptable = array(
    "video/mp4",
    "video/ogg",
    "video/quicktime"
);

请检查这可能有助于解决您的错误。

或者代替“quicktime”试试“mov”。

【讨论】:

刚刚尝试了您的建议,但仍然弹出该错误消息,但感谢您的建议 请在执行代码前尝试打印 $videotype。这将帮助您了解视频类型的确切值。【参考方案2】:
<?php
if(isset($_POST['submit']))

$video = $_FILES['video']; 
$videoname = $_FILES['video']['name'];
$videotmp = $_FILES['video']['tmp_name'];
$videosize = $_FILES['video']['size'];
echo $videotype = $_FILES['video']['type'];
$videoacceptable = array(
    "video/mp4",
    "video/ogg",
    "video/quicktime"
);
$videopath = "/videos/";
$videofile = $videopath . $video;   

?>
<form method='POST' action="" enctype='multipart/form-data'>
<input type='file' name='video'>
<input type='submit' name='submit'>
</form>

请试试这个。希望它会有所帮助。

【讨论】:

以上是关于特定文件类型上传不起作用的主要内容,如果未能解决你的问题,请参考以下文章

上传表单在 Mac OS X 的 Firefox 3 中不起作用?

输入类型='文件'在 Instagram 应用内浏览器中不起作用

删除IE10中的输入类型文件不起作用

多个 HTML 输入类型文件在 IE 中不起作用

上传 UIImage 类型的图像列表(Xamarin)

文件未使用 html 输入类型文件上传