2010-12-20 71 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"> 
相關問題