使用php将mp4文件上传到数据库

时间:2018-02-16 21:01:53

标签: php android mysql smarty mp4

我正在努力实现一个包含php,mysql,smarty的网站,我正面临着一个无法在注册屏幕上将mp4文件上传到数据库的问题,我可以用jpg上传文件。

当我点击上传按钮时,它会在屏幕的左下角显示上传进度的百分比达到99%左右,然后在结尾处说“文件格式未知”,这是来自下面的php编码客户端背面有白色屏幕,并且没有在服务器上显示任何错误消息。

我的问题是,如果您可以从下面的编码中看出错误的情况“1”是mp4,而情况“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
);

1 个答案:

答案 0 :(得分:1)

const TriggerComponent = () => <div> trigger example </div> <MyComponent trigger={<TriggerComponent />} /> 仅适用于图片。它在视频文件上的行为是未定义的,可能给出宽度和高度,但它可能只是失败。即使 工作,getimagesize()也不会是&#34; 1&#34;因为这意味着它是GIF图像。

我会使用$size[2]来确定上传的文件类型。这将返回上传文件的mimetype,这意味着您需要有多个案例来处理图像文件:

mime_content_type()

此功能是Fileinfo扩展程序的一部分,如果您的服务器上尚未启用,则可能需要安装该扩展程序。