Re: [PHP] Web friendly file names

2009-06-03 Thread Eric Butera
On Wed, Jun 3, 2009 at 2:29 PM, Paul M Foster  wrote:
> (Sorry, I hit the wrong button and sent the reply only to Skip.)
>
> On Wed, Jun 03, 2009 at 11:18:57AM -0500, Skip Evans wrote:
>
>> Hey all,
>>
>> I have a file uploader module that allows users to upload
>> documents and of course people are using all kinds of file
>> names that are not web friendly.
>>
>> I guess the best solution is to replace any non alphanumeric
>> with maybe '_' the underscore? How does that sound?
>>
>> Unfortunately, after 20+ years of coding I cannot get my brain
>> around regular expressions to any decent level of proficiency,
>> I know sad.
>>
>> I'd like to hear other solutions for this problem, I am
>> thinking of a regexp that replaces special chars with the
>> underscore; sounds pretty robust and globally acceptable?
>>
>> Opinions, witticisms, flames, quotations by famous débutantes?
>
> Here's what I do on some of my webpages:
>
> $fname = preg_replace("%[^A-Za-z0-9_\.]%", "_", $_POST['fname']);
>
> This says to replace any character which isn't alphanumeric or
> underscore or period, with an underscore. You can add characters to the
> first expression in preg_replace to allow more characters, or change the
> underscore expression with nothing ("") or whatever. I typically simply
> eliminate bad stuff by using "" as the second parameter for
> preg_replace.
>
> To expand further on one thing: inside the first parameter, you'll see
> the open square bracket ([) which indicates the beginning of a character
> grouping or class. The next character, the caret (^) indicates negation.
> So that expression means that anything which doesn't match these
> characters gets whatever the next parameter is.
>
> Paul
>
> --
> Paul M. Foster
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

Also don't forget to check for uniqueness after doing all of these
transformations fellas.  In my experience users love uploading the
same names over and over. :)

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Web friendly file names

2009-06-03 Thread Paul M Foster
(Sorry, I hit the wrong button and sent the reply only to Skip.)

On Wed, Jun 03, 2009 at 11:18:57AM -0500, Skip Evans wrote:

> Hey all,
>
> I have a file uploader module that allows users to upload
> documents and of course people are using all kinds of file
> names that are not web friendly.
>
> I guess the best solution is to replace any non alphanumeric
> with maybe '_' the underscore? How does that sound?
>
> Unfortunately, after 20+ years of coding I cannot get my brain
> around regular expressions to any decent level of proficiency,
> I know sad.
>
> I'd like to hear other solutions for this problem, I am
> thinking of a regexp that replaces special chars with the
> underscore; sounds pretty robust and globally acceptable?
>
> Opinions, witticisms, flames, quotations by famous débutantes?

Here's what I do on some of my webpages:

$fname = preg_replace("%[^A-Za-z0-9_\.]%", "_", $_POST['fname']);

This says to replace any character which isn't alphanumeric or
underscore or period, with an underscore. You can add characters to the
first expression in preg_replace to allow more characters, or change the
underscore expression with nothing ("") or whatever. I typically simply
eliminate bad stuff by using "" as the second parameter for
preg_replace.

