can some one help me with the below code? I am trying to create a multiple image upload form that displays the images.

I want to save the image name into my msql database and save the actual image into a folder in my dirrectory.


define('UPLOAD_PATH', $_SERVER['DOCUMENT_ROOT'] . '/upload/');
define('DISPLAY_PATH', '/upload/');
define('MAX_FILE_SIZE', 2000000);
$permitted = array('image/jpeg', 'image/pjpeg', 'image/png', 'image/gif');

if (isset($_POST['upload'])) {

$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];

// get the file extension 
$ext = substr(strrchr($fileName, "."), 1);
// generate the random file name
$randName = md5(rand() * time());

// image name with extension
$myfile = $randName . '.' . $ext;
// save image path
$path = UPLOAD_PATH . $myfile;

if (in_array($fileType, $permitted) && $fileSize > 0 && $fileSize <= MAX_FILE_SIZE) {
//store image to the upload directory
$result = move_uploaded_file($tmpName, $path);

if (!$result) {
echo "Error uploading image file";
} else {
$db = new mysqli("Localhost", "", "", "");

if (mysqli_connect_errno()) {
printf("Connect failed: %s<br/>", mysqli_connect_error());

$query =
 "INSERT INTO my_image(name, size, type, file_path) VALUES(?,?,?,?)";
$conn = $db->prepare($query);
if ($conn == TRUE) {
$conn->bind_param("siss", $myfile, $fileSize, $fileType, $path);
if (!$conn->execute()) {
echo 'error insert';
} else {
echo 'Success!<br/>';
echo '<img src="' . DISPLAY_PATH . $myfile . '"  height="330"  width="290"  />';
} else {
die("Error preparing Statement");
} else {
echo 'error upload file';
} else {
echo 'error';

        <title>Upload File To MySQL Database</title>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
        <style type="text/css">
            .myc {
                font-size: 12px;
                border: 1px solid #000000;
        <form action="form23.php" 
              enctype="multipart/form-data" name="uploadform" method="post" >
          <table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
                <input type="hidden" name="MAX_FILE_SIZE" value="2000000">
                <input name="userfile" type="file" id="userfile"/>
                <input name="upload" type="submit" class="myc" id="upload" value="Upload"/>

I dont want each image to have its own id in mysql database because i want to assigh four images to a user so that it can display into their profile.

I know something like this would work but please let me know if anyone knows of anything that can help.

$imageUploadDir = '../upload/';

$imageName1 = $_FILES['image1']['name'];
$tmpImageName1 = $_FILES['image1']['tmp_name'];

$imageName2 = $_FILES['image2']['name'];
$tmpImageName2 = $_FILES['image2']['tmp_name'];
$imageName3 = $_FILES['image3']['name'];
$tmpImageName3 = $_FILES['image3']['tmp_name'];
$imageName4 = $_FILES['image4']['name'];
$tmpImageName4 = $_FILES['image4']['tmp_name'];

// the files will be saved in filePath
$imagePath1 = $imageUploadDir . $imageName1;
$imagePath2 = $imageUploadDir . $imageName2;
$imagePath3 = $imageUploadDir . $imageName3;
$imagePath4 = $imageUploadDir . $imageName4;

$imageName1 = 'upload/'. $imageName1;
$imageName2 = 'upload/'. $imageName2;
$imageName3 = 'upload/'. $imageName3;
$imageName4 = 'upload/'. $imageName4;

// move the files to the specified directory
// if the upload directory is not writable or
// something else went wrong $result will be false
if( file_exists($tmp_name) ){
move_uploaded_file($tmp_name, $filePath);
if( file_exists($tmpImageName1) ){
move_uploaded_file($tmpImageName1, $imagePath1);
if( file_exists($tmpImageName2) ){
move_uploaded_file($tmpImageName2, $imagePath2);
if( file_exists($tmpImageName3) ){
move_uploaded_file($tmpImageName3, $imagePath3);
if( file_exists($tmpImageName4) ){
move_uploaded_file($tmpImageName4, $imagePath4);

$fileName = addslashes($fileName);
$filePath = addslashes($filePath);

<input name="image1" type="file" id="ufile[]" size="50" /></td>
<input name="image2" type="file" id="ufile[]" size="50" /></td>
<input name="image3" type="file" id="ufile[]" size="50" /></td>
<input name="image4" type="file" id="ufile[]" size="50" /></td>
5 Years
Discussion Span
Last Post by aflor


the name on these form inputs...

     <input name="image1" type="file" id="ufile[]" size="50" />

should also be in an array like this

     <input name="ufile[]" type="file" id="ufile[]" size="50" />

UPDATE! I have to remove some codes, because it was deprecated...just use $_FILES..

Edited by veedeoo: more info added


ok.. here is the php.net manual for multiple file uploads Click Here..Read on leehowarth1 post.. or by anyone should and can help you out.

We cannot use $_FILES['this'], because the upload is coming as an array. So, your codes above can be something like this

 $fileNameOne = $_FILES['ufile']['name'][0];
 $fileNameTwo = $_FILES['ufile']['name'][1];

Or better yet, just use one of the recommended approach in php.net as linked above..

I could have written this script.. this is not pretty hard to do, but I am preparing for my flight to California. I need to be in CalTech by Monday. So, I aplogize if I can't do it this time. Normally, I would write a simple demo script, but not this time.

Edited by veedeoo: more info added


Thank you sir. I really appreaciate it. I am at work and will look into it once i get home.

Have a safe flight.

If anyone else has anything to share please do :)


The pgp manual doesnt really explain how to save the images into the msql and server folder it seems.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.