tudoroprea Posted May 17, 2007 Share Posted May 17, 2007 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 ? Quote Link to comment Share on other sites More sharing options...
freakstyle Posted May 17, 2007 Share Posted May 17, 2007 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 Quote Link to comment Share on other sites More sharing options...
tudoroprea Posted May 17, 2007 Author Share Posted May 17, 2007 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.