Help!! How to make a "safe" file name using PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
axlemax
Forum Newbie
Posts: 5
Joined: Mon Dec 12, 2011 5:29 pm
Location: Sydney, Australia

Help!! How to make a "safe" file name using PHP

Post by axlemax »

Hi all,

Can you help me please?

My goal is to upload files to my web server (from my hard drive). I have written the code for this but I want to be able to handle file names with "illegal" characters in them. So basically I want to remove spaces, etc from file names as they get POST'ed to the PHP code that processes the files.

The strategy that I think is best will be to take all the "legal" characters and keep them, and simply leave out any other characters (including spaces). "Legal" characters for my purposes will be alphanumeric (e.g. a-z (upper and lower case & 0-9).

I assume that I can use preg_replace for this or event load the legal characters into an array and create a string from the array. Any advice would be great. Thanks
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: Help!! How to make a "safe" file name using PHP

Post by flying_circus »

You can use a regular expression if you want, but if you want to preserve the original file name, the work flow might look something like this:

1. Submit File to your server via Post
2. Generate unique file identifier (no extension)
3. Save the file to your server disk renamed to the generate file identifier.
4. Store the original filename (and other info) in a database, along with the unique file identifier.

When sending the file back to the client, use PHP to serve the file. Then you can send the original file name in the headers, so the user will download the file by the original file name and extension, rather than a file named with your unique identifier.
axlemax
Forum Newbie
Posts: 5
Joined: Mon Dec 12, 2011 5:29 pm
Location: Sydney, Australia

Re: Help!! How to make a "safe" file name using PHP

Post by axlemax »

Hi,

That's a top idea!! I will try that. Thanks for the reply.

I would also like to learn how to take a string and remove all of the non-alphnumeric characters. Could you give me some advice about how to do that please?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Help!! How to make a "safe" file name using PHP

Post by Celauran »

axlemax wrote:I would also like to learn how to take a string and remove all of the non-alphnumeric characters. Could you give me some advice about how to do that please?

Code: Select all

$string = preg_replace('/[^a-zA-Z0-9]/', '', $string);
preg_replace()
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: Help!! How to make a "safe" file name using PHP

Post by flying_circus »

Celauran wrote:
axlemax wrote:I would also like to learn how to take a string and remove all of the non-alphnumeric characters. Could you give me some advice about how to do that please?

Code: Select all

$string = preg_replace('/[^a-zA-Z0-9]/', '', $string);
preg_replace()
Any reason not to use the case insensitive modifier, and UTF8?

Code: Select all

$string = preg_replace('/[^a-z0-9]/ui', '', $string);
axlemax
Forum Newbie
Posts: 5
Joined: Mon Dec 12, 2011 5:29 pm
Location: Sydney, Australia

Re: Help!! How to make a "safe" file name using PHP

Post by axlemax »

Thanks guys. This has been great help.

Flying circus. I followed your 4 steps and they work great. Thank you so much!
Post Reply