Jump to content

[SOLVED] Getting the file path from a html form input


tudoroprea

Recommended Posts

I have the following problem: I want to get the file path from a file input field, not just the filename. I don't have to necesarily use the input of type 'file', but that is the only way i know how to have a browse button.I tried using javascript to fetch the content of the file with this function:

function GetDirectory()

{

strFile = document.FileForm.filename.value;

intPos = strFile.lastIndexOf("\\");

strDirectory = strFile.substring(0, intPos);

alert(strDirectory);

}

but i don't know how to pass the javascript variable to the php.

Can anyone help me ?

Link to comment
Share on other sites

hi tudoroprea,

to upload the file, you would need to have a file type field

the array passed to your process page, $_FILES does not, as i think you know,

contain any of the info where the file came from. generally

that information is not needed, but sounds like you want it,

so this solution might work for you:

<script type="text/javascript">
function GetDirectory()
{
   strFile = document.FileForm.filename.value;
   intPos = strFile.lastIndexOf("\\");
   strDirectory = strFile.substring(0, intPos);
   //alert(strDirectory);
   document.FileForm.Directory.value = strDirectory;
}

</script>
<input type="file" id="filename" name="filename" value="" onChange="GetDiretory()">
<input type="hidden" id="Directory" name="Directory" value="">

 

good luck

Link to comment
Share on other sites

yes, it works.

thank you very much it was exactly what i needed.

Only one small problem. When i get the path it has 2 slashes betwin the directories, not one as it should. Like this:

C:\\tudor\\photos\\album1.

But this is not a big problem as i can replace the \\ with \.

 

Thank you again.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.