簡體   English   中英

使用php將mp4文件上傳到數據庫

[英]Uploading mp4 file to the database by using php

我正在實施由 php、mysql、smarty 組成的網站,並且面臨無法在注冊屏幕上將 mp4 文件上傳到數據庫的問題,我可以使用 jpg 文件上傳該文件。

當我單擊那里的上傳按鈕時,它會在屏幕左下角顯示上傳進度的百分比高達 99% 左右,然后最后顯示“文件格式未知”,這是來自客戶端下面的 php 編碼背面的白屏,它沒有在服務器上顯示任何錯誤消息。

我的問題是,您是否可以從下面的編碼中看出 mp4 的案例“1”有問題,而案例“2”的 jpg 工作正常。 還是與上傳功能相關的其他一些編碼可能導致此問題? 或者,如果我可以在 appache 服務器上安裝 ffmpeg,它會解決那些問題嗎? 如果你能幫助我,我將不勝感激。

代碼:

function Main($path, $width, $height, $dst_file, $header = false) {

    if(!isset($path)) {
        return array(0, "Image path is not set.");
    }

    if(!file_exists($path)) {
        return array(0, "The file can not be found in the specified path.");
    }

    // Set the size of the image
    if($width) $this->imgMaxWidth = $width;
    if($height) $this->imgMaxHeight = $height;

    $size = @GetimageSize($path);
    $re_size = $size;

    //Aspect ratio fixed processing
    if($this->imgMaxWidth != 0) {
        $tmp_w = $size[0] / $this->imgMaxWidth;
    }

    if($this->imgMaxHeight != 0) {
        $tmp_h = $size[1] / $this->imgMaxHeight;
    }

    if($tmp_w > 1 || $tmp_h > 1) {
        if($this->imgMaxHeight == 0) {
            if($tmp_w > 1) {
                $re_size[0] = $this->imgMaxWidth;
                $re_size[1] = $size[1] * $this->imgMaxWidth / $size[0];
            }
        } else {
            if($tmp_w > $tmp_h) {
                $re_size[0] = $this->imgMaxWidth;
                $re_size[1] = $size[1] * $this->imgMaxWidth / $size[0];
            } else {
                $re_size[1] = $this->imgMaxHeight;
                $re_size[0] = $size[0] * $this->imgMaxHeight / $size[1];
            }
        }
    }

    $imagecreate = function_exists("imagecreatetruecolor") ? "imagecreatetruecolor" : "imagecreate";
    $imageresize = function_exists("imagecopyresampled") ? "imagecopyresampled" : "imagecopyresized";

    switch($size[2]) {
    case "1":

        if ($header) {
            header("Content-Type: video/mp4");

            $dst_im = copy($path, $dst_file);
            return "";
        } else {
            $dst_file = $dst_file . ".mp4";

            $dst_im = copy($path, $dst_file);
        }
        unlink($dst_im);

        break;

    case "2":

        $src_im = imageCreateFromJpeg($path);
        $dst_im = $imagecreate($re_size[0], $re_size[1]);

        $imageresize($dst_im, $src_im, 0, 0, 0, 0, $re_size[0], $re_size[1], $size[0], $size[1]);

        if ($header) {
            header("Content-Type: image/jpeg");
            imageJpeg($dst_im);
            return "";
        } else {
            $dst_file = $dst_file . ".jpg";
            if ($re_size[0] == $size[0] && $re_size[1] == $size[1]) {
                copy($path, $dst_file);
            } else {
                imageJpeg($dst_im, $dst_file);
            }
        }

        imagedestroy($src_im);
        imagedestroy($dst_im);

        break;

    default:
        return array(
            0,
            "file format is unknown"
        );
}
return array(
    1,
    $dst_file
);

getimagesize()僅適用於圖像。 它在視頻文件上的行為是未定義的,它可能會給出寬度和高度,但它可能會失敗。 即使它確實有效, $size[2]也不會是“1”,因為這意味着它是一個 GIF 圖像。

我會使用mime_content_type()來確定上傳的文件類型。 這將為上傳的文件返回一個 mimetype,這意味着您需要有多種情況來處理圖像文件:

$mimetype = mime_content_type($path);

switch ($mimetype) {
    case "video/mp4":
        // what is currently your case "1"
        break;

    case "image/jpeg":
    case "image/pjpeg":
    case "image/png":
    case "image/gif":
        // what is currently your case "2"
        break;

    default:
        return array(
            0,
            "file format is unknown"
        );
}

此功能是Fileinfo擴展的一部分,如果您的服務器上尚未啟用它,您可能需要安裝它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM