在php中上传图片时出错

时间:2013-09-12 09:31:27

标签: php mysql image-upload

当我上传jpg文件时,我的代码回显了else语句。我错过了哪里出错了?

它在代码btw的第39行

if(($type=="image/jpeg") || ($type=="image/JPG") || ($type=="image/bmp") || ($type=="image/png")) 
{
    move_uploaded_file($temp, "images/$mypic");
    echo "What a pretty face! <img border='1' width='50' height='50' src='images/$mypic'><p>";
    mysqli_query($link,$query);

    echo ("user registered successfully!<a href='home.php'>Login now!</a>");
}
else
{
    echo ($type);
    echo "Please load a valid jpeg, jpg or bmp! And size must be less than 10k!";           }
} 

3 个答案:

答案 0 :(得分:0)

尝试做:

$type_arr = array(
  "image/jpeg", "image/jpg", "image/bmp", "image/png"
);
//and
$type = strtolower($type);
if( in_array($type, $type_arr) ) {
    //your upload code here
}
else {
   //error message
   echo $type;
}

答案 1 :(得分:0)

我认为这是你的文件大小问题。检查一次。

$allow = array("jpg", "jpeg", "gif", "png");

$todir = 'uploads/';

if ( !!$_FILES['file']['tmp_name'] ) // is the file uploaded yet?
{
$ext = explode('.', strtolower( $_FILES['file']['name']) ); // whats the extension of the file

if ( in_array( $ext, $allow) ) // is this file allowed
{
    if ( move_uploaded_file( $_FILES['file']['tmp_name'], $todir . basename($_FILES['file']['name'] ) ) )
    {
        // the file has been moved correctly
    }
}
else
{
    // error this file ext is not allowed
}
}

答案 2 :(得分:0)

我认为你正在检查图像,我会这样做

if(!(getimagesize ($temp)==0))
{
//an image
}
else
{
//not an image
}