2009-02-05 5 views
0

안녕하세요 저는 PHP에 익숙하지만 일부 자습서를 따르고 있지만 작동하지 않는 것 같아서이를 적용하려고했습니다. 나는이 코드를 테스트 해봤지만 어떤 점에서 작동하지만 다른 것들은 머리에 쓸 수 없다. php 파일은 업로드되지 않지만 $ ok는 spose이지만 세부 사항은 아직 datbase에 쓰여지고있다. 0 (좋지 않음)으로 설정됩니다. 여기서 일어날 일이 무엇인지 설명하는 것이 더 쉽습니다.If 문을 사용하여 PHP 파일 유효성 검사

- 사용자가 gif 또는 jpeg 파일을 업로드 할 수 있습니다. 세부 정보가 db에 추가되었습니다. - 사용자가 기본값으로 사용될 파일을 업로드 할 수 없습니다. 세부 정보가 db에 추가되었습니다. - 사용자 이 다른 파일을 업로드 할 수 없어야합니다. 레코드가 DB에 없어야하므로 사용자는 다시 시도해야합니다. 지금까지

내 코드 : 사전에

<?php 

//This is the directory where images will be saved 
$target = "images/"; 
$target = $target . basename($_FILES['photo']['name']); 
$ok=0; 


//This gets all the other information from the form 
$name= mysql_real_escape_string ($_POST['nameMember']); 
$bandMember= mysql_real_escape_string ($_POST['bandMember']); 
$pic= mysql_real_escape_string ($_FILES['photo']['name']); 
$about= mysql_real_escape_string ($_POST['aboutMember']); 
$bands= mysql_real_escape_string ($_POST['otherBands']); 

$uploaded_size=$_FILES['photo']['file_size']; 
if ($uploaded_size > 350000) 
{ 
echo "Your file is too large, 35Kb is the largest file you can upload.<br>"; 
$ok=0; 
} 
if ($uploaded_type =="text/php") 
{ 
echo "No PHP files<br>"; 
$ok=0; 
} 

if (!($uploaded_type =="image/jpeg")) 
{ 
echo "JPEG<br>";$ok=1; 
} 

if ($uploaded_type =="image/gif") 
{ 
echo "GIf<br>";$ok=1; 
} 

if (empty($pic)){ 
echo "You haven't uploaded a photo, a default will be used instead.<br/>";$ok=1;} 


if ($ok==0) 
{ 
Echo "Sorry your file was not uploaded, please try again with the correct format."; 
} 

//If everything is ok we try to upload it 
else 
{ 

// Connects to your Database 
mysql_connect("localhost", "*******", "******") or die(mysql_error()) ; 
mysql_select_db("project") or die(mysql_error()) ; 

//Writes the information to the database 
mysql_query("INSERT INTO dbProfile (nameMember,bandMember,photo,aboutMember,otherBands) 
VALUES ('$name', '$bandMember', '$pic', '$about', '$bands')") ; 

//Writes the photo to the server 
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) 
{ 

//Tells you if its all ok 
echo "The file ". basename($_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory<br/>"; 
print "<a class=\"blue\" href=\"createMember.php\">Add Another Record</a> | <a class=\"blue\" href=\"listMember.php\">Band Member Profiles and Affiliates Menu</a>"; 
} 
else { 

//Gives and error if its not 
echo "<p>If you have uploaded a picture there may have been a problem uploading your file.</p>"; 
print "<a class=\"blue\" href=\"createMember.php\">Add Another Record</a> | <a class=\"blue\" href=\"listMember.php\">Band Member Profiles and Affiliates Menu</a>"; 
} 
} 
?> 

건배.

는 "이미지/JPEG를"동일 콘텐츠 형식이없는 이미지를 업로드 할 때마다 1로 확인 평가합니다 $
if (!($uploaded_type =="image/jpeg")) 
    { 
    echo "JPEG<br>";$ok=1; 
    } 

때문에, 그래서 모든 : CHL

답변

1

오류는 아마이 경우 성명 데이터베이스에 기록됩니다.

그러나 사용자가 파일의 MIME 유형을 위조 할 수 있으므로 이처럼 MIME 유형을 확인하면 문제가 발생할 수 있습니다.

예를 들어 Imagick을 사용하여 올바른 이미지 MIME 유형을 얻을 수 있습니다. 자세한 내용을 보시려면 여기를 클릭하십시오 : http://de2.php.net/manual/en/function.imagick-identifyimage.php

편집 : 그냥 $ uploaded_type이 스크립트의 어디서나 초기화되지 않습니다. 앞서 말했듯이 $ _FILES [ 'photo'] [ 'type']을 사용하여 MIME 유형을 대략적으로 추정 할 수 있습니다.

+0

$ ok = 0이 맞습니까? 이 파일을 jpeg가 아니라면 $ ok = 1로 만듭니다. 그건 사실 내가 잘못 $ ok = 0; 나는 그것을 바꾸고 그것이 문제인지 thats를 보는 시도 할 것이다. –

+0

그리고 PHP 코드의 맨 위에 파일 유형을 확인하기 위해 $ _FILES [ 'photo'] [ 'type']을 추가해야한다고 말했습니까? –

+0

지금 건배 : D –