2013-04-29 3 views
1

내 양식이 간단하고 업로드 PHP가 간단하다고 생각했지만 테스트 할 때 결과가 비정상적입니다. 나는 어떤 파일든지 어떤 크기든지 올려주기 할 수 있고 일할 것이다. 나는 그것이 특정 파일과 크기를 제한하기 위해 작성했다고 생각했는데 ... 어디가 잘못 되었나요?업 로더 파일 업로드 내용

형태 :

<form enctype="multipart/form-data" action="upload_file.php" method="POST"> 
Please choose a file: <input name="uploaded" type="file" /><br /> 
<input type="submit" value="Upload" /> 
</form> 

upload_file.php :

$target = "uploads/"; 
    $target = $target . basename($_FILES['uploaded']['name']) ; 
    $ok = 1; 
    $uploaded = $_POST['uploaded']; 
//This is our size condition 
    if ($uploaded_size > 3000){ 
     echo "Your file is too large.<br>"; 
     $ok=0; 
    } 

//This is our limit file type condition 
    if ($uploaded_type == "text/php"){ 
     echo "No PHP files are allowed for upload.<br>"; 
     $ok = 0; 
    } 

//Here we check that $ok was not set to 0 by an error 
    if ($ok == 0){ 
     Echo "Sorry your file was not uploaded"; 
    } 

//If everything is ok we try to upload it 
    else{ 
     if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)){ 
      echo "The file ". basename($_FILES['uploadedfile']['name']). " has been uploaded"; 
     } 
     else{ 
      echo "Sorry, there was a problem uploading your file."; 
     } 
    } 
+1

어디에 $ uploaded_type 및 $ uploaded_size var입니까? – Yordi

+1

여기에 정말 간단 명료 한 기사입니다. http://www.w3schools.com/php/php_file_upload.asp – Vector

+0

@Vector : 아뇨, w3schools.com을 참고로 사용하지 마십시오. 오류가 많습니다. http://w3fools.com –

답변

3

귀하의 코드는 크게 잘못된 것입니다. 아무데도 당신이 $uploaded_size$uploaded_type는, 등등 ... 그래서 코드는 아래로 비등 정의 할 수 없습니다 :

if ($uploaded_size > 3000 { 

false로 평가
if (0 > 3000) { // undefined variables are typecast to 0 

에 해당하므로 $ok 숙박 1 및 오류가 트리거되지 않습니다 .

난 당신이 파일 업로드 처리에 PHP 맨 페이지를 읽어 제안 강력히: 당신이

 if ($_FILES["file"]["size"] > 3000) ... 

처럼 사용하거나 정의 할 필요가 http://php.net/manual/en/features.file-upload.php

+0

$ uploaded_size 및 $ uploaded_type이 미리 결정된 PHP 명령이라고 생각했습니다. Errr, ... 다시 읽어 보겠습니다. –

+0

IT가 내 애널리스트 수준보다 매우 모호하거나 길인 것처럼 보입니다. 자원이 모두 발전된 경우 어떻게 배울 수 있습니까? :: Frustrated :: –

1

을 $ uploaded_size = $ _FILES [ "파일"] [ "size"]를 확인하십시오. 또한 유사하게 당신은 $ _FILES [ "파일"] [ "유형"] 사용해야합니다

 $uploaded_size = $_FILES["file"]["size"]; 
    $uploaded_type = $_FILES["file"]["type"]; 
    ... 
+0

고마워요. 변수를 만드는 방법이 정리되었습니다. –

+0

"file"이 위의 예에서 입력 한 name = "uploaded"인 경우, 맞습니까? 그래서 $ uploaded_type = $ _FILES [ "uploaded"] [ "size"] –

+0

$ uploaded_type = $ _FILES [ "uploaded"] [ "type"]; 권리. – Ekim

0

이를 시도해보십시오 die() 기능을 추가

$target = "uploads/"; 
$target = $target . basename($_FILES['uploaded']['name']) ; 
$ok = 1; 
$uploaded = $_POST['uploaded']; 
//This is our size condition 
if ($uploaded_size > 3000){ 
    echo "Your file is too large.<br>"; 
    $ok=0; 
} 

//This is our limit file type condition 
if ($uploaded_type == "text/php"){ 
    echo "No PHP files are allowed for upload.<br>"; 
    $ok = 0; 
} 

//Here we check that $ok was not set to 0 by an error 
if ($ok == 0){ 
    Echo "Sorry your file was not uploaded"; 
    die(); 
} 

//If everything is ok we try to upload it 
else{ 
    if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)){ 
     echo "The file ". basename($_FILES['uploadedfile']['name']). " has been  uploaded"; 
    } 
    else{ 
     echo "Sorry, there was a problem uploading your file."; 
     die(); 
    } 
} 

중지하는 코드를 알려줍니다. 또한 $ uploaded_type 및 $ uploaded_size var은 어디에 있습니까?

+0

감사합니다. –