Re: how to trim file path from filename
- From: Tom Cole <tcole6@xxxxxxxxx>
- Date: Mon, 31 Mar 2008 09:08:50 -0700 (PDT)
On Mar 31, 11:24 am, Bart Van der Donck <b...@xxxxxxxxxx> 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...) 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.
.
- Follow-Ups:
- Re: how to trim file path from filename
- From: Henry
- Re: how to trim file path from filename
- From: Erwin Moller
- Re: how to trim file path from filename
- From: Tom Cole
- Re: how to trim file path from filename
- References:
- how to trim file path from filename
- From: andyau
- Re: how to trim file path from filename
- From: Bart Van der Donck
- how to trim file path from filename
- Prev by Date: Re: how to trim file path from filename
- Next by Date: Re: how to trim file path from filename
- Previous by thread: Re: how to trim file path from filename
- Next by thread: Re: how to trim file path from filename
- Index(es):
Relevant Pages
|