I have an image upload script that allows simple PHP upload of profile pictures. It is now unexpectedly failing on upload. I am not entirely sure why. I have checked the post_max_size and the upload_max_size and they are way over 1MB. I have a suspicion it is to do with the //Delete previous picture but not sure why. If you look at the query where it says;
AND image1 !='../files/noprofile.jpg'
This is so that if the profile picture is the default the image for the default is not deleted. I have a feeling it is to do with this part and the upload fails when the image IS the default profile picture.
I know i should be using mysqli but please don’t mention that i am working on it.
Here’s the entire script with a few thing taken out for simplicity purposes:
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;
}
In your form tag, add the attribute enctype: