2015-02-09 77 views
2

我寫了一個PHP代碼上傳.pdf以及.docx文件到我的WWW目錄(WAMP服務器)中創建的文件夾 這裏是一塊上傳文件的代碼:PHP代碼從我的Wamp服務器下載的文件夾下載.pdf&.docx文件

<?php 
if ((($_FILES["file"]["type"] == "application/vnd.openxmlformats- officedocument.word") || ($_FILES["file"]["type"] == "application/pdf") 
|| ($_FILES["file"]["type"] == "application/x-pdf") || ($_FILES["file"] ["type"] == "application/acrobat") 
|| ($_FILES["file"]["type"] == "applications/vnd.pdf") || ($_FILES["file"]["type"] == "text/pdf") || ($_FILES["file"]["type"] == "text/x-pdf")) 
&& ($_FILES["file"]["size"]<5000000)) 
{ 
if ($_FILES["file"]["error"] > 0) 
    { 
    echo "Error: " . $_FILES["file"]["error"] . "<br/>"; 
    } 
else 
{  
if (file_exists("CVs/".$_FILES["file"]["name"])) 
    { 
    echo $_FILES["file"]["name"] ."already exists"; 
    } 
else 
    { 
    move_uploaded_file($_FILES["file"]["tmp_name"], 
    "CVs/" . $_FILES["file"]["name"]); 
$cv=$_FILES["file"]["name"]; 

上述代碼適用於上傳.pdf文件,但不能上傳.docx文件。我需要知道我的錯誤才能上傳.docx文件。

我又寫了一個PHP代碼來下載上傳的.pdf文件。 我的代碼看起來如下:

<?php 
    $connect=mysql_connect("localhost","root",""); 
     if(!$connect) die("Server failed to connect ".mysql_error()); 
     mysql_select_db("geosas_rosterofexperts") or die(mysql_error()); 
     $result=mysql_query("SELECT * FROM user_table"); 
     while($row=mysql_fetch_array($result)) 
     { 
       $id=$row["id"]; 
       $fname=$row["fname"]; 
       $mname= $row["mname"]; 
       $lname=$row["lname"]; 
       $gender=$row["gender"]; 
       $address=$row["address"]; 
       $country=$row["country"]; 
       $mobile=$row["mobile"]; 
       $email=$row["email"]; 
       $altmail=$row["altmail"]; 
       $education=$row["education"]; 
       $field=$row["field"]; 
       $certification=$row["award"]; 
       $cv=$row["cv"]; 
       echo "".$id.""; 
       echo "<table width='100%' border='1px'> 
       <tr style='Background-color:#CCCCCC;'> 
       <td><a href='file_update.php?id=$id'>Update</a></td> 
       <td><a href='file_delete.php?id=$id'>Delete</a></td> 
       </tr> 

       <tr> 
       <td> <p style='color:#FF3300;'>First name</p> ".$fname."</td> 
       <td> <p style='color:#FF3300;'>Middle name</p> ".$mname."</td> 
       </tr> 

       <tr> 
       <td> <p style='color:#FF3300;'>Last name</p> ".$lname."</td> 
       <td> <p style='color:#FF3300;'>Gender</p> ".$gender."</td> 
       </tr> 
       <tr> 
       <td colspan=2> <p style='color:#FF3300;'>Address</p> ".$address."</td> 
       </tr> 

       <tr> 
       <td> <p style='color:#FF3300;'>Country</p> ".$country."</td> 
       <td> <p style='color:#FF3300;'>Mobile</p> ".$mobile." </td> 
       </tr> 
       <tr> 
       <td> <p style='color:#FF3300;'>Email</p> ".$email." </td> 
       <td> <p style='color:#FF3300;'>Alt.Email</p> ".$altmail."</td> 
       </tr> 
       <tr> 
       <td> <p style='color:#FF3300;'>Education</p> ".$education."</td> 
       <td> <p style='color:#FF3300;'>Field</p> ".$field."</td> 
       </tr> 
       <tr> 
       <td><p style='color:#FF3300;'>Specialization</p> ".$certification."</td>  
       <td><a href='CVs/'".$cv."'>CV</a></td> 
       </tr> 
       </table> 
       ";   
     } 
?> 

此鏈接帶我到包含上傳的文件,但我需要的是根據ID帶我去確切的文件並下載文件時,我點擊一個文件夾「簡歷」鏈接。所以,請幫我

回答

0

1S前面回答

取代你MSWORD匹配代碼:

($_FILES["file"]["type"] == "application/msword") 

第二個答案

要強制下載,而不是瀏覽您的可以使用下載屬性。

<a href='#' download='your_cutom_file_name'>Download</a> 

注意:IE和safari不支持。

,或者如果你想要更長的路使用此代碼:

<?php 

$file = $_GET['file']; 

download_file($file); 

function download_file($fullPath){ 


    if(headers_sent()) 
    die('Headers Sent'); 

    // Required for some browsers 
    if(ini_get('zlib.output_compression')) 
    ini_set('zlib.output_compression', 'Off'); 

// File Exists? 
if(file_exists($fullPath)){ 

// Parse Info/Get Extension 
$fsize = filesize($fullPath); 
$path_parts = pathinfo($fullPath); 
$ext = strtolower($path_parts["extension"]); 

// Determine Content Type 
switch ($ext) { 
    case "pdf": $ctype="application/pdf"; break; 
    case "exe": $ctype="application/octet-stream"; break; 
    case "zip": $ctype="application/zip"; break; 
    case "doc": $ctype="application/msword"; break; 
    case "xls": $ctype="application/vnd.ms-excel"; break; 
    case "ppt": $ctype="application/vnd.ms-powerpoint"; break; 
    case "gif": $ctype="image/gif"; break; 
    case "png": $ctype="image/png"; break; 
    case "jpeg": 
    case "jpg": $ctype="image/jpg"; break; 
    default: $ctype="application/force-download"; 
}