当前位置:首页 > PHP

php实现上传视频文件

2026-02-13 21:54:09PHP

文件上传表单设计

创建HTML表单,设置enctype="multipart/form-data"以支持文件上传。表单需包含file类型的输入字段。

<form action="upload.php" method="post" enctype="multipart/form-data">
    <input type="file" name="video_file" accept="video/*">
    <input type="submit" value="Upload">
</form>

服务器端验证处理

在PHP脚本中通过$_FILES超全局数组获取上传文件信息。需验证文件类型、大小及错误状态。

$targetDir = "uploads/";
$maxFileSize = 500 * 1024 * 1024; // 500MB
$allowedTypes = ['video/mp4', 'video/quicktime'];

if ($_FILES['video_file']['error'] !== UPLOAD_ERR_OK) {
    die("Upload error: " . $_FILES['video_file']['error']);
}

if ($_FILES['video_file']['size'] > $maxFileSize) {
    die("File exceeds maximum size limit");
}

if (!in_array($_FILES['video_file']['type'], $allowedTypes)) {
    die("Invalid file type");
}

安全存储处理

生成唯一文件名防止冲突,使用move_uploaded_file()函数安全存储文件。

php实现上传视频文件

$fileExtension = pathinfo($_FILES['video_file']['name'], PATHINFO_EXTENSION);
$newFileName = uniqid() . '.' . $fileExtension;
$targetPath = $targetDir . $newFileName;

if (!move_uploaded_file($_FILES['video_file']['tmp_name'], $targetPath)) {
    die("Failed to move uploaded file");
}

文件类型深度验证

除MIME类型外,建议使用finfo扩展进行二进制内容验证。

$finfo = finfo_open(FILEINFO_MIME_TYPE);
$detectedType = finfo_file($finfo, $_FILES['video_file']['tmp_name']);
finfo_close($finfo);

if (!in_array($detectedType, $allowedTypes)) {
    unlink($_FILES['video_file']['tmp_name']);
    die("File content does not match declared type");
}

大文件上传优化

对于大视频文件,可在php.ini中调整以下参数:

php实现上传视频文件

upload_max_filesize = 512M
post_max_size = 520M
max_execution_time = 300

或通过.htaccess文件设置:

php_value upload_max_filesize 512M
php_value post_max_size 520M
php_value max_execution_time 300

分块上传实现

对于超大文件,可采用分块上传技术。前端使用JavaScript将文件分片,后端合并分片。

// 接收分片文件
$chunkNumber = $_POST['chunk'];
$totalChunks = $_POST['total'];
$tempDir = "temp_uploads/";

if (!file_exists($tempDir)) {
    mkdir($tempDir, 0755, true);
}

$tempFilePath = $tempDir . $_FILES['file']['name'] . '.part' . $chunkNumber;
move_uploaded_file($_FILES['file']['tmp_name'], $tempFilePath);

// 合并分片
if ($chunkNumber == $totalChunks - 1) {
    $finalPath = $targetDir . $_FILES['file']['name'];
    $fp = fopen($finalPath, 'wb');

    for ($i = 0; $i < $totalChunks; $i++) {
        $chunkPath = $tempDir . $_FILES['file']['name'] . '.part' . $i;
        $chunkContent = file_get_contents($chunkPath);
        fwrite($fp, $chunkContent);
        unlink($chunkPath);
    }

    fclose($fp);
}

分享给朋友:

相关文章

vue 怎么实现上传

vue 怎么实现上传

Vue 实现文件上传的方法 使用原生 HTML5 文件上传 通过 <input type="file"> 元素结合 Vue 的数据绑定实现基础文件上传功能。 <template&g…

php 实现上传视频

php 实现上传视频

上传视频的基本流程 在PHP中实现视频上传功能需要处理文件接收、验证、存储等步骤。核心是通过$_FILES超全局变量获取上传的文件信息,结合move_uploaded_file函数完成存储。 创建H…

vue实现拖拽上传

vue实现拖拽上传

Vue 拖拽上传实现方法 使用 HTML5 原生拖拽 API 在 Vue 中可以通过监听原生拖拽事件实现基础功能: <template> <div class="dr…

vue实现批量上传

vue实现批量上传

Vue 实现批量上传的方法 使用 <input type="file"> 和 multiple 属性 在 Vue 模板中添加一个文件输入框,设置 multiple 属性以允许选择多个文件。…

vue实现切片上传

vue实现切片上传

切片上传的实现原理 切片上传是将大文件分割成多个小块(切片)分别上传,最后在服务端合并。这种方式能提高上传效率、支持断点续传和并发上传。 前端实现步骤 使用Vue实现切片上传需要借助File对象…

vue实现音乐上传

vue实现音乐上传

Vue 实现音乐上传功能 音乐上传功能通常涉及前端界面交互、文件选择、上传进度显示以及后端接口对接。以下是基于 Vue 的实现方法: 前端界面设计 使用 Vue 的模板语法创建一个简单的上传界面,包…