Bob42
07-01-2008, 08:46 AM
I'm having some trouble with my upload files script. When I press the submit button, the files don't get added to the database. I don't know what's wrong.
HTML:
<form action="{$smarty.server.PHP_SELF}" method="post" enctype="multipart/form-data">
<div id="image"><input type="file" name="image[]" size="30" /></div>
{literal}
<script type="text/javascript">
function addmore()
{
var txt = "<br /><input type=\"file\" name=\"image[]\" size=\"30\">";
document.getElementById("image").innerHTML += txt;
}
</script>
{/literal}
<input type="button" onclick="addmore()" value="Upload An Image" class="submit" />
<input type="submit" name="addnews" value="Submit" class="submit" />
The {literal}{/literal} is just smarty stuff. You can ignore that. All it does is make sure that the javascript works.
PHP:
if (isset($_POST['addnews'])) {
//Attachments
for ($i = 0; $i < count($_FILES['image']['name']); $i++)
{
$image = array
(
'name' => $_FILES['avatar']['name'][$i], //Image name
'tmp_name' => $_FILES['avatar']['tmp_name'][$i], //The image's temporary name
'type' => $_FILES['avatar']['type'][$i], //The file type
'size' => $_FILES['avatar']['size'][$i] //The file size
);
$image_types = array //Image types
(
'image/bmp',
'image/jpeg',
'image/jpg',
'image/png',
'image/gif'
);
if (is_uploaded_file($image['tmp_name']))
{
$extract = fopen($image['tmp_name'], 'r');
$content = fread($extract, $image['size']);
$content = addslashes($content);
fclose($extract);
}
if (in_array(strtolower($image['type']), $image_types)) //Checks that the image is one of the listed image types
{
//Place image in database
$insert = mysql_query("INSERT INTO attachments (image_type, image, image_size, image_name, image_date) VALUES ('{$image['type']}', '{$content}', '{$image['size']}', '{$image['name']}', NOW())");
}
}
}
SQL:
CREATE TABLE attachments (
image_id int(10) unsigned NOT NULL auto_increment,
image_type varchar(50) NOT NULL default '',
image longblob NOT NULL,
image_size bigint(20) NOT NULL default '0',
image_name varchar(255) NOT NULL default '',
image_date datetime NOT NULL default '0000-00-00 00:00:00',
UNIQUE KEY image_id (image_id)
);
What could be the problem?
HTML:
<form action="{$smarty.server.PHP_SELF}" method="post" enctype="multipart/form-data">
<div id="image"><input type="file" name="image[]" size="30" /></div>
{literal}
<script type="text/javascript">
function addmore()
{
var txt = "<br /><input type=\"file\" name=\"image[]\" size=\"30\">";
document.getElementById("image").innerHTML += txt;
}
</script>
{/literal}
<input type="button" onclick="addmore()" value="Upload An Image" class="submit" />
<input type="submit" name="addnews" value="Submit" class="submit" />
The {literal}{/literal} is just smarty stuff. You can ignore that. All it does is make sure that the javascript works.
PHP:
if (isset($_POST['addnews'])) {
//Attachments
for ($i = 0; $i < count($_FILES['image']['name']); $i++)
{
$image = array
(
'name' => $_FILES['avatar']['name'][$i], //Image name
'tmp_name' => $_FILES['avatar']['tmp_name'][$i], //The image's temporary name
'type' => $_FILES['avatar']['type'][$i], //The file type
'size' => $_FILES['avatar']['size'][$i] //The file size
);
$image_types = array //Image types
(
'image/bmp',
'image/jpeg',
'image/jpg',
'image/png',
'image/gif'
);
if (is_uploaded_file($image['tmp_name']))
{
$extract = fopen($image['tmp_name'], 'r');
$content = fread($extract, $image['size']);
$content = addslashes($content);
fclose($extract);
}
if (in_array(strtolower($image['type']), $image_types)) //Checks that the image is one of the listed image types
{
//Place image in database
$insert = mysql_query("INSERT INTO attachments (image_type, image, image_size, image_name, image_date) VALUES ('{$image['type']}', '{$content}', '{$image['size']}', '{$image['name']}', NOW())");
}
}
}
SQL:
CREATE TABLE attachments (
image_id int(10) unsigned NOT NULL auto_increment,
image_type varchar(50) NOT NULL default '',
image longblob NOT NULL,
image_size bigint(20) NOT NULL default '0',
image_name varchar(255) NOT NULL default '',
image_date datetime NOT NULL default '0000-00-00 00:00:00',
UNIQUE KEY image_id (image_id)
);
What could be the problem?