Page 1 of 1

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

Posted: Mon Dec 12, 2011 5:49 pm
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

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

Posted: Mon Dec 12, 2011 6:27 pm
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.

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

Posted: Mon Dec 12, 2011 6:46 pm
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?

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

Posted: Mon Dec 12, 2011 7:56 pm
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()

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

Posted: Mon Dec 12, 2011 8:59 pm
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);

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

Posted: Mon Dec 12, 2011 9:01 pm
by axlemax
Thanks guys. This has been great help.

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