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

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 775
  • Last Modified:

File Upload Rename - Keep Extension

Hi,

Trying to do a file uplaod and keep the extension. Unforntunately I can only upload text files - any idea why?

$email_user is a variable contain username
$userfile is from the form

/* File Upload in Start */
$today = date("YmdGis");
$pieces = explode(".", $_FILES['userfile']['name']);
$ext = $pieces[count($pieces) - 1];  
$filename = "$email_user$today.$ext";
$uploaddir = '../uploads/';
$uploadfile = $uploaddir . $filename ;
if (is_uploaded_file($userfile)) {
  (move_uploaded_file($userfile, $uploadfile));
}
/* end file upload */

Thanks,

Farawayman
0
farawayman
Asked:
farawayman
  • 4
  • 3
  • 3
  • +2
3 Solutions
 
pYraniaCommented:
regardless of the extension, that won't work.
see my little example.

$pieces = explode(".", "asdf.foo.bar.txt");
$ext = $pieces[count($pieces) - 1)]; // ext = bar
0
 
farawaymanAuthor Commented:
Hey!

Whats with asdf.foo.bar.txt

Thanks.
0
 
pYraniaCommented:
that's my example filename.
just wanted to show you, that your script won't work as expected with serveral dots in the filename.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
CaNCommented:
Sorry pYrania, but your example is not right. The number of dots in the filename doesn't change anything in such an example. Beacuse file extension is always the last part in the filename. In your example $ext gives "txt" not "bar". The code that farawayman gave doesn't seem to be false. It should work.

Can you give a little more details pls? What happens when you try to upload a file other than txt file? Doesn't it upload? Or does it upload it with a false extension? What is the result?
0
 
farawaymanAuthor Commented:
Hi CaN,

The file does not upload - period.

If I try with a text file it works.

Really strange stuff.

Using php 4.3.7 and IIS

Cheers!
0
 
pYraniaCommented:
w00t, you're absolutely correct - it's still too early in the morning over here.
0
 
UmeshMySQL Principle Technical Support EngineerCommented:
Try with this..

$email_user is a variable contain username
$userfile is from the form

/* File Upload in Start */
       $today = date("YmdGis");
       $pieces = explode(".", $_FILES['userfile']['name']);
       $ext  = $pieces[1];
       $filename = $email_user.$today.".".$ext;
       $uploaddir = '../uploads/';
       $uploadfile = $uploaddir . $filename ;
if (is_uploaded_file($userfile)) {
  (move_uploaded_file($userfile, $uploadfile));
}
/* end file upload */

0
 
farawaymanAuthor Commented:
Hi,

This did not work just text files allowed still .... is this a setting somewhere else that has gone goofy?
0
 
UmeshMySQL Principle Technical Support EngineerCommented:
0
 
UmeshMySQL Principle Technical Support EngineerCommented:
If you do any changes in the Configuration files(php.ini,httpd.conf), then you must restart the web server to reflect the new changes.
0
 
Marcus BointonCommented:
There's a little wheel reinventing going on here - It's not like no-one has ever needed to figure out an extension before:

http://uk.php.net/manual/en/function.pathinfo.php

$p = pathinfo($_FILES['userfile']['name']);
$ext = $p['extension'];
0
 
farawaymanAuthor Commented:
Okay at first that did not work and then I think I discovered the problem and then my original solution should work ....

The problem was:

<INPUT TYPE="hidden" name="MAX_FILE_SIZE" value="1000">

Remove this and it works a treat.

:-))))))))))))))))))))))))))))

Okay points time:

pYrania - 100 points for being up so early to assist.
ushastry - 200 points for being persistant
squinky - 200 points for showing me the light

Thanks all!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 4
  • 3
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now