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
> 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...) 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.
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.
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
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.
Thanks heaps for helping me think this through.
Cheers
Andy
> 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
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)
> 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 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
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 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
>>> 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