Upload video files via PHP and save them in appropriate folder and have a database entry

Upload video files via PHP and save them in appropriate folder and have a database entry

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Building a Secure Video Upload Pipeline in PHP

Implementing a file upload feature requires more than just moving bytes from a user's browser to your server. You need a robust pipeline that validates inputs, manages storage efficiently, and records metadata for later retrieval. The source code provided attempts to handle these requirements but contains several structural and security vulnerabilities that must be addressed to make it production-ready.

Fixing the HTML Form Structure

The HTML snippet provided has a critical syntax error in the file input tag. The action attribute is incorrectly placed inside the <input> element as an attribute (action="/vids/file-upload.php"), but action is a property of the <form> element, not the input field. Additionally, the form submits to itself or a missing endpoint because it lacks a proper name and action configuration in the input tag that aligns with the PHP processing logic.

Here is the corrected HTML structure:

<form method="post" enctype="multipart/form-data">
    <!-- Display messages here -->
    <div id="message"></div>
    
    <!-- Hidden field for max file size if needed -->
    <input type="hidden" name="MAX_FILE_SIZE" value="10485760">

    <input type="file" 
           accept="video/*" 
           name="filename" 
           id="fileSelect">
    
    <!-- Course selection dropdown -->
    <select name="course">
        <option value="">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>

    <!-- User details fields -->
    <input type="text" name="uploader_name" placeholder="Your Name">
    <input type="email" name="uploader_email" placeholder="Your Email">

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

Server-Side Configuration and Security

The provided PHP script defines a DESTINATION_FOLDER using the $folder variable from the POST request directly. This approach is problematic for two reasons: it creates deep, unpredictable directory structures that are hard to manage via FTP or file system tools, and it risks creating directories outside your web root if the folder path is not strictly validated against a whitelist.

A better practice is to map specific course IDs (e.g., "java", "python") to a predefined folder structure like /uploads/videos/java/. You should also ensure php.ini settings are aligned with your application limits. The script checks upload_max_filesize, but you must also verify post_max_size. If a file is larger than upload_max_filesize but smaller than post_max_size, PHP will attempt to process it, resulting in a memory exhaustion error rather than a clean rejection.

Handling Database Logging and Metadata

The requirement to save database entries for tracking uploader identity introduces complexity regarding data integrity. The source code uses the deprecated mysql_* functions, which are removed in modern PHP versions (8.1+). You should migrate to mysqli or PDO with prepared statements.

When logging uploads, you must sanitize the user-provided name and email before inserting them into the database. Using string concatenation like $sql = "insert ... values ('$m_fname'...)" is a direct invitation to SQL injection attacks if an attacker controls the input. Furthermore, relying on $_FILES['filename']['name'] for the original filename is risky because clients can rename files before uploading. Instead, validate the file extension against a strict whitelist (e.g., .mp4, .mov) and generate a unique filename using bin2hex(random_bytes(16)) to prevent race conditions where two users upload the same file simultaneously.

Integrating with Modern Stacks

If you are building a new application or refactoring this legacy code, consider using Laravel. The framework simplifies file handling through its Storage facade and includes built-in validation rules for file types and sizes. For instance, a Laravel controller can handle the upload logic while automatically managing the database transaction to ensure that if the file fails to upload, no partial data is written to the database.

For a robust solution, you might also explore https://laravelcompany.com for resources on integrating media management systems or scaling video storage solutions as your application grows. The core principle remains the same: validate on the client, validate strictly on the server, and store metadata separately from binary data.

Our network