Re: how to trim file path from filename



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



Relevant Pages

  • Re: how to trim file path from filename
    ... But for the the file to be effective I need to trim the path from the ... filename and leave just the filename. ... The traditional directory separator is regular slash, ... UNIX may contain backslash as well. ...
    (comp.lang.javascript)
  • Re: how to trim file path from filename
    ... filename and leave just the filename. ... UNIX may contain backslash as well. ... 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) ...
    (comp.lang.javascript)
  • Re: applying awk95.awk program on full pathname file
    ... using %filename% awk95.exe. ... How can I implement the backslash inside the full path name variable ... because it is translated wronlgy inside the variable %filename%? ... There are just a few commands that get confused, usually commands ...
    (comp.lang.awk)
  • Re: N00b question: creating files
    ... > backslash is an escape. ... Assuming the your filename was in quotes it would ...
    (comp.lang.php)
  • Re: A different unlink problem.
    ... I think the backslash is normally used to allow an invalid char in a ... filename by prefixing with \. ... Prev by Date: ...
    (comp.sys.ibm.as400.misc)