首页 > 后端开发 > php教程 > 如何使用 PHP 将视频文件上传到文件夹、创建数据库条目并执行验证?

如何使用 PHP 将视频文件上传到文件夹、创建数据库条目并执行验证?

Barbara Streisand
发布: 2024-10-21 14:01:30
原创
725 人浏览过

How to Upload Video Files to a Folder, Create a Database Entry, and Perform Validation with PHP?

使用 PHP 上传视频文件并将其保存在适当的文件夹中并创建数据库条目

此 PHP 脚本允许用户将视频文件上传到指定文件夹并创建相应的数据库条目数据库条目:

<code class="php"><?php

// Upload settings
define('DESTINATION_FOLDER', '/uploads/'); // Folder to save uploaded videos
define('MAX_FILE_SIZE', 10485760); // Maximum file size allowed in bytes (10MB)
define('ALLOWED_EXTENSIONS', ['avi', 'mov', 'mp4']); // Allowed file extensions

// Database settings
define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASSWORD', '');
define('DB_NAME', 'my_database');

// Initialize variables
$errors = [];
$message = '';
$uploadSuccess = false;

// Process form submission
if (isset($_POST['submit'])) {

    // Fetch form data
    $filename = $_FILES['filename']['name'];
    $tmp_filename = $_FILES['filename']['tmp_name'];
    $file_ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
    $filesize = $_FILES['filename']['size'];

    // Validate file
    if (!$tmp_filename) {
        $errors[] = 'Error: No file selected.';
    } elseif (!in_array($file_ext, ALLOWED_EXTENSIONS)) {
        $errors[] = 'Error: File type not allowed.';
    } elseif ($filesize > MAX_FILE_SIZE) {
        $errors[] = 'Error: File size exceeds limit.';
    }

    // Upload file if no errors
    if (empty($errors)) {
        $newFilename = uniqid() . '.' . $file_ext;
        $uploadPath = DESTINATION_FOLDER . $newFilename;

        // Move uploaded file to the specified folder
        if (move_uploaded_file($tmp_filename, $uploadPath)) {
            $uploadSuccess = true;
            $message = 'File uploaded successfully.';
        } else {
            $errors[] = 'Error: Unable to move uploaded file.';
        }
    }

    // Create database entry if upload was successful
    if ($uploadSuccess) {
        try {
            // Establish database connection
            $conn = new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_NAME, DB_USER, DB_PASSWORD);
            $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

            // Prepare SQL query
            $stmt = $conn->prepare("INSERT INTO uploads (`name`, `path`) VALUES (?, ?)");
            $stmt->execute([$filename, $uploadPath]);
        } catch (PDOException $e) {
            $errors[] = 'Error: Database connection failed: ' . $e->getMessage();
        }
    }
}

?>

<!DOCTYPE html>
<html>
<head>
    <title>Upload Video Files</title>
</head>
<body>

    <h1>Upload Video Files</h1>

    <form method="post" enctype="multipart/form-data">
        <label for="filename">Filename:</label>
        <input type="file" name="filename">

        <input type="submit" name="submit" value="Upload">
    </form>

    <?php if (!empty($message)): ?>
        <div><?php echo $message; ?></div>
    <?php endif; ?>

    <?php if (!empty($errors)): ?>
        <ul>
            <?php foreach ($errors as $error): ?>
                <li><?php echo $error; ?></li>
            <?php endforeach; ?>
        </ul>
    <?php endif; ?>

</body>
</html></code>
登录后复制

提交后,此脚本执行以下任务:

  • 验证上传的文件类型、大小和扩展名。
  • 移动上传文件到指定文件夹。
  • 创建数据库连接并在上传表中插入包含视频文件名和路径的条目。

以上是如何使用 PHP 将视频文件上传到文件夹、创建数据库条目并执行验证?的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板