Home > Daily Programming > PHP Knowledge > How to implement the function of uploading videos in PHP? (Pictures + Videos)

How to implement the function of uploading videos in PHP? (Pictures + Videos)

藏色散人
Release: 2019-12-20 17:58:00
Original
28800 people have browsed it

This article mainly explains to you the specific implementation method of PHP uploading video.

It may be difficult for novices to implement the function of PHP video upload. But through the introduction in this article, I believe everyone can master the relevant knowledge.

So in the previous articles [Detailed explanation of PHP file upload method and its information analysis] and [What are the simple ways to upload multiple files in PHP? ], I have explained in detail the functions of PHP to implement file upload and multiple file upload. Friends in need can read and refer to these two articles first to help understand the content of this chapter.

In fact, video is also a kind of file, and their principles are basically the same.

Next, we will introduce the implementation method of uploading videos in PHP through specific code examples.

First the front-end HTML form code is as follows:

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <title>PHP中文网上传视频</title>
</head>
<body>
<form action=&#39;demo42.php&#39; method=post enctype="multipart/form-data">
    <input type="hidden" name="MAX_FILE_SIZE" value="2000000000">
    <input type=file name=upfile size=20>
    <input type=submit value=&#39;上传文件&#39;>
</form>
</body>
</html>
Copy after login

The front-end page effect is as follows:

How to implement the function of uploading videos in PHP? (Pictures + Videos)

Then the PHP code for background processing of uploaded videos is as follows:

<?php
/**
 * PHP上传视频
 */
$upfile = $_FILES[&#39;upfile&#39;];

function upload_file($files, $path = "./upload",$imagesExt=[&#39;jpg&#39;,&#39;png&#39;,&#39;jpeg&#39;,&#39;gif&#39;,&#39;mp4&#39;])
{
    // 判断错误号
    if (@$files[&#39;error&#39;] == 00) {
        // 判断文件类型
        $ext = strtolower(pathinfo(@$files[&#39;name&#39;],PATHINFO_EXTENSION));
        if (!in_array($ext,$imagesExt)){
            return "非法文件类型";
        }
        // 判断是否存在上传到的目录
        if (!is_dir($path)){
            mkdir($path,0777,true);
        }
        // 生成唯一的文件名
        $fileName = md5(uniqid(microtime(true),true)).&#39;.&#39;.$ext;
        // 将文件名拼接到指定的目录下
        $destName = $path."/".$fileName;
        // 进行文件移动
        if (!move_uploaded_file($files[&#39;tmp_name&#39;],$destName)){
            return "文件上传失败!";
        }
        return "文件上传成功!";
    } else {
        // 根据错误号返回提示信息
        switch (@$files[&#39;error&#39;]) {
            case 1:
                echo "上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值";
                break;
            case 2:
                echo "上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值";
                break;
            case 3:
                echo "文件只有部分被上传";
                break;
            case 4:
                echo "没有文件被上传";
                break;
            case 6:
            case 7:
                echo "系统错误";
                break;
        }
    }
}
echo upload_file($upfile);
?>
Copy after login

So in this PHP code, we define an upload_file upload function, through which not only video upload but also image upload can be achieved. And the meaning of each step of operation has been introduced to you through detailed annotations for your reference and study.

Finally we can choose to upload a video for testing. The results are as follows:

How to implement the function of uploading videos in PHP? (Pictures + Videos)

How to implement the function of uploading videos in PHP? (Pictures + Videos)

This article is Regarding the specific implementation method of PHP video uploading, I hope it will be helpful to friends in need!

If you want to learn more about PHP, you can follow the PHP Chinese website PHP Video Tutorial, everyone is welcome to refer to and learn!

The above is the detailed content of How to implement the function of uploading videos in PHP? (Pictures + Videos). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template