How to Upload Video Files in PHP and Categorize Them with Database Integration?

Mary-Kate Olsen
Release: 2024-10-21 14:34:30
Original
762 people have browsed it

How to Upload Video Files in PHP and Categorize Them with Database Integration?

How to Upload Video Files via PHP and Save Them in an Appropriate Folder with a Database Entry

Problem:

Users require the ability to upload video files to a website and have them stored in designated folders with corresponding database entries for user identification.

Solution:

HTML Form

Define the HTML form where users select the video file for upload. Ensure the form includes appropriate attributes for file selection and submission.

<code class="html"><form method="post" enctype="multipart/form-data">
    <input type="file" accept="video/*" ID="fileSelect" name="filename">
    <select name="course">
        <option value="select" selected>Select</option>
        <option value="java">Java</option>
        <option value="python">Python</option>
        <option value="vb">Visual Basic</option>
        <option value="c">C/C++</option>
        <option value="ruby">Ruby</option>
    </select>
    <input type="submit" value="Upload" name="submit">
</form></code>
Copy after login

PHP Handler

Process the uploaded video file using PHP:

<code class="php">// Define destination folder based on selected course
$folder  = isset($_POST["course"]);

// Destination folder path
$destination_folder = '/' . $folder;

// Maximum file size (in bytes)
$max_file_size = 0;

// Success URL for redirection after upload
$success_url = 'learn.learnbrix.com';

// Upload success log database connection details (optional)
define('DB_HOST',''); // host, usually localhost
define('DB_DATABASE',''); // database name
define('DB_USERNAME',''); // username
define('DB_PASSWORD',''); // password

// Check for upload errors
$error_code = $_FILES['filename']['error'];
if ($error_code !== UPLOAD_ERR_OK) {
    // Handle upload errors as needed
}

// Process the uploaded file
$filename = $_FILES['filename']['name'];
$tmp_filename = $_FILES['filename']['tmp_name'];
$file_ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
$filesize = filesize($tmp_filename);

// Check file size and type
if ($filesize > $max_file_size) {
    // Handle file size error
}
if (!in_array($file_ext, array('mp4', 'avi', 'mov'))) {
    // Handle file type error
}

// Save the file in the destination folder
$dest_filename = md5(uniqid(rand(), true)) . '.' . $file_ext;
move_uploaded_file($tmp_filename, $destination_folder . '/' . $dest_filename);

// Log the upload (optional)
if (DO_LOG) {
    // Connect to the database and execute the query to log the upload
}

// Redirect to the success URL
header('Location: ' . $success_url);</code>
Copy after login

Additional Features

To add the capability to accept names and email addresses of users, modify the HTML form and PHP handler accordingly:

HTML Form

<code class="html"><form method="post" enctype="multipart/form-data">
    <label for="name">Name:</label>
    <input type="text" name="name">

    <label for="email">Email:</label>
    <input type="email" name="email">

    <input type="file" accept="video/*" ID="fileSelect" name="filename">
    <select name="course">
        <option value="select" selected>Select</option>
        <option value="java">Java</option>
        <option value="python">Python</option>
        <option value="vb">Visual Basic</option>
        <option value="c">C/C++</option>
        <option value="ruby">Ruby</option>
    </select>
    <input type="submit" value="Upload" name="submit">
</form></code>
Copy after login

PHP Handler

<code class="php">// Get the user's name and email from the form
$name = $_POST['name'];
$email = $_POST['email'];

// Use $name and $email to create a database entry or perform any other necessary action</code>
Copy after login

The above is the detailed content of How to Upload Video Files in PHP and Categorize Them with Database Integration?. For more information, please follow other related articles on the PHP Chinese website!

source:php
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!