Due-diligence done, once again I return to the experts. Please forgive my ignorance, new to all this.
I'm trying to create a form which allows users to:
- Insert the values of various form fields into a mysql database table - Easy, no issues here.
- Attach a file which is saved within the file structure (in a folder called 'documents').
- Save the file name, size, type (pdf, txt, etc.) to the same record.
After a file is uploaded the table would contain:
- id (auto incremented)
- name (text field, user generated)
- description (text field, user generated)
- File name (e.g. text.txt, added automatically on upload)
- File size (e.g. 362455[kb], added automatically on upload)
- File type (e.g. pdf, added automatically on upload)
I've successfully saved files to the folder but have not been able to make my three requirements a reality... Despite hours or troubleshoot and Googling.
The database and form are correct, the 开发者_开发技巧php file I post to is the mystery. Any ideas?
<form method="post" id="addForm" action="includes/insert_news.php">
<table class="addForm" cellspacing="0">
<tr>
<th>Name:<span class="greenText">*</span></th>
<td><input name="name" type="text" class="textBox required" value="Friendly Document Name" maxlength="80" /></td>
</tr>
<tr>
<th>Description:<span class="greenText">*</span></th>
<td><textarea name="description" class="textBox required">Document description blah blah</textarea></td>
</tr>
<tr>
<th>File:</th>
<td><input name="file" class="textBox" /></td>
</tr>
<tr>
<th> </th>
<td><input type="image" class="button" src="images/button_submit.gif" /></td>
</tr>
</table>
I am wondering you said that
I've successfully saved files to the folder but
but I think you are not getting anything in the $_FILES
because this thing is missing in your form tag
<form enctype="multipart/form-data">
Assuming that you have already added the missing thing @shakti pointed out, and you change the <input>
by adding type="file"
and since you didn't give any information about your php code, try these out:
<?php
class UploadFile{
//declare some variables in corresponding to your database field here, like fields, table name and stuffs
public function attach_file($file) {
if($file['error'] != 0) {
//do something
} else {
$this->temp_path = $file['tmp_name'];
$path_parts = pathinfo($file['name']);
$this->filename = $path_parts['extension'];// to get the filename
$this->type = $file['type'];// to get the file type
$this->size = $file['size'];// to get the size
$this->name = $name;
$this->description = $description;
}
}
public function save() {
$target_path = "/some/folder";
if(move_uploaded_file($this->temp_path, $target_path)) {
if($this->create()) {
unset($this->temp_path);
return true;
}
} else {
return false;
}
}
public function create() {
//your INSERT INTO
}
?>
and in your insert_news.php :
<?php
require_once("class/location");
if($_FILES['file']) {
$news = new UploadFile();
$news->attach_file($_FILES['main_picture'], $_POST['name'], $_POST['description']);
if($pic->save()){
//do something
}
}
?>
haven't tested this, but i hope you get the point :D
精彩评论