Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to trim file path from filename

14 views
Skip to first unread message

andyau

unread,
Mar 31, 2008, 9:49:54 AM3/31/08
to
I am uploading a file to the server and at the same time I want to
place the file name in to a database.

But for the the file to be effective I need to trim the path from the
filename and leave just the filename.

Can anyone suggest how I do this?

It's all part of a google maps mash up.

The page can be found here.

http://www.streetartlocator.com/dev/add_location_3.php

Thanks in advance.

Andy

Bart Van der Donck

unread,
Mar 31, 2008, 11:24:43 AM3/31/08
to
andyau wrote:

> I am uploading a file to the server and at the same time I want to
> place the file name in to a database.
>
> But for the the file to be effective I need to trim the path from the
> filename and leave just the filename.
>
> Can anyone suggest how I do this?

The traditional directory separator is regular slash ('/'), but Winoid
systems generally use backslash ('\'). But a file or directory name in
UNIX may contain backslash as well.

One way is to detect which of the slashes comes first, and take that
as separator for what follows. This should be a reasonably safe
assumption (I can't think of an alternative criterion).

Hope this helps,

--
Bart

hakimoun

unread,
Mar 31, 2008, 11:53:27 AM3/31/08
to
in what language are you doing the job on the server side ?
if you do that with php the file name is in $_FILES['file']
['name'] ... I think


Tom Cole

unread,
Mar 31, 2008, 12:08:50 PM3/31/08
to

I don't think PHP filesystem functions has a method to return filepath
separators (for example java does...) so this method is probably the
best way to go. I would typically perform this function on the server
side (rather than requiring that javscript will be enabled, which is
only 90% of the time on average)...but...

function upload() {
try {
var file = document.getElementById("id_of_input_element").value;
var filename = "";
if (file.indexOf("/")) {
filename = file.substring(file.lastIndexOf("/") + 1,
file.length);
}
else {
filename = file.substring(file.lastIndexOf("\\") + 1,
file.length);
}
var form = document.getElementById("id_of_form_element");
//var form = document.getElementsByName("name_of_form_element")
[0];
var fileNameInput = document.createElement("input");
//This may not be required, I believe "text" is default...
input.type = "text";
input.name = "filename";
input.id = "filename";
input.value = filename;
form.appendChild(input);
form.submit();
return true;
}
catch(e) {
alert(e.message);
return false;
}
}

Then update your form to include an onsubmit function:

<form ... onsubmit="return upload();">
...
</form>

HTH.

Tom Cole

unread,
Mar 31, 2008, 12:11:24 PM3/31/08
to

Actually you would remove this line... (form.submit()). Sorry.

>     form.submit();
>     return true;
>   }
>   catch(e) {
>     alert(e.message);
>     return false;
>   }
>
> }
>
> Then update your form to include an onsubmit function:
>
> <form ... onsubmit="return upload();">
> ...
> </form>
>

> HTH.- Hide quoted text -
>
> - Show quoted text -


See above comment.

Erwin Moller

unread,
Mar 31, 2008, 12:34:29 PM3/31/08
to
Tom Cole schreef:

