How can i upload a video and a picture at the same time to a server using php and my sql

Issue

{ currently, all the samples I have seen so far are mainly for a single file upload. thank you in advance. i’m a student and this is my first task. I would equally love to download or save the files from data base back to my computer. I wrote the code in php and sql, my problem is that each time i try uploading the files to xampp free database,I keep on receiving one file instead of two files in my folder
I really need a professional way to do it and avoid users from submitting the same file twice. Thanks.

HTML

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <form action="post.php" method="POST" enctype="multipart/form-data">
    <input type="file" class="videofile" name="videofile">
    <input type="file" class="picturefile" name="picturefile">
    <input type="submit" class="submit" name="submit">
    </form>
</body>
</html>


    MYSQL
    
    CREATE TABLE userstable(id int(20) NOT NULL AUTO_INCREMENT PRIMARY KEY,
     VARCHAR (200) NOT NULL,
    videofile VARCHAR (200) NOT NULL,
    picturefile VARCHAR (200) NOT NULL,
    
    <?php
    
    include "connect.php";
    
    if(isset($_POST['submit'])){
    $videofile = $_FILES['vfile']['name'];
    $picturefile = $_FILES['pfile']['name'];
    
    $folder = "uploads/";
    
    
    if ($videofile == "" || $picturefile == ""){
        echo "Sorry, your file was not uploaded, ensure all filed are filed.";
    }
    
    else{
        move_uploaded_file($folder.$videofile, folder.$picturefile);
    
     
    
    
    mysqli_query ($user, "INSERT INTO usertable(videofile, picturefile) VALUES('$videofile', '$picturefile')");
    
        echo "Registration successful!";
    }
    }
    ?> }

Solution

You need to call move_uploaded_file() twice: once for the video file and once for the picture file.

move_uploaded_file($_FILES['vfile']['tmp_name'], folder.$videofile);
move_uploaded_file($_FILES['pfile']['tmp_name'], folder.$picturefile);

The first argument is the name of the file on the server and the second argument is the name of the destination file.

You should also check the return value; see the move_uploaded_file() documentation.

Answered By – kmoser

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published