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_p hoto'] ['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 :)
if (!empty($profile_photo))
{
move_uploaded_file ($_FILES['$profile_photo']
"pics/{$_FILES['$profile_p
}
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Can you show us the HTML form?
I think the code should be
move_uploaded_file ($_FILES['profile_photo'] ['tmp_name'], "pics/{$_FILES['profile_ph oto']['nam e']}");
but I can't ensure without the HTML form.
I think the code should be
move_uploaded_file ($_FILES['profile_photo'] ['tmp_name'], "pics/{$_FILES['profile_ph
but I can't ensure without the HTML form.
ASKER
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;?></TEXTA REA> </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['profilepho to']['name ']}");
still not working... Any ideas :(
BTW I changed permissions of folder to allow access.. tried 755 then went to 757
<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;?></TEXTA
</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['profilepho
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['profilep hoto']['na me']}" ?
And how does it fails anyway?
How about changing the path to "./pics/{$_FILES['profilep
And how does it fails anyway?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
for info visit
http://www.php.net/is_uploaded_file
http://www.php.net/is_uploaded_file
ASKER
Very cool, works now. Final code was:
if (is_uploaded_file($_FILES[ 'profileph oto'] ['tmp_name']))
{
move_uploaded_file ($_FILES['profilephoto'] ['tmp_name'], "pics/{$_FILES['profilepho to'] ['name']}");
}
plus form data above
thx guys
if (is_uploaded_file($_FILES[
{
move_uploaded_file ($_FILES['profilephoto'] ['tmp_name'], "pics/{$_FILES['profilepho
}
plus form data above
thx guys
ASKER
Here is my code:
$profile_photo = $_POST['profilephoto'];
move_uploaded_file ($_FILES[$profile_photo] ['tmp_name'], "pics/{$_FILES[$profile_ph