Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

File Upload Rename - Keep Extension

Posted on 2004-08-02
12
Medium Priority
?
772 Views
Last Modified: 2008-01-09
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
Comment
Question by:farawayman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
  • +2
12 Comments
 
LVL 3

Expert Comment

by:pYrania
ID: 11701186
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
 

Author Comment

by:farawayman
ID: 11701226
Hey!

Whats with asdf.foo.bar.txt

Thanks.
0
 
LVL 3

Expert Comment

by:pYrania
ID: 11701250
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 1

Expert Comment

by:CaN
ID: 11701320
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
 

Author Comment

by:farawayman
ID: 11701351
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
 
LVL 3

Assisted Solution

by:pYrania
pYrania earned 400 total points
ID: 11701409
w00t, you're absolutely correct - it's still too early in the morning over here.
0
 
LVL 26

Assisted Solution

by:Umesh
Umesh earned 800 total points
ID: 11701566
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
 

Author Comment

by:farawayman
ID: 11701621
Hi,

This did not work just text files allowed still .... is this a setting somewhere else that has gone goofy?
0
 
LVL 26

Expert Comment

by:Umesh
ID: 11701693
0
 
LVL 26

Expert Comment

by:Umesh
ID: 11701724
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
 
LVL 25

Accepted Solution

by:
Marcus Bointon earned 800 total points
ID: 11701788
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
 

Author Comment

by:farawayman
ID: 11701938
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

What’s Wrong with Your Cloud Strategy ?

Even as many CIOs are embracing a cloud-first strategy, the reality is that moving to the cloud is a lengthy process and the end-state is likely to be a blend of multiple clouds—public and private. Learn why multicloud solutions matter in this webinar by Nimble Storage.

Question has a verified solution.

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

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

636 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