Php Multiple File Upload with Arrays

Hey guys,

I have been running tutorials for PHP and I am getting a good handle on things, and how they work.

I am currently working with file uploads and storing the image in the database. My code works fine, however, how would I add a array to loop through 4 images, and upload those to the database.

Form

<form action="image.php" method="post" enctype="multipart/form-data">
File: <input name="image" type="file" /><br />
<input type="submit" value="upload" />
</form>

[php]

<?php // Connect to the database mysql_connect('localhost','root','root') or die (mysql_error()); mysql_select_db('espada') or die (mysql_error()); //File Properties $file = $_FILES['image']['tmp_name']; if (!isset($file)){ echo 'Please select an image'; }else{ $image = addslashes(file_get_contents($_FILES['image']['tmp_name'])); $image_name = addslashes($_FILES['image']['name']); $image_size = getimagesize($_FILES['image']['tmp_name']); if($image_size == FALSE){ echo 'Thats not an image!'; }else{ if($insert = mysql_query("INSERT INTO model_login VALUES('1', 'NULL', 'NULL', '$image_name', '$image', 'NULL')")){ echo "Problem uploading image."; }else{ echo 'Success'; } } } ?>

[/php]

Overall, as I progress more I think it will be better practice to upload the images to the server, and storing the image location in the sql. My overall objective is to make a image gallery for users.

Best Regards,
Josh

[php]
if($insert = mysql_query(“INSERT INTO model_login VALUES(‘1’, ‘NULL’, ‘NULL’, ‘$image_name’, ‘$image’, ‘NULL’)”)){
echo “Problem uploading image.”;
}else{
echo ‘Success’;
}
[/php]
I can see a couple problems here. Firstly:
[php]if($insert = mysql_query(“INSERT INTO model_login VALUES(‘1’, ‘NULL’, ‘NULL’, ‘$image_name’, ‘$image’, ‘NULL’)”))[/php]
This query should have a ; at the end and NULL doesn’t need ’ symbols. Integers also don’t need them.
INSERT INTO model_login VALUES(1, NULL, NULL, ‘$image_name’, ‘$image’, NULL);

Now if you look at that particular if statement, you are telling it to say there is an error, IF the query is successful. It should be
[php]
if($insert = mysql_query(“INSERT INTO model_login VALUES(‘1’, ‘NULL’, ‘NULL’, ‘$image_name’, ‘$image’, ‘NULL’)”)){
echo “Success”;
}else{
echo ‘There was a problem uploading the image’;
}
[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service