2013-05-14 6 views
-1

PHP를 사용하여 파일을 업로드하려고합니다. 그러나 다음 오류가 생성됩니다. Error: A problem occurred during file upload!. 우분투 OS를 사용하고 있습니다. 그 파일을 저장하려고하는 동안 오류가 발생했다고 생각합니다. 다음 코드를 사용했습니다.는 PHP를 사용하여 자바 스크립트에서 파일을 업로드 할 수 없습니다.

<html> 
<body> 
    <form enctype="multipart/form-data" action="upload.php" method="post"> 
    <input type="hidden" name="MAX_FILE_SIZE" value="1000000" /> 
    Choose a file to upload: <input name="uploaded_file" type="file" /> 
    <input type="submit" value="Upload" /> 
    </form> 
</body> 
</html> 


<?php 
//Сheck that we have a file 
if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0)) { 
    //Check if the file is JPEG image and it's size is less than 350Kb 
    $filename = basename($_FILES['uploaded_file']['name']); 
    $ext = substr($filename, strrpos($filename, '.') + 1); 
    if (($ext == "jpg") && ($_FILES["uploaded_file"]["type"] == "image/jpeg") && 
    ($_FILES["uploaded_file"]["size"] < 350000)) { 
    //Determine the path to which we want to save this file 
     $newname = dirname(__FILE__).'/upload/'.$filename; 
     //Check if the file with the same name is already exists on the server 
     if (!file_exists($newname)) { 
     //Attempt to move the uploaded file to it's new place 
     if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$newname))) { 
      echo "It's done! The file has been saved as: ".$newname; 
     } else { 
      echo "Error: A problem occurred during file upload!"; 
     } 
     } else { 
     echo "Error: File ".$_FILES["uploaded_file"]["name"]." already exists"; 
     } 
    } else { 
    echo "Error: Only .jpg images under 350Kb are accepted for upload"; 
    } 
} else { 
echo "Error: No file uploaded"; 
} 
?> 

이 코드의 문제점은 무엇입니까? 폴더에 접근하기위한 파일 퍼미션 때문에 이것이 무엇입니까?

+0

PHP 매뉴얼에서 '또한 경고가 발행됩니다 .'. 오류가 기록 된 경우 게시하십시오. –

+0

$ _FILES 배열에 오류 코드 –

+1

이 포함됩니다. 권한에 문제가있는 것 같습니다. 또한 경로'dirname (__ FILE __). '/ upload /'가 실제로 존재하는지 확인하십시오. 더하기 -'dirname (__ FILE __)'이 예상 한 것과 정확히 일치하는지 확인하십시오. –

답변

0

어디에서 오류가 발생하는지 찾으십시오.

<?php 

$msg = ''; 
$upload_key = 'uploaded_file'; 

if (isset($_FILES[$upload_key])) { 

    try { 

     $error = $_FILES[$upload_key]['error']; 
     switch ($error) { 
      case UPLOAD_ERR_INI_SIZE: 
       throw new Exception('Exceeded upload_max_filesize'); 
      case UPLOAD_ERR_FORM_SIZE: 
       throw new Exception('Exceeded MAX_FILE_SIZE'); 
      case UPLOAD_ERR_PARTIAL: 
       throw new Exception('Incomplete file uploaded'); 
      case UPLOAD_ERR_NO_FILE: 
       throw new Exception('No file uploaded'); 
      case UPLOAD_ERR_NO_TMP_DIR: 
       throw new Exception('No tmp directory'); 
      case UPLOAD_ERR_CANT_WRITE: 
       throw new Exception('Can\'t write data'); 
      case UPLOAD_ERR_EXTENSION: 
       throw new Exception('Extension error'); 
     } 

     $finfo = new finfo(FILEINFO_MIME); 
     $name  = $_FILES[$upload_key]['name']; 
     $tmp_name = $_FILES[$upload_key]['tmp_name']; 
     $size  = $_FILES[$upload_key]['size']; 

     if ($size > 350000) 
      throw new Exception('Exceeded 350KB limit'); 
     if (!is_uploaded_file($tmp_name)) 
      throw new Exception('Not an uploaded file'); 

     $type = $finfo->file($tmp_name); 

     if ($type === false) 
      throw new Exception('Failed to get MimeType'); 
     if ($type !== 'image/jpeg; charset=binary') 
      throw new Exception('Only JPEG files available'); 

     $new_name = dirname(__FILE__).'/upload/'.$name; 

     if (is_file($new_name)) 
      throw new Exception("The file {$new_name} already exists"); 

     if (!move_uploaded_file($tmp_name,$new_name)) 
      throw new Exception('Failed to move uploaded file'); 

     $msg = "File successfully uploaded as {$new_name}"; 

    } catch (Exception $e) { 

     $msg = 'Error: '.$e->getMessage(); 

    } 

} 
?> 
<!DOCTYPE html> 
<html> 
<head> 
<title>Uploader</title> 
</head> 
<body> 
<form enctype="multipart/form-data" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="post"> 
<div><input type="hidden" name="MAX_FILE_SIZE" value="1000000" /></div> 
<div>Choose a file to upload: <input name="uploaded_file" type="file" /></div> 
<div><input type="submit" value="Upload" /></div> 
</form> 
<p> 
<?php echo $msg.PHP_EOL; ?> 
</p> 
</body> 
</html> 
+0

오류 : 업로드 된 파일을 이동하지 못했습니다. -이 메시지는 현재 표시됩니다. –

+0

경고가 발생하지 않았습니까? – mpyw

+1

매뉴얼에 따르면 : * filename *이 유효한 업로드 파일이 아니면 액션이 일어나지 않고 move_uploaded_file()은 FALSE를 반환합니다. * filename *이 유효한 업로드 파일이지만 어떤 이유로 이동 될 수 없으면 아무런 조치도 취하지 않으며'move_uploaded_file()'은 * FALSE *를 반환합니다. 또한 ** 경고 **가 발행됩니다. – mpyw

관련 문제