How to Insert File Name Array Into MySQL

In many instances, you have to upload multiple images using PHP and store their names in MySQL database. In this article I am not going into the details of uploading the images and will only cover the Prepared INSERT Statement and the HTML form input statement used to carry out the upload and insertion of the image name array in this article, as there is much more coming up in the near future to assist you in creating a full-fledged script.

For those who may not know how to upload multiple images in an HTML form, here is the input code to be used for the process.

<input type="file" name="files[]" multiple />

When using this HTML input code to upload multiple images make sure you are using the correct enctype function in your form. As seen below.

< form method="POST" enctype="multipart/form-data" action="post" />

Below is the Prepared Statement used to process our images upload. I will dissect sections of the code for you to understand it better.

<?php
$stmt = $con->prepare("INSERT INTO pages (title,metadesc,text,page,pics,status) VALUES (?,?,?,?,?,?)");
$files = $_FILES['files'];
$uploaded = array();
foreach($files['name'] as $position => $file_name) {
$file_tmp = $files['tmp_name'][$position];
$file_size = $files['size'][$position];
$file_ext = explode('.', $file_name);
$file_ext = strtolower(end($file_ext));
$file_destination = $_SERVER['DOCUMENT_ROOT'] . '/images/' . $file_name;
(move_uploaded_file($file_tmp, $file_destination));
$uploaded[$position] = $file_destination;
$images_list = array($uploaded);
$images_list = array_unique($uploaded);	
}
$pics = implode(",",$images_list);
$stmt->bind_param("ssssss", $_POST["title"], $_POST["metadesc"], $_POST["text"], $page, $pics, $_POST["status"]);
$page = strtolower($_POST["title"]);
$page = str_replace(" ","-",$page);
$stmt->execute();
$stmt->close();
$con->close();
 ?>

1. We prepare to insert the form field data we have entered into the HTML form using a Prepared Statement.

2. $files = $_FILES[‘files’] is how we represent our HTML form field name and then we create an array entitled $uploaded with the uploaded items.

3. As I have explained in the past about arrays, we need to explode first before doing anything with them. Especially when we are inserting an array into a MySQL database table. The foreach statement takes care of this part of the process for us. Our goal is to insert the image names into the database in a comma separated list.

4. After making sure the file names have been turned into lowercase letters we upload them and turn them into the final array. After imploding the array into the comma separated list ‘bind_param’ checks the files and, along with our strings, enter the information in the database table with the execute (); statement.

Here is what the array of image file names will look like in the MySQL table once this has been processed.

Make sure you configure the ‘pics’ column in the table for LONGTEXT, this way you can insert as large an array as is needed without MySQL truncating it due to there not being enough characters.

This code is bare bones, as in a real situation we would want to check for the proper file extensions when uploading, etc. I will cover that in the next video tutorial, which I am hoping will be tomorrow.

In the meantime, sink your teeth into this and give it a try on a development server if you have one. For those who do not, I will probably create a tutorial on installing WAMP pretty soon. So stay tuned for that.

Add a Comment

Your email address will not be published. Required fields are marked *

ABOUT CODINGACE

My name is Nohman Habib and I am a web developer with over 10 years of experience, programming in Joomla, Wordpress, WHMCS, vTiger and Hybrid Apps. My plan to start codingace.com is to share my experience and expertise with others. Here my basic area of focus is to post tutorials primarily on Joomla development, HTML5, CSS3 and PHP.

Nohman Habib

CEO: codingace.com

Request a Quote









PHP Code Snippets Powered By : XYZScripts.com