multiple file upload, photo album, mysql
I have a form with various fields the user need to feel out with the option of attaching number of images:
<?php
$num = 0;
while($num < $num_uploads)
{
echo '<div><input name="userfile[]" type="file" /></div>';
$num++;
}
?>
After submission it creates a table in the database, called "album" which looks something like this:
function create_album($params)
{
db_connect();
$query = sprintf("INSERT INTO albums set
albums.title = '%s',
albums.email = '%s',
albums.discuss_url = '%s',
albums.theme_id = '%s',
albums.fullname = '%s',
albums.description = '%s',
created_at = NOW()",
mysql_real_escape_string($params['title']),
mysql_real_escape_string($params['email']),
mysql_real_escape_string($params['discuss_url']),
开发者_如何学运维 mysql_real_escape_string($params['theme_id']),
mysql_real_escape_string($params['fullname']),
mysql_real_escape_string($params['description'])
);
$result = mysql_query($query);
if(!$result)
{
return false;
}
$album_id = mysql_insert_id();
return $album_id;
}
I want the files, however to go to "images" table and link to the correct album.
$create_album = create_album($_POST['album']);
mysql_query( "INSERT INTO images(`name`,`album_id`) VALUES('$newName', '$create_album')" );
I am having the problem is attaching those multiple images(the user can choose to submit one or 2, or 3 files) with one form submission to one album. Right now, if the user submit 3 files it creates 3 albums with every form submission.
Finally, this is my database structure:
CREATE TABLE `albums` (
`id` int(11) NOT NULL auto_increment,
`title` varchar(50) NOT NULL,
`fullname` varchar(40) NOT NULL,
`discuss_url` varchar(150) NOT NULL,
`email` varchar(100) NOT NULL,
`created_at` datetime NOT NULL,
`theme_id` int(11) NOT NULL,
`description` int(11) NOT NULL,
`vote_cache` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=20 ;
CREATE TABLE `images` (
`id` int(11) NOT NULL auto_increment,
`album_id` int(11) NOT NULL,
`name` varchar(30) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=18 ;
I just wanted to run quick update on this issue. Perhaps I didn't give enough info which was important to see the problem. This article explains it very well. Basically the album was created each time the file was submitted, but not the form.
So here is my form(working):
<?php if (!isset($_POST['btnSubmit'])) { ?>
<form action="index.php?view=create" method="post" enctype="multipart/form-data">
<fieldset>
<div>
<label><b>Title:</b></label>
<input name="album[title]" size="40" type="text" value="" class="textfield" />
</div>
<div>
<label><b>Fullname</b></label>
<input name="album[fullname]" size="40" type="text" value="" class="textfield" />
</div>
<div>
<label><b>Attach Photo</b> </label>
<input type="hidden" name="" value="" />
<?php
$num = 0;
while($num < $num_uploads)
{
echo '<div><input name="userfile[]" type="file" /></div>';
$num++;
}
?>
Here is php (I cut out some of it so it is easier):
//If form was submitted
if (isset($_POST['btnSubmit'])) {
create_album($_POST['album']);
$find_album_id = mysql_insert_id();
/*** check if a file has been submitted ***/
if(isset($_FILES['userfile']['tmp_name']))
{
/** loop through the array of files ***/
for($i=0; $i < count($_FILES['userfile']['tmp_name']);$i++)
{
$ext = strrchr($imgName, ".");
// then create a new random name
$newName = md5(rand() * time()) . $ext;
if (is_image_types($_FILES['userfile']['type'][$i])
and is_valid_file_size($_FILES['userfile']['size'][$i])
and is_uploaded_file($_FILES['userfile']['tmp_name'][$i])
and is_valid_width_height($_FILES['userfile']['tmp_name'][$i])
)
{
mysql_query("INSERT INTO images(name, album_id) VALUES('$newName', '$find_album_id')") ;
copy($_FILES['userfile']['tmp_name'][$i], './photos/'.$original_dir.'/' .$newName.'.jpg');
}
else
{
$warning = "Click back error uploading file.
Please make sure your file is a JPEG and less than 1MB";
}
}
}
}
break;
}
I hope it helps anyone who is looking...
精彩评论