2013-02-13 2 views
0

"stack"에서이 문제를 검색했지만 유사한 문제가 발견되지 않았습니다.phpl의 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 파일)의 경우성공을 인쇄하지만 이미지 파일 만 업로드됩니다.

나는이 문제가 무엇인지 된 .txt 을 위해 잘 작동

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

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

조건을 대체? 내가 잘못하고있는 곳에서?

+1

시도는 Word 파일에 대한'$ _FILES [ "이력서"] [ "유형"]'의 값을 에코합니다. 검사 대상 MIME과 다를 수 있습니다. – Raptor

+0

else { // echo "잘못된 파일"; }이 주석 처리를 제거하고 시도하십시오 –

+0

그것은 어떤 조건에서도 없기 때문에 항상 성공을 인쇄합니다. –

답변

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; 

    } 

?> 
관련 문제