Connecting Tech Pros Worldwide Forums | Help | Site Map

Saving an uploaded file with a User-specified name

Member
 
Join Date: Jun 2007
Posts: 49
#1: Sep 24 '07
Hi All.

Anyone plz tell me how can I upload a file with the file name of my choice.
am having problem with this, the file got uploaded but not with the name I wanted to...how can I do this ....please tell me.

Here is the code:

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <title>Uploading...</title>
  4. </head>
  5. <body>
  6. <h1>Uploading file...</h1>
  7. <?php
  8. if ($_FILES['userfile']['error'] > 0)
  9. {
  10.     switch ($_FILES['userfile']['error'])
  11.       {
  12.              case 1: echo 'File exceeded upload_max_filesize'; break;
  13.              case 2: echo 'File exceeded max_file_size'; break;
  14.              case 3: echo 'File only partially uploaded'; break;
  15.              case 4: echo 'No file uploaded'; break;
  16.       }
  17. exit;
  18. }
  19.  
  20. $cr=$_REQUEST[fname];
  21. $upfile = "uploads/$cr/".$_FILES['userfile']['name'] ;
  22.  
  23. if (is_uploaded_file($_FILES['userfile']['tmp_name']))
  24.   {
  25.      if (!move_uploaded_file($_FILES['userfile']['tmp_name'], $upfile)) 
  26.          {
  27.             echo 'Problem: Could not move file to destination directory';
  28.            exit;
  29.         }
  30.   }
  31. else
  32. {
  33.       echo 'Problem: Possible file upload attack. Filename: ';
  34.       echo $_FILES['userfile']['name'];
  35.       exit;
  36. }
  37.  
  38.     echo 'File uploaded successfully<br><br>';
  39.  
  40. echo "<a href='tlecs.php'>click here to continue</a>";
  41.  
  42. echo '<br><hr>';
  43. ?>
  44. </body>
  45. </html>
  46.  


Regards

pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Posts: 5,435
#2: Sep 24 '07

re: Saving an uploaded file with a User-specified name


Heya, Muddasir.

Please use CODE tags when posting source code:

[CODE=php]
PHP code goes here.
[/CODE]

This is not the first time I've had to ask you.

Changed thread title to better describe the problem (did you know that threads whose titles do not follow the Posting Guidelines actually get FEWER responses?).
volectricity's Avatar
Expert
 
Join Date: Jun 2007
Location: Baltimore
Posts: 587
#3: Sep 24 '07

re: Saving an uploaded file with a User-specified name


Quote:

Originally Posted by Muddasir

the file got uploaded but not with the name I wanted to

Really? Because according to the code, the name you wanted was the original filename.

[php]$upfile = "uploads/$cr/".$_FILES['userfile']['name'] ;[/php]

If you want to change the filename, it's right there.
Reply