2012-01-19 15 views
0

Iアップロードスクリプトとして使用しています次のコードは、PHPのアップロードスクリプト - ファイルサイズやファイルタイプの問題

$allowed_filetypes = array('.jpg', '.jpeg', '.gif', '.bmp', '.png'); 
$max_filesize = 262144; 
$upload_normal_path = '../uploads/normal/'; 
$upload_thumb_path = '../uploads/thumbnail/'; 

if(isset($_POST['Submit'])) 
{ 
$filename = $_FILES['image']['name']; 
$filesize = $_FILES['image']['size']; 
$fileext = substr($filename, strpos($filename,'.'), strlen($filename)-1); 

if(!in_array($fileext, $allowed_filetypes)){ 
$upload_status = "The file you attempted to upload is not allowed."; 
} 

if($filesize > $max_filesize){ 
$upload_status = "The file you attempted to upload is too large."; 
} 

$image_name = time().$fileext; 
$newname = $image_name; 

$moved = move_uploaded_file($_FILES['image']['tmp_name'],$upload_normal_path . $newname); 

if(!$moved){ 
$upload_status = 'There was an error during the file upload. Please try again.'; 
} else { 
$upload_status = 'Your file upload was successful, view the file <a href="' . $upload_normal_path . $newname . '" title="Your File">here</a>'; 
} 
} 

スクリプト自体は時々動作しているようですが、それは状況IFの一部を飛ばしているように見えるがあり、 ELSE。 たとえば、ファイルサイズが$ filesizeより大きい場合、「アップロードしようとしたファイルが大きすぎます」という正しい$ upload_statusが表示されず、代わりに「There wereファイルのアップロード中にエラーが発生しました。もう一度お試しください。 "また、いつか私はいくつかのMP3またはHTMLファイルをアップロードすることができます。つまり、全体(!in_array($ fileext、$ allowed_filetypes))をスキップしています。

これらの問題を引き起こしている可能性のあることと解決方法。 よろしくお願いします。

[解決済み] お時間をいただきありがとうございます。ご回答いただきありがとうございます。 あなたの答えを見て、私はそれが私がそれが必要なものを正確に行うまで、私はいくつかのコードをクリアしました。

ここに私の現在のコードのコピーがあります。それは、このような問題に遭遇する可能性のある同僚の開発者を助けることを期待しています。

よろしく

現在の作業コード:

function make_thumb($img_name,$filename,$new_w,$new_h) 
{ 

$ext=getExtension($img_name); 

if(!strcmp("jpg",$ext) || !strcmp("jpeg",$ext)) 
$src_img=imagecreatefromjpeg($img_name); 

if(!strcmp("png",$ext)) 
$src_img=imagecreatefrompng($img_name); 


$old_x=imageSX($src_img); 
$old_y=imageSY($src_img); 

$ratio1=$old_x/$new_w; 
$ratio2=$old_y/$new_h; 
if($ratio1>$ratio2) { 
$thumb_w=$new_w; 
$thumb_h=$old_y/$ratio1; 
} 
else { 
$thumb_h=$new_h; 
$thumb_w=$old_x/$ratio2; 
} 

$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h); 

imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y); 

if(!strcmp("png",$ext)) 
imagepng($dst_img,$filename); 
else 
imagejpeg($dst_img,$filename); 

imagedestroy($dst_img); 
imagedestroy($src_img); 
} 

function getExtension($str) { 
$i = strrpos($str,"."); 
if (!$i) { return ""; } 
$l = strlen($str) - $i; 
$ext = substr($str,$i+1,$l); 
return $ext; 
} 

$upload_status = ""; 
$max_filesize = 2097152; 
$error = 0; 
$allowed_filetypes = array('jpg', 'jpeg', 'png', 'JPG', 'JPEG', 'PNG'); 

