Page 1 of 1

uploaded image not updating

Posted: Wed Jul 07, 2010 1:39 pm
by Smudly
I've got a page where the user can upload an avatar/photo. Once it uploads, it isn't updated for the user. It either shows the old image that was already there, or shows a broken image. What is wrong with my code. How can I get it to update the most recent image uploaded upon hitting submit?

Thanks.

Code: Select all

<?php
session_start();

if (isset($_SESSION['username'])){

$username = $_SESSION['username'];
$upload = $_POST['upload'];


include('inc/connect.php');
$uploadsql = mysql_query("SELECT * FROM `users` WHERE `username`='$username'"); 
$uploadrow = mysql_fetch_assoc($uploadsql); 

$id = $uploadrow['id']; 
$avatar = $uploadrow['avatar'];
$folder = "avatars/";


$avatarshow = '<img src="avatars/' . $avatar . '" width="65" height="65" />';
if ($upload)
{

	// Name of file
$name = $_FILES["image"]["name"];
	// Type of file (video/avi) or image/jpg, etc
$type = $_FILES["image"]["type"];
	//size of file
$size = $_FILES["image"]["size"];
	//stores file in a temporary location
$temp = $_FILES["image"]["tmp_name"];
	// if there is an error
$error = $_FILES["image"]["error"];


// function genRandomString() {
    // $characters = '0123456789abcdefghijklmnopqrstuvwxyz';
    // $length = 10;
    // $string = '';    

    // for ($p = 0; $p < $length; $p++) {
        // $string .= $characters[mt_rand(0, strlen($characters))];
    // }

    // return $string;
// }

if ($error > 0)
{
	$uploaderror = "An error occured. Please try again.";
}
else
{

if ($type=="image/jpg" || $type=="image/png" || $type=="image/gif" || $type=="image/bmp" || $type=="image/jpeg")
{
	if ($size <= 1048576)
	{
		//if(file_exists("avatars/".$name)){
			if ($type=="image/jpeg"){
				$extension = ".jpg";
			}
			if ($type=="image/jpg"){
				$extension = ".jpg";
			}
			if ($type=="image/png"){
				$extension = ".png";
			}
			if ($type=="image/bmp"){
				$extension = ".bmp";
			}
			if ($type=="image/gif"){
				$extension = ".gif";
			}
		//$name1 = genRandomString($string);
		
		
		$ext = $extension;
		$name = $id.$ext;
		//}
			
			// unlink deletes the file so a new file can be uploaded in its place
			unlink('avatars/' . $avatar);
			
			$uploadquery = mysql_query("UPDATE users SET avatar='$name' WHERE username='$username'");
			
			move_uploaded_file($temp, "avatars/".$name);
			
			$success = "Upload Complete!";
		
	}
	else{
		$uploaderror = "Your image must be less than 1 megabytes.";
	}
}
else
{
die("That format is not allowed!");

}
}
}
}
else{

header("Location: index.php");
	
}
?>
<html>
<h1>Upload a File</h1>
<form action="upload.php" method="POST" enctype="multipart/form-data">
	<input type="file" name="image"><br />
	<input type="submit" name="upload" value="Upload"><br />
	<?php $avatarshow = '<img src="avatars/' . $avatar . '" width="65" height="65" />'; ?>
	<?php echo $success, $uploaderror."<br />".$avatarshow; ?>

</form>
</html>

Re: uploaded image not updating

Posted: Wed Jul 07, 2010 1:49 pm
by Jade
Try these headers...

Code: Select all

header("Expires: Mon, 14 Jul 1789 12:30:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");