2012-04-16 80 views
0

我有一個簡單的html表單,它發送一個電子郵件,帶有多個附件(最多5個)。我在phpadmin保存電子郵件地址。現在我想保存多個附件。 如何保存多個附件,然後再回到其他頁面。保存多個附件並對它們進行檢索

我使用的PHPMailer發送電子郵件和附件,發送附件的循環是:

$MyUploads = array(); 
    foreach(array_keys($_FILES['attach']['name']) as $key) 
{ 
$filesize = $_FILES['attach']['size'][$key]; 
$extention = pathinfo ($_FILES['attach']['name'][$key] ,PATHINFO_EXTENSION); 
$name=$_FILES['attach']['name'][$key]; 
    $filePath = $Location . $name; 
    $tmpName = $_FILES['attach']['tmp_name'][$key]; 


if(move_uploaded_file($tmpName, $filePath)) 
    {$MyUploads[] = $Location;} 
    else 
    {$MyUploads[]='';} 
    print_r($MyUploads); 


$source = $_FILES['attach']['tmp_name'][$key]; 
$filename = $_FILES['attach']['name'][$key]; 
$mail->AddAttachment($source, $filename); 

} 

以及在數據庫中插入這些文件位置的查詢是

$insert = "INSERT INTO upload (name, type, size, file1, file2, file3, file4, file5, 
    created) VALUES 
      ( 
      '".$name."', 
      '".$extention."', 
      '".$filesize."', 
      '".$MyUploads[0]."', 
      '".$MyUploads[1]."', 
      '".$MyUploads[2]."', 
      '".$MyUploads[3]."', 
    '".$MyUploads[4]."' , 
    now() 
      )"; 
     $result = $dbLink->query($insert); 

請現在怎麼樣我可以在另一個php頁面上檢索這些文件嗎?請我是一個初學者,所以任何toturial。

+0

相關:[從數據庫檢索圖像和顯示所有頁面](http://stackoverflow.com/questions/10196929/retrieving-images-from-database-and-displaying-all-on-a-page) – hakre 2012-05-18 22:45:08

回答

2

這已經在這裏找到答案多次..請參閱

multi image upload wrong quantity on file-upload

limiting the checking condition while uploading swf files

編輯1

什麼,我認爲你的表應該像

CREATE TABLE IF NOT EXISTS `upload` (
    `uploadID` int(10) unsigned NOT NULL AUTO_INCREMENT, 
    `uploadExt` varchar(10) NOT NULL, 
    `uploadSize` varchar(50) NOT NULL, 
    `uploadPath` varchar(200) NOT NULL, 
    `uploadName` varchar(100) NOT NULL, 
    PRIMARY KEY (`uploadID`) 
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ; 

如何插入

$mysql = new mysqli("localhost","root","","test"); 
$sql = "INSERT INTO `upload` (
`uploadID` , 
`uploadExt` , 
`uploadSize` , 
`uploadPath` , 
`uploadName` 
) 
VALUES (
NULL , '%s', '%s', '%s', '%s');" 


foreach($image_you_have_uploaded as $valid) /// Am sure you know what this is 
{ 
    //Make sure you mysql_real_escape_string everthing 
    $mysql->query(sprintf($sql,$extention,$filesize,$filePath,$name)); 
} 

如何獲得圖像

$sql = "SELECT * FROM upload WHERE uploadName = '%s'" ; 
$result = $mysql->query(sprintf($sql,mysql_real_escape_string($name))); 

var_dump($result->fetch_assoc())// Output Result 

謝謝。

+0

謝謝你,我已經編輯了我的問題,在你做出修改之後。請你指定我現在可以做些什麼來檢索他們 – 2012-04-16 11:11:39

+0

我不同意你如何插入你的圖片..我會更新我認爲最好的解決方案..堅持 – Baba 2012-04-16 11:22:50

+0

看到更新的代碼 – Baba 2012-04-16 11:50:39

相關問題