Jump to content

uploaded image not updating


Smudly

Recommended Posts

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.

 

<?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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.