2010-12-20 73 views
0

尽职尽责,我再次回到了专家。请原谅我的无知,这是新的。上传一个文件到文件结构,形成数据到数据库

我试图创建一个表单,允许用户:

  • 插入各种表单字段的值到MySQL数据库表 - 简单,这里没有问题。
  • 附加保存在文件结构中的文件(位于名为'documents'的文件夹中)。
  • 将文件名,大小,类型(pdf,txt等)保存到同一记录中。

文件上传后,该表将包含:

  • ID(自动递增)
  • 名称(文本字段,用户生成的)
  • 描述(文本字段,用户生成的)
  • 文件名称(例如text.txt,上传时自动添加)
  • 文件大小(例如362455 [kb],在上传时自动添加)
  • 文件类型(例如pdf,上传时自动添加)

我已经成功地将文件保存到文件夹,但一直没有能够使我的三个要求成为现实...尽管数小时或排除故障和谷歌搜索。

数据库和表单都正确,我发布的php文件是个谜。有任何想法吗?

<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>&nbsp;</th> 
    <td><input type="image" class="button" src="images/button_submit.gif" /></td> 
    </tr> 
</table> 

+0

你可以发布includes/insert_news.php文件的代码吗? – ughoavgfhw 2010-12-20 06:44:55

回答

0

假设你已经添加@shakti指出缺少的东西,您可以通过添加type="file"改变<input>因为你没有给你的PHP代码的任何信息,尝试这些了:

<?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 
} 
?> 

,并在您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 
    } 
} 
?> 

没有测试过这一点,但我希望你明白了吧:d

1

我不知道你说的那个

我已经成功地保存文件到 文件夹,但

,但我觉得你是不是在得到任何东西$_FILES,因为这件东西在你的form tag

<form enctype="multipart/form-data"> 
相关问题