I am trying to make a file upload script for my website but I can't figure out how to get the name
and size
values out my array (I am not very good with arrays).
I can get it to work with uploading a single file, but how can I get it to loop?
I need to upload it to my server, and it's also created an entry in the database.
Here is my code...
HTML:
<form method="post" action="" enctype="multipart/form-data">
<textarea name="reply_message"></textarea>
<input name="attachments[]" type="file" size="20">
<input type="submit" name="ReplyForm" value="Send">
</form>
PHP:
if(!empty($_POST['reply_message']))
{
$reply_messages = $_POST['reply_message'];
$database->query('INSERT INTO email_response (email_id, message) VALUES (?, ?)', array($email_id, $reply_message));
if(isset($_FILES['attachments']))
{
require("upload.class.php");
$upload = new upload();
$last_email_response_id = $database->lastInsertId();
$attachments = $_FILES['attachments'];
foreach($attachments as $key => $value)
{
print_r($attachments);
$database->query('INSERT INTO email_attachments (email_id, reply_id, file_name, created) VALUES (?, ?, ?, NOW())', array($email_id, $last_email_response_id, $attachments['name'][0]));
$last_attachment_id = $database->lastInsertId();
$upload->set('attachments', ATTACHMENTS.$reply_result->sender_email.'/'.$email_id.'/'.$last_attachment_id);
$upload->upload();
}
}
}
Array (with uploading two files):
Array (
[name] => Array (
[0] => linkdownarrow.gif
[1] => overlay-button.png
)
[type] => Array (
[0] => image/gif
[1] => image/png
)
[tmp_name] => Array (
[0] => F:\xampp\tmp\phpFEC0.tmp
[1] => F:\xampp\tmp\phpFEC1.tmp
)
[error] => Array (
[0] => 0
[1] => 0
)
[size] => Array (
[0] => 63
[1] => 135
)
)