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 :)


LVL 10
stormistAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

KennyTMCommented:
Use strstr:

strstr('file.ext', '.');

will return ".ext". However it won't deal with double extension like "abc.txt.jpg" properly. To extract the real extension from this kind of file, use RegExp:

$a = '';
preg_match('/\.[^.]+$/', 'file.name.ext', $a);
echo $a[0];

this will output ".ext".
0
stormistAuthor Commented:
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']}");  
0
KennyTMCommented:
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.
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

stormistAuthor Commented:
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

     
0
KennyTMCommented:
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?
0
Muhammad WasifCommented:
change
<form method='post' action='updateprofile.php'>
to
<form method='post' action='updateprofile.php' enctype="multipart/form-data">

and

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

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Muhammad WasifCommented:
use the appropriate HTML file element name in

$_FILES['profile_photo']['tmp_name']
I think it should be
$_FILES['profilephoto']['tmp_name']
0
Muhammad WasifCommented:
0
stormistAuthor Commented:
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.