> On Mar 31, 11:24 am, Bart Van der Donck <b...@nijlen.com> wrote:
>> andyau wrote:
>>> I am uploading a file to the server and at the same time I want to
>>> place the file name in to a database.
>>> But for the the file to be effective I need to trim the path from the
>>> filename and leave just the filename.
>>> Can anyone suggest how I do this?
>> The traditional directory separator is regular slash ('/'), but Winoid
>> systems generally use backslash ('\'). But a file or directory name in
>> UNIX may contain backslash as well.
>>
>> One way is to detect which of the slashes comes first, and take that
>> as separator for what follows. This should be a reasonably safe
>> assumption (I can't think of an alternative criterion).
>>
>> Hope this helps,
>>
>> --
>> Bart
>
> I don't think PHP filesystem functions has a method to return filepath
> separators (for example java does...)

Hi Bart,

What about this?

[from http://nl2.php.net/dir]
Predefined Constants

The constants below are defined by this extension, and will only be
available when the extension has either been compiled into PHP or
dynamically loaded at runtime.

DIRECTORY_SEPARATOR (string)
PATH_SEPARATOR (string)

Regards,
Erwin Moller

Henry

unread,
Mar 31, 2008, 12:37:12 PM3/31/08
to
On Mar 31, 5:08 pm, Tom Cole wrote:
<snip>

> I don't think PHP filesystem functions has a method to return
> filepath separators (for example java does...)
<snip>

But the Java method returns the file path separator in the environment
where the JVM is executing, so probably a server where we are
concerned, but the file path originates from the browser client and so
its separator may be tied client's OS.

Tom Cole

unread,
Mar 31, 2008, 1:36:57 PM3/31/08
to

You are correct. Sorry about that.

I would still recommend using server side code rather than javascript
on the client for making this split. The package reference by Erwin
Moller above would be how I would probably proceed with something like
this.

andyau

unread,
Mar 31, 2008, 2:01:48 PM3/31/08
to
I am passing the variable using javascript via a string to the insert
script which is php.
So yeah I could pass the long path then trim it before database
insertion.

Thanks heaps for helping me think this through.

Cheers
Andy

Bart Van der Donck

unread,
Mar 31, 2008, 4:53:08 PM3/31/08
to
Tom Cole wrote:

> I would still recommend using server side code rather than javascript
> on the client for making this split. The package reference by Erwin
> Moller above would be how I would probably proceed with something like
> this.

Me too. And it is a somewhat uncommon task for (client-side)
javascript. Normally a server program would parse this kind of data.

--
Bart

Evertjan.

unread,
Mar 31, 2008, 5:08:33 PM3/31/08
to

Indeed, but that can be done using serverside javascript
as requested and on topic:

<% // javascript

var url = 'http://aa.xx/bb/cc/dd.html';

var temp = url.split('/');
var file = temp.pop();
temp.push('');
var path = temp.join('/');

%>


--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

Bart Van der Donck

unread,
Mar 31, 2008, 5:21:24 PM3/31/08
to
Tom Cole wrote:

> function upload() {
>   try {
>     var file = document.getElementById("id_of_input_element").value;
>     var filename = "";
>     if (file.indexOf("/")) {

This flag always returns true.

if (file.indexOf("/") != -1)

>         filename = file.substring(file.lastIndexOf("/") + 1,
> file.length);
>     }

Clever :) [*]

>     else {
>         filename = file.substring(file.lastIndexOf("\\") + 1,
> file.length);
>     }
>     var form = document.getElementById("id_of_form_element");
>     //var form = document.getElementsByName("name_of_form_element")
> [0];
>     var fileNameInput = document.createElement("input");
>     //This may not be required, I believe "text" is default...
>     input.type = "text";
>     input.name = "filename";
>     input.id = "filename";
>     input.value = filename;
>     form.appendChild(input);

I would prefer a hidden field from the beginning, and then do:

document.getElementsById('ID_of_hidden_element').value = filename;

[*] Mac appears to use (or have used) colon as directory separator.
I'm not sure to which extent this is still relevant today.

Cheers,

--
Bart

Bart Van der Donck

unread,
Mar 31, 2008, 5:26:13 PM3/31/08
to
"Evertjan." wrote:

> Bart Van der Donck wrote:

>> And it is a somewhat uncommon task for (client-side) javascript.
>> Normally a server program would parse this kind of data.
>
> Indeed, but that can be done using serverside javascript
> as requested and on topic:
>
> <%    // javascript
>
> var url = 'http://aa.xx/bb/cc/dd.html';
>
> var temp = url.split('/');
> var file = temp.pop();
> temp.push('');
> var path = temp.join('/');
>
> %>

For an URI, yes. But the specifications of a system path are
considerably different (and OS-dependent):

http://en.wikipedia.org/wiki/URI
http://en.wikipedia.org/wiki/Path_(computing)

Cheers,

--
Bart

Thomas 'PointedEars' Lahn

unread,
Apr 2, 2008, 4:32:09 PM4/2/08
to
Bart Van der Donck wrote:
> The traditional directory separator is regular slash ('/'), but Winoid
> systems generally use backslash ('\'). But a file or directory name in
> UNIX may contain backslash as well.

That appears not to apply to all Unices:

$ touch '1/2'
touch: cannot touch `1/2': No such file or directory
$ touch '1\/2'
touch: cannot touch `1\\/2': No such file or directory
$ mkdir '1/2'
mkdir: cannot create directory `1/2': No such file or directory
$ mkdir '1\/2'
mkdir: cannot create directory `1\\/2': No such file or directory
$ uname -a
Linux pointedears 2.6.17.13-20060915.184752+0200 #1 PREEMPT Fri Sep 15
19:07:16 CEST 2006 i686 GNU/Linux


PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann

Bart Van der Donck

unread,
Apr 3, 2008, 4:26:14 AM4/3/08
to
Thomas 'PointedEars' Lahn wrote:

> Bart Van der Donck wrote:
>
>> But a file or directory name in
>> UNIX may contain backslash as well.
>
> That appears not to apply to all Unices:
>
> $ touch '1/2'
> touch: cannot touch `1/2': No such file or directory
> $ touch '1\/2'
> touch: cannot touch `1\\/2': No such file or directory
> $ mkdir '1/2'
> mkdir: cannot create directory `1/2': No such file or directory
> $ mkdir '1\/2'
> mkdir: cannot create directory `1\\/2': No such file or directory
> $ uname -a
> Linux pointedears 2.6.17.13-20060915.184752+0200 #1 PREEMPT Fri Sep 15
> 19:07:16 CEST 2006 i686 GNU/Linux

Your test only indicates that UNIX filenames cannot contain forward
slash. This is logical, because that character is reserved as
directory separator. I don't see any problems with backslash here:

%ls -l
%touch '1\2'
%ls -l
total 0
-rw-r--r-- 1 bart users 0 Apr 3 10:21 1\2
%uname -smr
FreeBSD 4.8-STABLE i386
%

Backslash handling might depend on the shell:

%echo $SHELL
/bin/csh
%

--
Bart

Thomas 'PointedEars' Lahn

unread,
Apr 3, 2008, 1:21:32 PM4/3/08
to
Bart Van der Donck wrote:
> Thomas 'PointedEars' Lahn wrote:
>> Bart Van der Donck wrote:
>>> But a file or directory name in UNIX may contain backslash as well.
^ ^^^^^^^^^
>> That appears not to apply to all Unices: [...]

>
> Your test only indicates that UNIX filenames cannot contain forward
> slash. This is logical, because that character is reserved as directory
> separator. I don't see any problems with backslash here: [...]
^^^^^^^^^
Ahh, now that you mention it again ... Must have been late.

>>> One way is to detect which of the slashes comes first, and take that
>>> as separator for what follows. This should be a reasonably safe
>>> assumption (I can't think of an alternative criterion).

http://php.net/dirname
http://php.net/basename


HTH

PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16

0 new messages