if(isset($_POST['Submit'])) 
{ 

$image = $_FILES['image']['name']; 

if ($image) 
{ 

$filename = stripslashes($_FILES['image']['name']); 
$sizekb = filesize($_FILES['image']['tmp_name']); 

$extension = getExtension($filename); 
$extension = strtolower($extension); 

    if(!in_array($extension, $allowed_filetypes)){ 
    $upload_status = "<div id='file-upload'><div class='upload-bar-error'><span class='upload-error'>The file extension is not supported.</span></div></div>"; 
    $error = 1; 
    } 

    if(isset($_SERVER['CONTENT_LENGTH']) && $_SERVER['CONTENT_LENGTH']> $max_filesize){ 
    $upload_status = "<div id='file-upload'><div class='upload-bar-error'><span class='upload-error'>The file size has extended the size limit.</span></div></div>"; 
    $error = 1; 
    } 

    if($error == 0){ 
    $image_name=time().'.'.$extension; 

    $newname="../uploads/normal/".$image_name; 
    $newname_db = "uploads/normal/".$image_name; 
    copy($_FILES['image']['tmp_name'], $newname); 

    $thumb_name='../uploads/thumbnail/thumb_'.$image_name; 
    $thumb_name_db = 'uploads/thumbnail/thumb_'.$image_name; 
    $thumb = make_thumb($newname,$thumb_name,$thumb_width,$thumb_height); 

    $upload_status = "<div id='file-upload'><div class='upload-bar-success'><span class='upload-success'>The file has been uploaded successfully.</span></div></div>"; 
    } 

} 
} 
+0

ようにしたいものを印刷する

ではなく、利用ダイ機能を使用すると、$ファイルサイズを印刷し、それが$ max_filesizeかより大きい確認しましたか? –

答えて

1

最後if文は$upload_statusメッセージが上書きされますのでです。あなたはいつもmove_uploaded_fileをやっているので

$allowed_filetypes = array('.jpg', '.jpeg', '.gif', '.bmp', '.png'); 
$max_filesize = 262144; 
$upload_normal_path = '../uploads/normal/'; 
$upload_thumb_path = '../uploads/thumbnail/'; 

if (isset($_POST['Submit'], $_FILES['image'])) { 
    $filename = $_FILES['image']['name']; 
    $filesize = $_FILES['image']['size']; 
    $fileext = substr($filename, strpos($filename, '.'), strlen($filename) - 1); 

    $errors = array(); 

    if (!in_array($fileext, $allowed_filetypes)) { 
    $errors[] = 'The file you attempted to upload is not allowed.'; 
    } 

    if ($filesize > $max_filesize) { 
    $errors[] = 'The file you attempted to upload is too large.'; 
    } elseif ($filesize == 0) { 
    $errors[] = 'You cannot upload a empty file.'; 
    } 

    if (sizeof($errors)) { 
    echo '<p>There was some error: </p><ul>'; 
    for ($i = 0, $errorsLength = sizeof($errors); $i < $errorsLength; ++$i) { 
     echo '<li>' . $errors[$i] . '</li>'; 
    } 
    echo '</ul>'; 
    } else { 
    $newname = time() . $fileext; 
    $moved = move_uploaded_file($_FILES['image']['tmp_name'], $upload_normal_path . $newname); 

    if (!$moved) { 
     echo 'There was an error during the file upload. Please try again.'; 
    } else { 
     echo '<p>Your file upload was successful, view the file <a href="' . $upload_normal_path . $newname . '" title="Your File">here</a></p>'; 
    } 
    } 
} 
1

:ここ

は正しいコードです。

0

実際には、これはserialize ifコマンドを使用しているために起こります。条件すなわち

if(!in_array($fileext, $allowed_filetypes)){$upload_status = "The file you attempted to upload is not allowed.";} 

if(!$moved){$upload_status = 'There was an error during the file upload. Please try again.';} 

!ファイルすなわち移動が移動になっていない変数の値が変化する「場合」 次の2つを使用しています。あなたは

die("The file you attempted to upload is not allowed.");