Change file name

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
S_henry
Forum Contributor
Posts: 148
Joined: Sun Jan 25, 2004 10:25 pm
Location: M'sia

Change file name

Post by S_henry »

I want to upload my picture and change that file (picture) name same as my login. This mean everybody can upload their picture when they login into my system and the picture will be saved in 1 folder. Let say my picture's file name is 'johnphillip' and my login is 'john'. So I want to save this picture using 'john' as a name in that folder after uploaded. Any idea pls...
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post by DuFF »

Do you already have an upload script and authentication (or at least the Username held in a cookie/session)?? If so all you need to add is a few lines, other wise you should start with the upload form first and we'll help you from there.
S_henry
Forum Contributor
Posts: 148
Joined: Sun Jan 25, 2004 10:25 pm
Location: M'sia

Change file name

Post by S_henry »

Ok. Let me do session part first and then we'll continue. I'll be back..
S_henry
Forum Contributor
Posts: 148
Joined: Sun Jan 25, 2004 10:25 pm
Location: M'sia

Change file name

Post by S_henry »

Ok. I've create a session and can hold the login and password value to any page. After that, what should I do?
S_henry
Forum Contributor
Posts: 148
Joined: Sun Jan 25, 2004 10:25 pm
Location: M'sia

Change file name

Post by S_henry »

Anybody can help me pls...
S_henry
Forum Contributor
Posts: 148
Joined: Sun Jan 25, 2004 10:25 pm
Location: M'sia

Change file name

Post by S_henry »

Maybe too difficult I think, at least for me... :(
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

simplest way i would do it
psuedo-code wrote: create a session with your username appended to it

go to your 'upload area'

run an if/else statement to see if a var has been set
rename the file using $username
S_henry
Forum Contributor
Posts: 148
Joined: Sun Jan 25, 2004 10:25 pm
Location: M'sia

Post by S_henry »

Actually I not very understand how to do it. Can anybody explain pls.. Thanx.
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Try this

Post by andre_c »

Try something like this:
- An upload form:

Code: Select all

<form action="formhandler.php" enctype="multipart/form-data" method="post">

    <input type='file' name='loginpic' />

    <input type='submit' name='imgupload' value='Send Picture' />

</form>
- Form Handler Page:

Code: Select all

<?
if (isset($_REQUEST['imgupload'])) {
    $pic_dir_name = '/path/to/image/'.$_SESSION['login_name'].'.jpg';

    if (is_uploaded_file($_FILES['loginpic']['tmp_name'])) {
        copy($_FILES['loginpic']['tmp_name'], $pic_dir_name);
    } else {
        echo "Image Upload Failed. Filename: " . $_FILES['loginpic']['name'];
    }
}
?>
Post Reply