Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

adding multiple images into a MySQL database with PHP

max_file_size mysql

  • Please log in to reply
17 replies to this topic

#1 whitestar

whitestar

    CC Regular

  • Just Joined
  • PipPipPip
  • 33 posts

Posted 18 February 2011 - 05:36 AM

Hello

I followed the MySQL with PHP / Part II / Display your images there was a mention of adding multiple images at the same time using a single submit button and that it was achieved by specifing the image variable as an array by using the following code

<form enctype="multipart/form-data" action="insert.php" method="post" name="changer">
<input name="MAX_FILE_SIZE" value="102400" type="hidden">
<input name="image[]" accept="image/jpeg" type="file">
<input name="image[]" accept="image/jpeg" type="file">
<input name="image[]" accept="image/jpeg" type="file">
<input name="image[]" accept="image/jpeg" type="file">
<input value="Submit" type="submit">


it then stated that you would need to iterate thriough the image[] array on the insert.php page but there was no mention of the script to use on the insert.php page to do this I was wondering if you could assist me in doing this?
and then
  • 0

#2 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 18 February 2011 - 12:48 PM

image[] will be an array in $_FILES, so you would access each individual file like so:

$_FILES['image']['name'][0] //image 1
$_FILES['image']['name'][1] //image 2
$_FILES['image']['name'][2] //image 3
$_FILES['image']['name'][3] //image 4
$_FILES['image']['size][0] 
$_FILES['image']['size][1] 
$_FILES['image']['size][2]
$_FILES['image']['size][3]
If they had not used the third upload box, then $_FILES['image']['error']['2'] will be '4', 4 denoting that they haven't used it, so you must ignore it.

Once you have the data of all 4 files, you can insert the multiple images in with one query, or separate queries, i.e.

$id = mysql_real_escape_string($_POST[$id]);
$name = mysql_real_escape_string($_POST['image_name']);
$query = "INSERT INTO $table" .
"($column, $name, $id) VALUES ('$data1', '$name', $id), ('$data2', '$name', $id ), ('$data3', '$name', $id), ('$data4', '$name', $id)";
This assumes $data1 .. $data4 are the data from your uploaded files like you got with fopen.

More info can be found here:
PHP: Uploading multiple files - Manual
PHP: Error Messages Explained - Manual
MySQL :: MySQL 5.5 Reference Manual :: 12.2.5 INSERT Syntax
  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#3 whitestar

whitestar

    CC Regular

  • Just Joined
  • PipPipPip
  • 33 posts

Posted 22 February 2011 - 04:24 AM

Hello Alexander thank you

I tried it on my page in the following way but it is not uploading the images into the database it is ging the mesage that "No image selected/uploaded." this is the actuall code on my pages I think Ive missed somethin or am doing something wrong

add.html

<form action="insert.php" method="post" enctype="multipart/form-data" name="browse" id="brows_form">

<div align="left">
<p <span class="style10">Main Image Max File Size 330KB</span>
<input type="hidden" name="MAX_FILE_SIZE" value="852400" >
<input name="image[]" type="file" class="style1" accept="image/jpeg" />
<span class="style10">Name the Main Image</span>
<input type="text" name="image_name[]" value="" size="32" />
<input type="hidden" name="subscriber[]" value="<?php echo $row_super_subscriber_username[subscriber_id']; ?>" /></p>

<p <span class="style10">Image #1 Max File Size 330KB</span>
<input name="image[]" type="file" class="style1" accept="image/jpeg" />
<span class="style10">Name the Image #1</span>
<input type="text" name="image_name[]" value="" size="32" />
<input type="hidden" name="subscriber[]" value="<?php echo $row_super_subscriber_username[subscriber_id']; ?>" /></p>

<p <span class="style10">Image #2 Max File Size 330KB</span>
<input name="image[]" type="file" class="style1" accept="image/jpeg" />
<span class="style10">Name the Image #2</span>
<input type="text" name="image_name[]" value="" size="32" />
<input type="hidden" name="subscriber[]" value="<?php echo $row_super_subscriber_username[subscriber_id']; ?>" /></p>

<p <span class="style10">Image #3 Max File Size 330KB</span>
<input name="image[]" type="file" class="style1" accept="image/jpeg" />
<span class="style10">Name the Image #3</span>
<input type="text" name="image_name[]" value="" size="32" />
<input type="hidden" name="subscriber[]" value="<?php echo $row_super_subscriber_username[subscriber_id']; ?>" /></p>
<input type="submit" value="Upload" >
<span class="style10">Choose & Name Your Picture</span></div>
</form>


insert.php
<?php

$username = "root";
$password = "159753";
$host = "localhost";
$database = "the_secrets_out";
$table = "images";
$column = "image";
$id = "subscriber";
$name ="image_name";


$link = mysql_connect($host, $username, $password); if (!$link) {
die("Can not connect to database: ".mysql_error());
}

mysql_select_db ($database);





if (isset($_FILES['image']['image_name'][0], //image 1
$_FILES['image']['image_name'][1], //image 2
$_FILES['image']['image_name'][2], //image 3
$_FILES['image']['image_name'][3], //image 4
$_FILES['image']['size'][0],
$_FILES['image']['size'][1],
$_FILES['image']['size'][2],
$_FILES['image']['size'][3])) {

$tmpName = $_FILES[$column]['tmp_name'];

$fp = fopen($tmpName, 'r');
$data = fread($fp, filesize($tmpName));
$data = addslashes($data);
fclose($fp);

$id = mysql_real_escape_string($_POST[$id]);
$name = mysql_real_escape_string($_POST['image_name']);
$query = "INSERT INTO $table" .
$query .="($column, $name, $id) VALUES ('$data1', '$name', $id), ('$data2', '$name', $id ), ('$data3', '$name', $id), ('$data4', '$name', $id)";
$results = mysql_query($query, $link);

print "Thank you, Your Image has been uploaded. DONT FORGET to add the image into your profile! ";

}
else {
print "No image selected/uploaded. Make sure that you have not exceeded the max file size!";
}

mysql_close($link);
?>

  • 0

#4 Orjan

Orjan

    CC Mentor

  • Moderator
  • 2918 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#

Posted 22 February 2011 - 09:07 AM

Your if statement in insert.php requires all four slots to be uploaded with files. better to loop through them one by one.

Example:


for (i=0; i < 4; i++) {
if ($_FILES['image']['image_name'][$i] != "" && $_FILES['image']['size'][$i] > 0) {
// here you do what you do for each slot...
}
]

  • 0

I'm a System developer at XLENT Consultant Group mainly working with SugarCRM.
Please DO NOT send mail or PM to me with programming questions, post them in the appropriate forum instead, where I and others can answer you.


#5 whitestar

whitestar

    CC Regular

  • Just Joined
  • PipPipPip
  • 33 posts

Posted 23 February 2011 - 01:44 AM

Hello Orjan

That doesnt seem to be working Im new to this and cant figure out what to do for each slot Please could you elaborate.
I keep getting an error about this line
 for (i=0; i < 4; i ++)  {

  • 0

#6 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 23 February 2011 - 02:19 AM

He was writing out in example and forgot to append the dollar sign to the variables:
 for ($i=0; $i < 4; $i++)  {  

  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#7 whitestar

whitestar

    CC Regular

  • Just Joined
  • PipPipPip
  • 33 posts

Posted 23 February 2011 - 02:52 AM

Thanks I fixed that part and Im not getting any errors on the page. I am however getting the message four times repeatedadly that No image was uploaded and there is no image bieng uploaded to the database. This is what my code looks like now. I can tell that it is really close but just not there yet!

<?php

$username = "root";
$password = "159753";
$host = "localhost";
$database = "the_secrets_out";
$table = "images";
$column = "image";
$id = "suubscriber";
$name ="image_name";


$link = mysql_connect($host, $username, $password); if (!$link) {
die("Can not connect to database: ".mysql_error());
}

mysql_select_db ($database);

for ($i=0; $i < 4; $i++)
if ($_FILES['image']['image_name'][$i] != "" && $_FILES['image']['size'][$i] > 0) {
// here you do what you do for each slot...


$tmpName = $_FILES[$column]['tmp_name'];

$fp = fopen($tmpName, 'r');
$data = fread($fp, filesize($tmpName));
$data = addslashes($data);
fclose($fp);

$id = mysql_real_escape_string($_POST[$id]);
$name = mysql_real_escape_string($_POST['image_name']);
$query = "INSERT INTO $table" .
$query .="($column, $name, $id) VALUES ('$data1', '$name', $id), ('$data2', '$name', $id ), ('$data3', '$name', $id), ('$data4', '$name', $id)";
$results = mysql_query($query, $link);

print "Thank you, Your Image has been uploaded. DONT FORGET to add the image into your profile! ";

}
else {
print "No image selected/uploaded. Make sure that you have not exceeded the max file size!";
}

mysql_close($link);
?>

  • 0

#8 Orjan

Orjan

    CC Mentor

  • Moderator
  • 2918 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#

Posted 24 February 2011 - 10:26 AM

in your if statement, you read out $data from the file, but in your inserts, you use $data1 $data2 etc. with this design, you don't do a multiple insert, you do one for each picture.
so you can shorten your SQL statement to
INSERT INTO $table ($column, $name, $id) VALUES ('$data', '$name',  $id)

  • 0

I'm a System developer at XLENT Consultant Group mainly working with SugarCRM.
Please DO NOT send mail or PM to me with programming questions, post them in the appropriate forum instead, where I and others can answer you.


#9 whitestar

whitestar

    CC Regular

  • Just Joined
  • PipPipPip
  • 33 posts

Posted 25 February 2011 - 05:33 AM

Hi

I tried it as follows but am still getting the same message. "No image selected or uploaded". Im including my add.html page as well incase there is something wrong with that maybe?

Add.html

<p align="left" class="style1 style9"><form action="insert.php" method="post" enctype="multipart/form-data" name="browse" id="brows_form">

<div align="left">
<p <span class="style10">Main Image Max File Size 330KB</span>
<input type="hidden" name="MAX_FILE_SIZE" value="852400" >
<input name="image[]" type="file" class="style1" accept="image/jpeg" />
<span class="style10">Name the Main Image</span>
<input type="text" name="image_name[]" value="" size="32" />
<input type="hidden" name="subscriber[]" value="<?php echo $row_subscriber_username['subscriber_id']; ?>" /></p>

<p <span class="style10">Image #1 Max File Size 330KB</span>
<input name="image[]" type="file" class="style1" accept="image/jpeg" />
<span class="style10">Name the Image #1</span>
<input type="text" name="image_name[]" value="" size="32" />
<input type="hidden" name="subscriber[]" value="<?php echo $row_subscriber_username['subscriber_id']; ?>" /></p>


<p <span class="style10">Image #2 Max File Size 330KB</span>
<input name="image[]" type="file" class="style1" accept="image/jpeg" />
<span class="style10">Name the Image #2</span>
<input type="text" name="image_name[]" value="" size="32" />
<input type="hidden" name="subscriber[]" value="<?php echo $row_subscriber_username['subscriber_id']; ?>" /></p>


<p <span class="style10">Image #3 Max File Size 330KB</span>
<input name="image[]" type="file" class="style1" accept="image/jpeg" />
<span class="style10">Name the Image #3</span>
<input type="text" name="image_name[]" value="" size="32" />
<input type="hidden" name="subscriber[]" value="<?php echo $row_subscriber_username['subscriber_id']; ?>" /></p>
<input type="submit" value="Upload" >
<span class="style10">Choose & Name Your Picture</span></div>
</form></p>


and insert.php
<?php

$username = "";
$password = "";
$host = "localhost";
$database = "the_secrets_out";
$table = "images";
$column = "image";
$id = "subscriber";
$name ="image_name";


$link = mysql_connect($host, $username, $password); if (!$link) {
die("Can not connect to database: ".mysql_error());
}

mysql_select_db ($database);

for ($i=0; $i < 4; $i++)
if ($_FILES['image']['image_name'][$i] != "" && $_FILES['image']['size'][$i] > 0) {
// here you do what you do for each slot...


$tmpName = $_FILES[$column]['tmp_name'];

$fp = fopen($tmpName, 'r');
$data = fread($fp, filesize($tmpName));
$data = addslashes($data);
fclose($fp);

$id = mysql_real_escape_string($_POST[$id]);
$name = mysql_real_escape_string($_POST['image_name']);
$query = "INSERT INTO $table" .
$query .= "($column, $name, $id) VALUES ('$data', '$name', $id)";
$results = mysql_query($query, $link);

print "Thank you, Your Image has been uploaded. DONT FORGET to add the image into your profile! ";

}
else {
print "No image selected/uploaded. Make sure that you have not exceeded the max file size!";
}

mysql_close($link);
?>

and thank you again for all your help!
  • 0

#10 mutago

mutago

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 478 posts

Posted 04 March 2011 - 01:58 PM

Are you using Apache server or IIS server. May be u do not set the files permission
  • 0

#11 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 04 March 2011 - 01:59 PM

You only need to set the subscriber ID once, otherwise you are putting four duplicates in to an array, there is only need for one.
<input type="hidden" name="subscriber" value="<?php echo $row_subscriber_username['subscriber_id']; ?>" />
In your insert query you also use $id for the id, but $id is the string "subscriber", did you mean to use mysql_real_escape_string($_POST['subscriber']) instead so the image belongs to them?

As for your image upload problem

Remember you are using a FOR loop, so if you only upload two images the other two will throw an error as they are empty, a you do not want this you must check if the name exists before checking the size, so the uploader will safely ignore the unused image selection forms.

You can likely put this as your checking (untested, but logic works):
   for ($i=0; $i < 4; $i++)  
if (isset($_FILES['image']['image_name'][$i])
&& !is_empty($_FILES['image']['image_name'][$i])
&& $_FILES['image']['size'][$i] > 0) {

//file size is above zero, name exists, and name is not empty
$tmpName = $_FILES[$column]['tmp_name'];

$fp = fopen($tmpName, 'r');
$data = fread($fp, filesize($tmpName));
$data = addslashes($data);
fclose($fp);

$id = mysql_real_escape_string($_POST[$id]);
$name = mysql_real_escape_string($_POST['image_name']);
$query = "INSERT INTO $table" .
$query .= "($column, $name, $id) VALUES ('$data', '$name', $id)";
$results = mysql_query($query, $link);

print "Thank you, Your Image has been uploaded. DONT FORGET to add the image into your profile! ";

}
else if(!is_empty($_FILES['image']['image_name'][$i]) &&
$_FILES['image']['size'][$i] == 0) {

//Name exists, but file size is zero, this means an improper upload
print "Image $i has not been uploaded properly."
} //no need for an else as nothing was actually uploaded in the other forms.

mysql_close($link);
?>

  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#12 whitestar

whitestar

    CC Regular

  • Just Joined
  • PipPipPip
  • 33 posts

Posted 06 March 2011 - 11:31 PM

Hello Mutago

I am using Apache, I have not checked the permissions because when I upload a single image it works fine. So I dont think the permissions would effect it on multiple images. Am I wrong in saying that?
  • 0





Also tagged with one or more of these keywords: max_file_size, mysql

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download