Multi Upload Script

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.
 

About this Thread

  • 1
    Replies
  • 387
    Views
  • 1
    Participants
Last reply from:
Unknown user

Trending Topics

Online now

Members online
1,207
Guests online
1,432
Total visitors
2,639

Forum statistics

Threads
2,273,458
Posts
28,949,640
Members
1,235,752
Latest member
hojerry7
Back
Top