how to trim file path from filename

A

andyau

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
 
B

Bart Van der Donck

andyau said:
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,
 
H

hakimoun

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
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
 
T

Tom Cole

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,

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.
 
T

Tom Cole

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,

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);

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.
 
E

Erwin Moller

Tom Cole schreef:
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
 
H

Henry

On Mar 31, 5:08 pm, Tom Cole wrote:
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.
 
T

Tom Cole

On Mar 31, 5:08 pm, Tom Cole wrote:


<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.

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.
 
A

andyau

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
 
B

Bart Van der Donck

Tom said:
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.
 
E

Evertjan.

Bart Van der Donck wrote on 31 mrt 2008 in comp.lang.javascript:
Me too. 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('/');

%>
 
B

Bart Van der Donck

Tom said:
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,
 
B

Bart Van der Donck

T

Thomas 'PointedEars' Lahn

Bart said:
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
 
B

Bart Van der Donck

Thomas said:
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
%
 
T

Thomas 'PointedEars' Lahn

Bart said:
Thomas said:
Bart said:
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.

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


HTH

PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top