2013-02-13 50 views
0

我在「堆棧」上搜索了這個問題,但是對於類似的問題,我沒有找到解決方案。在PHP中無法正常工作的doc&docx文件

我有一個代碼在兩個不同的文件夾中上傳兩個文件(圖像&文本)。

 $file_path = "users/".$uname."/dp/"; 
     $file_path2 = "users/".$uname."/resume/"; 

     $q=mkdir("users/".$uname."/dp/", 0777, $recursive=true); 
     $r=mkdir("users/".$uname."/resume/", 0777, $recursive=true); 
     if($q && $r) 
     { 
      $targate = $file_path.basename($_FILES['dp']['name']); 
      //echo $targate ;die; 
      if ((($_FILES['dp']["type"] == "image/gif") || ($_FILES['dp']["type"] == "image/jpeg") || ($_FILES['dp']["type"] == "image/png") || 
       ($_FILES['dp']["type"] == "image/jpg")) && ($_FILES['dp']["size"] < 20000)) 
      { 
       if ($_FILES['dp']["error"] > 0) 
       { 
        echo "Return Code: " . $_FILES['dp']["error"] . " "; 
       } 
       else 
       { 
        move_uploaded_file($_FILES['dp']["tmp_name"], $targate); 
       } 
      } 
      else 
      { 
       //echo "Invalid file"; 
      } 

      $targate2 = $file_path2.basename($_FILES['resume']['name']); 
      //echo $targate2 ;die; 
      if ((($_FILES["resume"]["type"] == "text/plain") || ($_FILES["resume"]["type"] == "application/msword") 
      || ($_FILES["resume"]["type"] == "application/vnd.openxmlformats-officedocument.wordprocessingml.document")) && $_FILES['resume']["size"] < 20000) 
      { 
       if ($_FILES['resume']["error"] > 0) 
       { 
        echo "Return Code: " . $_FILES['resume']["error"] . " "; 
       } 
       else 
       { 
        move_uploaded_file($_FILES['resume']["tmp_name"], $targate2); 

       } 
      } 
      else 
      { 
       //echo "Invalid file"; 
      } 

      echo "success";die; 
     } 
     else{ echo "fail";die;} 

對於所有類型的圖像,它的工作正常。但在文本文件的情況下(doc & docx文件)它打印成功但只有圖像文件正在上傳。

當我更換此

if ((($_FILES["resume"]["type"] == "text/plain") 
      || ($_FILES["resume"]["type"] == "application/msword") 
      || ($_FILES["resume"]["type"] == "application/vnd.openxmlformats-officedocument.wordprocessingml.document")) && $_FILES['resume']["size"] < 20000) 

if (($_FILES["resume"]["type"] == "text/plain") 
      && $_FILES['resume']["size"] < 20000) 

條件這個工作得很好.TXT 問題是什麼?我在哪裏做錯了?

+1

嘗試呼應'$ _FILES [ 「簡歷」] [ 「型」]'爲你的Word文件的價值。它可能不是你正在檢查的MIME。 – Raptor 2013-02-13 04:52:56

+0

else { // echo「Invalid file」; }取消註釋並嘗試 – 2013-02-13 05:01:39

+0

它將始終打印成功,因爲它不處於任何狀態。 – 2013-02-13 05:03:07

回答

1

步驟1

創建一個名爲index.html的html文件,並將以下代碼粘貼到其中。

<html> 

<body> 

<form enctype="multipart/form-data" method="POST" action="upload.php">This is the code for html: 

<table border="0"> 

<tbody> 

<tr> 

<td align="left">File:</td> 

<td><input accept="doc/docx" name="filename" size="40" type="file" /></td> 

</tr> 

<tr> 

<td><input name="Upload" type="submit" value="Upload" /></td> 

</tr> 

</tbody></table> 

</form> 

</body> 

</html> 

步驟2

創建一個名爲upload.php的一個PHP文件,並粘貼以下代碼。

<?php 

//if we clicked on Upload button 

if($_POST['Upload'] == 'Upload') 

    { 

    //make the allowed extensions 

    $goodExtensions = array(

    '.doc', 

    '.docx', 

); 

    $error=''; 

    //set the current directory where you wanna upload the doc/docx files 

    $uploaddir = './ '; 

    $name = $_FILES['filename']['name'];//get the name of the file that will be uploaded 

    $min_filesize=10;//set up a minimum file size(a doc/docx can't be lower then 10 bytes) 

    $stem=substr($name,0,strpos($name,'.')); 

    //take the file extension 

    $extension = substr($name, strpos($name,'.'), strlen($name)-1); 

    //verify if the file extension is doc or docx 

    if(!in_array($extension,$goodExtensions)) 

    $error.='Extension not allowed<br>'; 

echo "<span> </span>"; //verify if the file size of the file being uploaded is greater then 1 

    if(filesize($_FILES['filename']['tmp_name']) < $min_filesize) 

    $error.='File size too small<br>'."\n"; 

    $uploadfile = $uploaddir . $stem.$extension; 

$filename=$stem.$extension; 

if ($error=='') 

{ 

//upload the file to 

if (move_uploaded_file($_FILES['filename']['tmp_name'], $uploadfile)) { 

echo 'File Uploaded. Thank You.'; 

} 

} 

else echo $error; 

} 

?> 
0
// Above answer is right but for perfection need some slight changes....... 
// thanx... 
//It will store particular document in a folder 




<?php 

//if we clicked on Upload button 

if($_POST['Upload'] == 'Upload') 

    { 

    //make the allowed extensions 

    $goodExtensions = array('.doc', '.docx',); 

    $error=''; 

    //set the current directory where you wanna upload the doc/docx files 

    $uploaddir = 'upload./ '; 

    $name = $_FILES['filename']['name'];//get the name of the file that will be uploaded 

    $min_filesize=10;//set up a minimum file size(a doc/docx can't be lower then 10 bytes) 

    $stem=substr($name,0,strpos($name,'.')); 

    //take the file extension 

    $extension = substr($name, strpos($name,'.'), strlen($name)-1); 

    //verify if the file extension is doc or docx 

    if(!in_array($extension,$goodExtensions)) 

    $error.='Extension not allowed<br>'; 

echo "<span> </span>"; //verify if the file size of the file being uploaded is greater then 10 

    if(filesize($_FILES['filename']['tmp_name']) < $min_filesize) 

    $error.='File size too small<br>'."\n"; 
    else 

    $uploadfile = $uploaddir . $stem.$extension; 

    $filename=$stem.$extension; 

    if ($error=='') 

    { 

//upload the file to 

    if (move_uploaded_file($_FILES['filename']['tmp_name'], $uploadfile)) { 

    echo 'File Uploaded. Thank You.'; 

    } 

    } 

    else echo $error; 

    } 

?>