Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
uploaded image not updating
Old 07-07-2010, 02:39 PM uploaded image not updating
Skilled Talker

Posts: 83
Trades: 0
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 Code:
<?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>
Smudly is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 07-07-2010, 03:39 PM Re: uploaded image not updating
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Nothing you can do from the server. The user will have to clear their cache and refresh the page.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 07-08-2010, 08:27 AM Re: uploaded image not updating
EdB
Skilled Talker

Posts: 79
Name: Ed Barnett
Trades: 0
Quote:
Originally Posted by chrishirst View Post
Nothing you can do from the server. The user will have to clear their cache and refresh the page.
A little trick on this one actually...

<?php
$rand = rand()*10000;
?>
<img src="image.jpg?rand=<?php echo $rand; ?>" />

Addition of the query string to the end of the source means that the browser will see it as a new image every single time. That means the user doesn't need to clear their cache or refresh their page.
__________________

Please login or register to view this content. Registration is FREE
EdB is offline
Reply With Quote
View Public Profile Visit EdB's homepage!
 
Reply     « Reply to uploaded image not updating
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.20787 seconds with 12 queries