To expand further on one thing: inside the first parameter, you'll see
the open square bracket ([) which indicates the beginning of a character
grouping or class. The next character, the caret (^) indicates negation.
So that expression means that anything which doesn't match these
characters gets whatever the next parameter is.

Paul

-- 
Paul M. Foster

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Web friendly file names

2009-06-03 Thread Bastien Koert
On Wed, Jun 3, 2009 at 1:18 PM, Eddie Drapkin  wrote:
> As far as I know it will only bug out when the host filesystem would bug
> out, ie UTF-16 characters tend to explode on my linux setup that I develop
> on, and PHP's file handlign doesn't like them, but then again neither does
> the filesystem.  Stuff like "Some silly name! YAY#" work fine, on the
> other hand.
>
> On Wed, Jun 3, 2009 at 1:00 PM, Skip Evans  wrote:
>
>> Oh, of course that makes sense, and I suppose the PHP move_uploaded_file()
>> function has no problem with weird and crappy file names?
>>
>> Skip
>>
>> Eddie Drapkin wrote:
>>
>>> Well, erm, no.
>>>
>>> I'd store the filename, etc. as-is in the database, and then link it with
>>> urlencode() and you should be able to serve a file called "A non friendly
>>> name!" site.com/A%20non%20friendly.. 
>>> ..
>>> and a modern webserver should be fine with that, and even most browsers will
>>> allow you to type site.com/A  non friendly... without
>>> the url escaped characters and it will url escape them in the request.  The
>>> only issue is that the urlencoded special characters are a lot uglier, which
>>> may or may not be a consideration for you.
>>>
>>> On Wed, Jun 3, 2009 at 12:39 PM, Skip Evans >> s...@bigskypenguin.com>> wrote:
>>>
>>>
>> --
>> 
>> Skip Evans
>> Big Sky Penguin, LLC
>> 503 S Baldwin St, #1
>> Madison WI 53703
>> 608.250.2720
>> http://bigskypenguin.com
>> 
>> Those of you who believe in
>> telekinesis, raise my hand.
>>  -- Kurt Vonnegut
>>
>> --
>> PHP General Mailing List (http://www.php.net/)
>> To unsubscribe, visit: http://www.php.net/unsub.php
>>
>>
>


how about just hashing the filename? Store them in a week-year folder
and store a record in the db with the real file name and the hash.
-- 

Bastien

Cat, the other other white meat

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Web friendly file names

2009-06-03 Thread Eddie Drapkin
As far as I know it will only bug out when the host filesystem would bug
out, ie UTF-16 characters tend to explode on my linux setup that I develop
on, and PHP's file handlign doesn't like them, but then again neither does
the filesystem.  Stuff like "Some silly name! YAY#" work fine, on the
other hand.

On Wed, Jun 3, 2009 at 1:00 PM, Skip Evans  wrote:

> Oh, of course that makes sense, and I suppose the PHP move_uploaded_file()
> function has no problem with weird and crappy file names?
>
> Skip
>
> Eddie Drapkin wrote:
>
>> Well, erm, no.
>>
>> I'd store the filename, etc. as-is in the database, and then link it with
>> urlencode() and you should be able to serve a file called "A non friendly
>> name!" site.com/A%20non%20friendly.. ..
>> and a modern webserver should be fine with that, and even most browsers will
>> allow you to type site.com/A  non friendly... without
>> the url escaped characters and it will url escape them in the request.  The
>> only issue is that the urlencoded special characters are a lot uglier, which
>> may or may not be a consideration for you.
>>
>> On Wed, Jun 3, 2009 at 12:39 PM, Skip Evans > s...@bigskypenguin.com>> wrote:
>>
>>
> --
> 
> Skip Evans
> Big Sky Penguin, LLC
> 503 S Baldwin St, #1
> Madison WI 53703
> 608.250.2720
> http://bigskypenguin.com
> 
> Those of you who believe in
> telekinesis, raise my hand.
>  -- Kurt Vonnegut
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


Re: [PHP] Web friendly file names

2009-06-03 Thread Skip Evans
Oh, of course that makes sense, and I suppose the PHP 
move_uploaded_file() function has no problem with weird and 
crappy file names?


Skip

Eddie Drapkin wrote:

Well, erm, no.

I'd store the filename, etc. as-is in the database, and then link it 
with urlencode() and you should be able to serve a file called "A non 
friendly name!" site.com/A%20non%20friendly.. 
.. and a modern webserver should 
be fine with that, and even most browsers will allow you to type 
site.com/A  non friendly... without the url escaped 
characters and it will url escape them in the request.  The only issue 
is that the urlencoded special characters are a lot uglier, which may or 
may not be a consideration for you.


On Wed, Jun 3, 2009 at 12:39 PM, Skip Evans > wrote:




--

Skip Evans
Big Sky Penguin, LLC
503 S Baldwin St, #1
Madison WI 53703
608.250.2720
http://bigskypenguin.com

Those of you who believe in
telekinesis, raise my hand.
 -- Kurt Vonnegut

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Web friendly file names

2009-06-03 Thread Eddie Drapkin
Well, erm, no.

I'd store the filename, etc. as-is in the database, and then link it with
urlencode() and you should be able to serve a file called "A non friendly
name!" site.com/A%20non%20friendly and a modern webserver should be fine
with that, and even most browsers will allow you to type site.com/A non
friendly... without the url escaped characters and it will url escape them
in the request.  The only issue is that the urlencoded special characters
are a lot uglier, which may or may not be a consideration for you.

On Wed, Jun 3, 2009 at 12:39 PM, Skip Evans  wrote:

> You mean like this? This would work as a good file name to be on the server
> and link to?
>
> $filename = urlencode($_FILES['myfile']['name']);
> move_uploaded_file($_FILES['myfile']['tmp_name'], $filename);
>
> Think that would do the trick?
>
> Skip
>
> Eddie Drapkin wrote:
>
>> Why not just urlencode() the filename? (and suggest people use a URL
>> shortening service and/or provide one)
>>
>> On Wed, Jun 3, 2009 at 12:31 PM, Richard Heyes > rich...@php.net>> wrote:
>>
>>Hi,
>>
>> > I have a file uploader module that allows users to upload
>>documents and of
>> > course people are using all kinds of file names that are not web
>>friendly.
>> >
>> > I guess the best solution is to replace any non alphanumeric with
>>maybe '_'
>> > the underscore? How does that sound?
>> >
>> > Unfortunately, after 20+ years of coding I cannot get my brain
>> around
>> > regular expressions to any decent level of proficiency, I know sad.
>> >
>> > I'd like to hear other solutions for this problem, I am thinking
>>of a regexp
>> > that replaces special chars with the underscore; sounds pretty
>>robust and
>> > globally acceptable?
>>
>>I replace any non alpha chat with a hyphen, then replace two or more
>>hyphens with one. Simple, but I would also include the date so that
>>naming clashes are less likely (if it's applicable). So you might end
>>up with something similar to this:
>>
>>/product/2009/06/03/24ct-gold-earrings
>>
>>Or if using the date is not applicable, you could get something like
>>this:
>>
>>/product/24ct-gold-earrings
>>
>>--
>>Richard Heyes
>>HTML5 graphing: RGraph (www.rgraph.net  -
>>updated 23rd May)
>>PHP mail: RMail (www.phpguru.org/rmail )
>>PHP datagrid: RGrid (www.phpguru.org/rgrid
>>)
>>PHP Template: RTemplate (www.phpguru.org/rtemplate
>>)
>>PHP SMTP: http://www.phpguru.org/smtp
>>
>>--
>>PHP General Mailing List (http://www.php.net/)
>>To unsubscribe, visit: http://www.php.net/unsub.php
>>
>>
>>
> --
> 
> Skip Evans
> Big Sky Penguin, LLC
> 503 S Baldwin St, #1
> Madison WI 53703
> 608.250.2720
> http://bigskypenguin.com
> 
> Those of you who believe in
> telekinesis, raise my hand.
>  -- Kurt Vonnegut
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


Re: [PHP] Web friendly file names

2009-06-03 Thread Skip Evans
You mean like this? This would work as a good file name to be 
on the server and link to?


$filename = urlencode($_FILES['myfile']['name']);
move_uploaded_file($_FILES['myfile']['tmp_name'], $filename);

Think that would do the trick?

Skip

Eddie Drapkin wrote:
Why not just urlencode() the filename? (and suggest people use a URL 
shortening service and/or provide one)


On Wed, Jun 3, 2009 at 12:31 PM, Richard Heyes > wrote:


Hi,

 > I have a file uploader module that allows users to upload
documents and of
 > course people are using all kinds of file names that are not web
friendly.
 >
 > I guess the best solution is to replace any non alphanumeric with
maybe '_'
 > the underscore? How does that sound?
 >
 > Unfortunately, after 20+ years of coding I cannot get my brain around
 > regular expressions to any decent level of proficiency, I know sad.
 >
 > I'd like to hear other solutions for this problem, I am thinking
of a regexp
 > that replaces special chars with the underscore; sounds pretty
robust and
 > globally acceptable?

I replace any non alpha chat with a hyphen, then replace two or more
hyphens with one. Simple, but I would also include the date so that
naming clashes are less likely (if it's applicable). So you might end
up with something similar to this:

/product/2009/06/03/24ct-gold-earrings

Or if using the date is not applicable, you could get something like
this:

/product/24ct-gold-earrings

--
Richard Heyes
HTML5 graphing: RGraph (www.rgraph.net  -
updated 23rd May)
PHP mail: RMail (www.phpguru.org/rmail )
PHP datagrid: RGrid (www.phpguru.org/rgrid
)
PHP Template: RTemplate (www.phpguru.org/rtemplate
)
PHP SMTP: http://www.phpguru.org/smtp

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




--

Skip Evans
Big Sky Penguin, LLC
503 S Baldwin St, #1
Madison WI 53703
608.250.2720
http://bigskypenguin.com

Those of you who believe in
telekinesis, raise my hand.
 -- Kurt Vonnegut

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Web friendly file names

2009-06-03 Thread Eddie Drapkin
Why not just urlencode() the filename? (and suggest people use a URL
shortening service and/or provide one)

On Wed, Jun 3, 2009 at 12:31 PM, Richard Heyes  wrote:

> Hi,
>
> > I have a file uploader module that allows users to upload documents and
> of
> > course people are using all kinds of file names that are not web
> friendly.
> >
> > I guess the best solution is to replace any non alphanumeric with maybe
> '_'
> > the underscore? How does that sound?
> >
> > Unfortunately, after 20+ years of coding I cannot get my brain around
> > regular expressions to any decent level of proficiency, I know sad.
> >
> > I'd like to hear other solutions for this problem, I am thinking of a
> regexp
> > that replaces special chars with the underscore; sounds pretty robust and
> > globally acceptable?
>
> I replace any non alpha chat with a hyphen, then replace two or more
> hyphens with one. Simple, but I would also include the date so that
> naming clashes are less likely (if it's applicable). So you might end
> up with something similar to this:
>
> /product/2009/06/03/24ct-gold-earrings
>
> Or if using the date is not applicable, you could get something like this:
>
> /product/24ct-gold-earrings
>
> --
> Richard Heyes
> HTML5 graphing: RGraph (www.rgraph.net - updated 23rd May)
> PHP mail: RMail (www.phpguru.org/rmail)
> PHP datagrid: RGrid (www.phpguru.org/rgrid)
> PHP Template: RTemplate (www.phpguru.org/rtemplate)
> PHP SMTP: http://www.phpguru.org/smtp
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


Re: [PHP] Web friendly file names

2009-06-03 Thread Richard Heyes
Hi,

> I have a file uploader module that allows users to upload documents and of
> course people are using all kinds of file names that are not web friendly.
>
> I guess the best solution is to replace any non alphanumeric with maybe '_'
> the underscore? How does that sound?
>
> Unfortunately, after 20+ years of coding I cannot get my brain around
> regular expressions to any decent level of proficiency, I know sad.
>
> I'd like to hear other solutions for this problem, I am thinking of a regexp
> that replaces special chars with the underscore; sounds pretty robust and
> globally acceptable?

I replace any non alpha chat with a hyphen, then replace two or more
hyphens with one. Simple, but I would also include the date so that
naming clashes are less likely (if it's applicable). So you might end
up with something similar to this:

/product/2009/06/03/24ct-gold-earrings

Or if using the date is not applicable, you could get something like this:

/product/24ct-gold-earrings

-- 
Richard Heyes
HTML5 graphing: RGraph (www.rgraph.net - updated 23rd May)
PHP mail: RMail (www.phpguru.org/rmail)
PHP datagrid: RGrid (www.phpguru.org/rgrid)
PHP Template: RTemplate (www.phpguru.org/rtemplate)
PHP SMTP: http://www.phpguru.org/smtp

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Web friendly file names

2009-06-03 Thread Skip Evans

Hey all,

I have a file uploader module that allows users to upload 
documents and of course people are using all kinds of file 
names that are not web friendly.


I guess the best solution is to replace any non alphanumeric 
with maybe '_' the underscore? How does that sound?


Unfortunately, after 20+ years of coding I cannot get my brain 
around regular expressions to any decent level of proficiency, 
I know sad.


I'd like to hear other solutions for this problem, I am 
thinking of a regexp that replaces special chars with the 
underscore; sounds pretty robust and globally acceptable?


Opinions, witticisms, flames, quotations by famous débutantes?

(The last is optional)
--

Skip Evans
Big Sky Penguin, LLC
503 S Baldwin St, #1
Madison WI 53703
608.250.2720
http://bigskypenguin.com

Those of you who believe in
telekinesis, raise my hand.
 -- Kurt Vonnegut

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php