Php Upload Multiple Files and Form Data

Upload Multiple Files in PHP

  1. Use the multiple Tag, an Array in the proper noun Tag of the input Attribute in the Form and the move_uploaded_file() Role to Upload Multiple File in PHP
  2. Utilize the PDO to Upload the Multiple Files in the Database in PHP

We will introduce a method to upload multiple files in PHP by specifying the name attribute of the input tag as an array and using the multiple aspect. The value of the enctype aspect of the course tag is multipart/class-data. This method uses mysqli for the database connection.

We volition also introduce another method to upload multiple files in PHP using the PDO. Nosotros will upload the files in a folder and and so upload them to a database. This method is similar to the beginning method in terms of implementation.

Use the multiple Tag, an Array in the name Tag of the input Aspect in the Form and the move_uploaded_file() Function to Upload Multiple File in PHP

We tin can specify the proper noun attribute of the input tag every bit an array to upload multiple files. The input tag uses the multiple keyword that lets u.s. select the multiple file while uploading. Nosotros can write the encoding multipart/form-data for the enctype attribute to specify how the course-data should be encoded while submitting the grade. We can utilize the mysqli to connect to the database. The move_uploaded_file() function moves the uploaded file from a temporary location of the server to the desired location. We can apply the INSERT SQL statements to upload the selected file to the database.

For case, set a database connection with the mysqli object and assign the value to the $db variable. Create an HTML form with POST method and encoding type every bit multipart/form-data to upload the files. Specify the type attribute of the input tag as file and the name attribute as file[]. Practise not forget to write the aspect multiple earlier closing the input tag. Write the input tag to submit the grade.

Check whether the form has been submited with the isset() function. Use the count function to count the number of uploaded files. Take the $_FILES['file']['name'] as the parameter for the count part and assign it to a variable $countfiles. Use the for loop to loop over the uploaded file. Inside the loop, create a variable $filename and assign it equally $_FILES['file']['name'][$i]. Create a folder named upload in the root directory. Use the move_upload_file() function to move the file. Use $_FILES['file']['tmp_name'][$i] as the beginning parameter which is the file with temporary name. Employ 'upload/'.$filename as the second parameter which is the filename and the location to store the uploaded file.

Run a SQL INSERT query to insert the files into the database. Insert the variable $filename every bit both id and proper noun into the fileup table in the database. Use the$db variable to phone call the query() function with the $sql variable every bit the parameter to execute the query.

Example Lawmaking:

              # php 7.* <?php if(isset($_POST['submit'])){     $countfiles = count($_FILES['file']['name']);     for($i=0;$i<$countfiles;$i++){         $filename = $_FILES['file']['name'][$i];         $sql = "INSERT INTO fileup(id,name) VALUES ('$filename','$filename')";         $db->query($sql);         move_uploaded_file($_FILES['file']['tmp_name'][$i],'upload/'.$filename);     } } ?> <grade method='postal service' action='' enctype='multipart/grade-data'> <input type="file" name="file[]" id="file" multiple> <input type='submit' name='submit' value='Upload'> </course> ?>                          

Output:

              SELECT * FROM `fileup` id name  1 cfc.jpg  ii hills.jpg                          

Use the PDO to Upload the Multiple Files in the Database in PHP

We can use the PDO to upload multiple files in PHP. Nosotros can employ the PDO object to create a connexion to the database. We use the prepared statements to insert the file into the database. This method is only different from the first method in terms of the database connection. We can utilize the exact HTML form for this method too. We demonstrate this method by uploading two jpg files in the folder and the database.

For case, establish a PDO database connection using the new keyword and assign information technology to $conn variable. Check whether the form has been submitted with the isset() part. Count the files using the count function as higher up. Then, write a query on the $query variable to insert the id and proper name. Write the placeholders ? for the values in the query. Fix the query using the fix() function and assign the value in $statement variable. Move the each file to the uplaod folder using the move_uploaded_file() office. Execute the query using the execute() part. Supply an array with the $filename and $target_file as the parameters in the office.

In the instance beneath, the user uploads ii jpg files. Firstly, the file gets uploaded to the upload folder, and then it gets uploaded to the database. The database contains the table fileup, and it has the columns id and name. To learn more well-nigh the move_uploaded_files() part, please refer to the PHP Manual.

Case Code:

              #php seven.ten <?php $conn = new PDO("mysql:host=$server;dbname=$dbname","$username","$password"); $conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); if(isset($_POST['submit'])){     $countfiles = count($_FILES['file']['proper noun']);     $query = "INSERT INTO fileup (id, name) VALUES(?,?)";     $statement = $conn->ready($query);     for($i=0;$i<$countfiles;$i++){         $filename = $_FILES['file']['proper name'][$i];         $target_file = 'upload/'.$filename;         move_uploaded_file($_FILES['file']['tmp_name'][$i],$target_file);         $statement->execute(array($filename,$target_file));         }   }     ?>                          

Output:

              SELECT * FROM `fileup` id name  1 upload/cfc.jpg  2+ upload/count.jpg                          

Write for us

DelftStack articles are written by software geeks like y'all. If yous as well would like to contribute to DelftStack by writing paid manufactures, y'all tin can check the write for us folio.

Ezoic

schmidtyouttleste.blogspot.com

Source: https://www.delftstack.com/howto/php/php-multiple-file-upload/

0 Response to "Php Upload Multiple Files and Form Data"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel