PDA

View Full Version : upload multiple files


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?

abduraooft
07-01-2008, 09:14 AM
Are they getting uploaded to the server?

Bob42
07-01-2008, 09:16 AM
Are they getting uploaded to the server?

Trying to store them in a database. Do they need to be uploaded to the server first?

WyrmFyre
07-01-2008, 09:23 AM
Trying to store them in a database. Do they need to be uploaded to the server first?

Yes they do. The rest of your code looks spot on but your missing the file upload functions. Heres a great tutorial from Tizag that will help.

http://www.tizag.com/phpT/fileupload.php

abduraooft
07-01-2008, 09:51 AM
And it's better to store the files in folders and save their name/path in the database.

Bob42
07-01-2008, 10:09 AM
And it's better to store the files in folders and save their name/path in the database.

Even for stuff like file attachments and avatars? What if two people upload two separate images but with the same name? Wouldn't one of the files be overwritten?

spoot
07-01-2008, 10:34 AM
Even for stuff like file attachments and avatars? What if two people upload two separate images but with the same name? Wouldn't one of the files be overwritten?

That depends on how your hosting works, it'll either overwrite or throw up some kind of error, unless your scripts somehow detect the problem first.

abduraooft
07-01-2008, 10:41 AM
We need to adopt some naming convension/ directory structure to store files. Say, prefix the user_id or some unique value with the name of that file.

Bob42
07-01-2008, 10:41 PM
We need to adopt some naming convension/ directory structure to store files. Say, prefix the user_id or some unique value with the name of that file.

Well, the uploaded files script is part of a news system I am creating, so I think it would be best to associate each uploaded file with the ID of a news entry.

What I decided to do is generate a random number and input it in a hidden <input> box in the form. Then I could just use $_POST to take that value and write it to the uploaded file's name. Something like:

<input type="hidden" name="news_id" value="the unique ID goes here" />

Here's my function. The only problem is that the same number could be generated twice. I want an unique number for each news entry. How can I do that?


function generateNewsID()
{
$random_number = rand(1, 10000);
$random = $random_number;
}

skmd
07-02-2008, 12:18 AM
you can use the auto increament for the database !!