0

ユーザーが画像をアップロードできるようにするスクリプトを作成していますが、今は gif と png のみを受け入れる単純な if ステートメントを含めたいと考えています。これが私のコードですが、必要な方法で機能していないようです。

#extention filter
$allowed = array("image/gif","image/png");  
if(!in_array($files,$allowed)){
 $error_message = 'Only jpg, gif, and pdf files are allowed.';
  $error = 'yes';
  echo $error_message;
  exit();
}   #extention
4

3 に答える 3

7
    $filename=$_FILE['name']['filename_in_html'];  //you can change this with your filename
    $allowed='png,jpg';  //which file types are allowed seperated by comma

    $extension_allowed=  explode(',', $allowed);
    $file_extension=  pathinfo($filename, PATHINFO_EXTENSION);
    if(array_search($file_extension, $extension_allowed))
    {
        echo "$extension_allowed allowed for uploading file";
    }
    else
    {
        echo "$extension_allowed is not allowed for uploading file";
    }
于 2013-02-09T15:01:19.217 に答える
3
$file_type = $_FILES['name']['type']; //returns the mimetype

$allowed = array("image/png", "image/gif", "application/pdf");
if(!in_array($file_type, $allowed)) {
  $error_message = 'Only png, gif files are allowed.';
  $error = 'yes';
}

問題の解決に役立つことを願っています。

于 2013-02-09T14:55:48.507 に答える
0
$path_parts = pathinfo('/path/emptyextension.');
var_dump($path_parts['extension']);

これにより、正確な拡張子が返され、必要に応じて比較できます。

于 2013-02-09T14:41:39.000 に答える