What's new

Multi Upload Script

reacah

Eternal Poster
Established
Joined
Oct 9, 2015
Posts
1,095
Reaction
167
Points
362
Age
34
Create A Multi Upload File Script using php and mysql
 
To create a Multi Upload File Script using PHP and MySQL, you can follow these steps:

1. Create a form in HTML with the necessary input fields for uploading files.

Code:
<form action="upload.php" method="post" enctype="multipart/form-data">
    <input type="file" name="file[]" multiple>
    <input type="submit" value="Upload">
</form>

2. Create a PHP file called upload.php that will handle the file upload process. In this file, you will need to do the following:

  • Check if the upload directory exists and is writable.
  • Loop through the uploaded files and move them to the upload directory.
  • Store the file names in a MySQL database table.

Code:
<?php
// Check if the upload directory exists and is writable
if (!file_exists('uploads')) {
    mkdir('uploads');
}

if (!is_writable('uploads')) {
    die('Upload directory is not writable');
}

// Loop through the uploaded files and move them to the upload directory
foreach ($_FILES['file']['tmp_name'] as $key => $tmp_name) {
    $file_name = $_FILES['file']['name'][$key];
    $file_size = $_FILES['file']['size'][$key];
    $file_type = $_FILES['file']['type'][$key];
    $file_error = $_FILES['file']['error'][$key];
    $file_tmp = $_FILES['file']['tmp_name'][$key];
    
    $upload_dir = 'uploads/';
    $target_file = $upload_dir . $file_name;
    
    if (move_uploaded_file($file_tmp, $target_file)) {
        // Store the file names in a MySQL database table
        $conn = mysqli_connect('localhost', 'user', 'password', 'database');
        $query = "INSERT INTO files (name, size, type) VALUES ('$file_name', $file_size, '$file_type')";
        mysqli_query($conn, $query);
    }
}
?>

3. Create a MySQL database table called files to store the uploaded file names.

Code:
CREATE TABLE files (
    id INT(11) NOT NULL AUTO_INCREMENT,
    name VARCHAR(255) NOT NULL,
    size INT(11) NOT NULL,
    type VARCHAR(255) NOT NULL,
    PRIMARY KEY (id)
);

With these steps, you can create a Multi Upload File Script using PHP and MySQL.
 

Similar threads

Back
Top