Link to home
Start Free TrialLog in
Avatar of stormist
stormist

asked on

Dynamically naming user's uploaded file based upon username

Here is my code:
if (!empty($profile_photo))
{
 move_uploaded_file ($_FILES['$profile_photo'] ['tmp_name'],
       "pics/{$_FILES['$profile_photo'] ['name']}");
   
}


Next I'd like to rename the file to the usernames + the extension. The user can only have one photo which either has to be .gif or .jpg. So I'd like to overwrite or delete the old photo.
So I know I can use rename but
1) how do i extract the extension from the original name?
(username is contained in variable $username)

say the user with username: sara15 uploads lisa456.jpg. Id like to copy lisa456.jpg to /pics then rename it to sara15.jpg after making sure sara15.gif doesn't exist and overwriting sara15.jpg if it exists.

Any help would be much appreciated :)


SOLUTION
Avatar of KennyTM
KennyTM

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of stormist
stormist

ASKER

I seem to have a more serious issue than that. My file uploads are not working. Any idea why?


Here is my code:
 $profile_photo = $_POST['profilephoto'];  
 move_uploaded_file ($_FILES[$profile_photo] ['tmp_name'], "pics/{$_FILES[$profile_photo] ['name']}");  
Can you show us the HTML form?

I think the code should be

 move_uploaded_file ($_FILES['profile_photo'] ['tmp_name'], "pics/{$_FILES['profile_photo']['name']}");  

but I can't ensure without the HTML form.
Either way it doesn't work. The direct variable name is profilephoto. Using the POST statement above I put it in $profile_photo. Here is the HTML form:
<form method='post' action='updateprofile.php'>    
  <table bgcolor='#4B0082'>
    <tr>
        <td><font color="#FFFFFF"> Name:</font></td>
        <td><input type='text' value='<?php echo $profile_name."'"; ?> name='profilename' size=30 maxlength=100></td></tr>
       
    <tr>
        <td><font color="#FFFFFF"> City: </font> <br />:</td>
        <td valign='top'><input type='text' value='<?php echo $profile_city."'"; ?> name='profilecity' size=16 maxlength=16></td></tr>
       
        <td><font color="#FFFFFF"> State: </font> <br />:</td>
        <td valign='top'><input type='text' value='<?php echo $profile_state."'"; ?> name='profilestate' size=16 maxlength=16></td></tr>
       
    <tr>
        <td><font color="#FFFFFF">Change Photo:</font></td>
        <td><input type='file' name='profilephoto' size=16 maxlength=16></td></tr>
       
    <tr>
        <td><font color="#FFFFFF"> About Me: </font></td>
         <td> <TEXTAREA NAME="profileaboutme" COLS=40 ROWS=4><?php echo $profile_aboutme;?></TEXTAREA> </td>
          </tr>  
    <tr>
     <td colspan=2 align='center'>
     <input type='submit' value='Update Profile'></td></tr>
     
 </table></form>


So once again when I change it to:
move_uploaded_file ($_FILES['profilephoto'] ['tmp_name'], "pics/{$_FILES['profilephoto']['name']}");  

still not working...  Any ideas :(

BTW I changed permissions of folder to allow access.. tried 755 then went to 757

     
After the correction the failure should have no connection with the HTML form then.

How about changing the path to "./pics/{$_FILES['profilephoto']['name']}" ?

And how does it fails anyway?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Very cool, works now. Final code was:
if (is_uploaded_file($_FILES['profilephoto'] ['tmp_name']))
{
      move_uploaded_file ($_FILES['profilephoto'] ['tmp_name'], "pics/{$_FILES['profilephoto'] ['name']}");
}
plus form data above

thx guys