简体   繁体   English

PHP:图片上传器错误

[英]PHP: Image uploader error

Im making an image uploader, but i get the error: Only JPG, JPEG and PNG are allowed image types. 我正在制作图像上传器,但出现错误:只允许JPG,JPEG和PNG图像类型。

The uploader doesn't get the extension right. 上传者没有正确使用扩展名。 What do i do wrong? 我做错了什么? The function to get the extension is at line 33. Ad from line 59 is where im trying to get the extension. 获取扩展名的功能在第33行。第59行的广告是我试图获取扩展名的地方。

<?php session_start(); if ($_SESSION['username']) {} else { header("location:index.php"); exit(); } ?>

<?php

include 'db_connect.php';
$uploadSubmit = mysql_real_escape_string($_POST['imageSubmit']);

if ($uploadSubmit)
{
if ($_FILES['image'])
{
    $contents = file_get_contents($_FILES['image']['tmp_name']);

    if (stristr($contents, "<?php") || stristr($contents, "system(") || stristr($contents, "exec(") ||
    stristr($contents, "mysql") || stristr($contents, "include(") || stristr($contents, "require(") ||
    stristr($contents, "include_once(") || stristr($contents, "require_once(") || stristr($contents, "echo'") || stristr($contents, 'echo"'))
    {
        echo 'Are you really trying to hack this site? Enjoy your upload b&.';
        $sql = "INSERT INTO banned (ip) VALUES ('".$_SERVER['REMOTE_ADDR']."')";
        $result = mysql_query($sql) or trigger_error(mysql_error()."".$sql);
        die();
    }
}

else
{
    $sql = "SELECT * FROM banned WHERE ip='".$_SERVER['REMOTE_ADDR']."'";
    $result = mysql_query($sql) or trigger_error(mysql_error()."".$sql);
    $num_rows = mysql_fetch_row($result);

    if ($num_rows[0] == 0)
    {
        function getExtension($str)
        {
            $i = strrpos($str,".");

            if (!$i)
            {
                return "";
            }

            $I = strlen($str) - $i;
            $ext = substr($str,$i+1,$I);
            return $ext;
        }

        define ("MAX_SIZE","5000");
        $error = 0;
        $file = $_FILES['image']['name'];

        if ($file = '')
        {
            echo 'You didn\'t select an image to upload.';
            $error = 1;
        }

        else
        {
            $filename = stripslashes($file);
            $extension = getExtension($filename);
            $extension = strtolower($extension);

            if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png"))
            {
                echo 'Only JPG, JPEG and PNG are allowed image types.';
                $error = 1;
            }

            else
            {
                $size = filesize($_FILES['image']['tmp_name']);

                if ($size > MAX_SIZE*1024)
                {
                    echo 'The max allowed filesize is 5MB.';
                    $error = 1;
                }

                $time = time();
                $newImageName = 'wally-'.$time.'.'.$extension.'';
                $imageFullPath = 'images/'.$newImageName.'';

                if (!$errors)
                {
                    if (!move_uploaded_file($_FILES['image']['tmp_name'], $imageFullPath))
                    {
                        $error = 1;
                    }
                }

                if ($uploadSubmit && !$error)
                {
                    include 'class.imageResizer.php';
                    $work = new ImgResizer($imageFullPath);
                    $work -> resize(125, "thumbs/".$newImageName."");

                    $uploader = $_SESSION['username'];
                    $sql = "INSERT INTO images (image, uploader, validated) VALUES ('$newImageName','$uploader','0')";
                    $result = mysql_query($sql) or trigger_error(mysql_error()."".$sql);

                    echo 'Your image has been uploaded and awaiting validation.';
                    echo 'The page will redirect in 2 seconds.';
                    echo '<meta http-equiv="Refresh" content="2;url=http://www.wallpapers.puffys.net">';

                }
            }
        }
    }

    else
    {
        die("You are banned from uploading.");
    }
}
}

?>

Try using something like this: 尝试使用如下所示的内容:

$allowedExtensions = array("jpg","jpeg","png"); 
if (!in_array(end(explode(".",strtolower($file))),$allowedExtensions)) { 
   echo 'Only JPG, JPEG and PNG are allowed image types.';
   $error = 1;
} 
$i = strrpos($str,".");

if (!$i)

isn't a good way to test if the strrpos function returns a positive value. 这不是测试strrpos函数是否返回正值的好方法。

You should use the === operator, like this : 您应该使用===运算符,如下所示:

$i = strrpos($str,".");

if ($pos === false)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM