0

我有一个图片上传脚本,允许简单的 PHP 上传个人资料图片。现在上传时意外失败。我不完全确定为什么。我检查了 post_max_size 和 upload_max_size ,它们都超过 1MB。我怀疑它与//删除上一张图片有关,但不知道为什么。如果您查看它所说的查询;

AND image1 !='../files/noprofile.jpg'

这样一来,如果配置文件图片是默认设置,则默认设置的图像不会被删除。我感觉这与这部分有关,当图像是默认个人资料图片时,上传失败。

我知道我应该使用 mysqli 但请不要提及我正在研究它。

这是整个脚本,为简单起见,删除了一些内容:

session_start();
$username=$_SESSION['username'];
$pass = $_SESSION['password']; 
$path = "../imageuploads/";

//Delete previous picture
$pic = mysql_query("
SELECT * FROM members WHERE artist='Y' AND username='$username' AND password='$pass' AND image1 !='../files/noprofile.jpg'
")or die(mysql_error());

 while($fetchpic = mysql_fetch_array($pic)){ 

//if image1 does not equal no profile
unlink( $fetchpic['image1']); 

}


$valid_formats = array("jpg", "png", "gif", "bmp", "jpeg");
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
    {
        $name = $_FILES['photoimg1']['name'];
        $size = $_FILES['photoimg1']['size'];

        if(strlen($name))
            {
                list($txt, $ext) = explode(".", $name);
                if(in_array($ext,$valid_formats))
                {
                if($size<(2024*2024))
                    {
                        $actual_image_name = time().substr(str_replace(" ", "_", $txt), 5).".".$ext;
                        $tmp = $_FILES['photoimg1']['tmp_name'];
                        if(move_uploaded_file($tmp, $path.$actual_image_name))
                            {
                            mysql_query("UPDATE members SET image1='../imageuploads/$actual_image_name' WHERE username='$username' AND password ='$pass' AND artist='Y'");

                                echo "<p1><img src='../imageuploads/".$actual_image_name."'  class='imageright1'></p1>";
                            }
                        else
                            echo "<p1>failed</p1>";
                    }
                    else
                    echo "<p1>Your image is a bit too big. It has to be below 1MB.</p1>";                   
                    }
                    else
                    echo "<p1>Only JPG, PNG GIF and BMP file formats are accepted.</p1>";   
            }

        else
            echo "<p1>You've gotta select an image first!</p1>";

        exit;
    }
4

1 回答 1

0

在您的表单标签中,添加属性 enctype:

<form action="page.php" method="post" enctype="multipart/form-data">
于 2012-07-23T17:22:22.647 回答