我想问一下如何限制PHP图片上传的文件类型?我想将它限制为png,jpg和gif。对不起这个noob问题。
我试过的是在条件中添加'OR'。但是,当我上传jpg,gif或png时,为什么它不起作用呢?
<?php
//limit file type condition
if(!($_FILES["photo"]["type"] == "image/png")
|| !($_FILES["photo"]["type"] == "image/jpg")
|| !($_FILES["photo"]["type"] == "image/gif")){
echo "You may only upload png, jpg or gif.<br>";
$ok=0;
}
?>
答案 0 :(得分:2)
使用in_array()
:
$allowed_extensions = array( "image/png", "image/jpg", "image/gif" );
if ( !in_array( $_FILES[ "photo" ][ "type" ], $allowed_extensions ) ){
echo "You may only upload png, jpg or gif.<br>";
$ok = 0;
}
答案 1 :(得分:0)
这样做
$config['upload_path'] = '';
$config['allowed_types'] = 'gif|jpg|png'; //pic type
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors()); //you will get error statement in $error
}
else
{....
}