Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Dynamically naming user's uploaded file based upon username

Posted on 2006-04-16
9
Medium Priority
?
272 Views
Last Modified: 2008-03-06
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 :)


0
Comment
Question by:stormist
  • 3
  • 3
  • 3
9 Comments
 
LVL 8

Assisted Solution

by:KennyTM
KennyTM earned 400 total points
ID: 16467245
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
 
LVL 10

Author Comment

by:stormist
ID: 16467265
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
 
LVL 8

Expert Comment

by:KennyTM
ID: 16467277
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Author Comment

by:stormist
ID: 16467565
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
 
LVL 8

Expert Comment

by:KennyTM
ID: 16467583
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
 
LVL 20

Accepted Solution

by:
Muhammad Wasif earned 1600 total points
ID: 16468432
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
 
LVL 20

Assisted Solution

by:Muhammad Wasif
Muhammad Wasif earned 1600 total points
ID: 16468441
use the appropriate HTML file element name in

$_FILES['profile_photo']['tmp_name']
I think it should be
$_FILES['profilephoto']['tmp_name']
0
 
LVL 20

Expert Comment

by:Muhammad Wasif
ID: 16468447
0
 
LVL 10

Author Comment

by:stormist
ID: 16469851
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
This holiday season, we’re giving away the gift of knowledge—tech knowledge, that is. Keep reading to see what hacks, tips, and trends we have wrapped and waiting for you under the tree.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question