• Welcome to The Cave of Dragonflies forums, where the smallest bugs live alongside the strongest dragons.

    Guests are not able to post messages or even read certain areas of the forums. Now, that's boring, don't you think? Registration, on the other hand, is simple, completely free of charge, and does not require you to give out any personal information at all. As soon as you register, you can take part in some of the happy fun things at the forums such as posting messages, voting in polls, sending private messages to people and being told that this is where we drink tea and eat cod.

    Of course I'm not forcing you to do anything if you don't want to, but seriously, what have you got to lose? Five seconds of your life?

PHP Upload Problem

Yenaa

kid a
Pronoun
he
So I've been working on using PHP to make a user control panel for a project for my site. I got the profile to work just I wanted it to, but am having a bit of a problem with avatars. Basically, I'm using this form to upload the image to my server:

PHP:
 <form enctype="multipart/form-data" action="uploader_avatar.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>

and this form as the action, which uploads the file to my server.

PHP:
<?php
include "init.php";
$target_path = "avatars/";

$uploads = $_FILES['uploadedfile'];
$uploads = addslashes(mysql_escape_string($_FILES['uploadedfile']));

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 

$current = mysql_query("SELECT * FROM user WHERE username='".$_SESSION['username']."'");
$current = mysql_fetch_array($current);


$replace = mysql_query("UPDATE user SET avatar='http://ds.rothion.com/Art Boards/avatars/".  basename( $_FILES['uploadedfile']['name']). "' WHERE username='".$_SESSION['username']."'");
$replace = mysql_fetch_array($replace);


if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
    " has been uploaded";
} else{
    echo "There was an error uploading the file, please try again!";
}
?>

It functions properly. It saves the uploaded avatar into a folder, then rewrite's the user's avatar. However, it works in a way that makes hundreds of unused avatars left in my avatar folder. Is there a way to make only one of these avatars per user or to delete the last avatar?
 
maybe put in a bit of code before updating the avatar to grab the old avatar and unlink it? the absolute pathnames might make that a bit difficult, though.

but what does param MAX_FILE_SIZE actually do? you aren't using it for anything; is it some sort of intrinsic php thing?
 
I haven't assigned it a task yet, just in case it might conflict with something someone else suggests. It will limit the amount of bytes an image can have.

I'm thinking of finding a way to save it as a certain name, perhaps something corresponding with the $_SESSION['username']. So there will be a Seadra.png file (my nickname. :P) and each time I upload, it overwrites. Thing is, I have no idea how that could be coded functionally.

What do you think?
 
create a directory avatar/user and have the script clean out those directories leaving only the most recently-accessed image?

the problem with your idea is that someone might want a gif or a jpeg for their avatar.

you'll have Seadra.gif, Seadra.png, Seadra.jpg, Seadra.jpeg, Seadra.jpe, Seadra.bmp, and so on or unless you 're will to limit to one format.
 
Hm, I seemed to have solved the problem, though I wanted it to save the name as the user or at least something recognizable, like avatar_1. Oh well.

What I did was I used a query to snatch the avatar-in-use, and used str_replace to get rid of the "http://ds.rothion.com/...." so it's just the avatar and file_type. Then added unlink() at the end of my if. A lot easier than expected, no? XD
 
Back
Top Bottom