我有一个图像上传脚本,它允许简单的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;
}
在表单标记中,添加属性enctype:
<form action="page.php" method="post" enctype="multipart/form-data">