1

Is there a way to direct the browser to choose only from a certain directory?

This is my script for the browser:

Data Save Directory:<input name="datasavedir" type="file" id="fileUpload" value="Browse">

I am trying to restrict the user to only choose a file form one directory. Is there a way to do this using PHP or Javascript?

Jack
  • 725
  • 1
  • 10
  • 27
  • 6
    " on **my** computer" - O.o ? – Andreas Oct 26 '15 at 17:14
  • 1
    You won't be able to, and the idea won't work anyhow as users can have different directories tree depending on a lot of variable (OS, users, company restrictions etc..) – Florian Oct 26 '15 at 17:17
  • 1
    _" trying to restrict the user to only choose a file form one directory. Is there a way to do this using PHP or Javascript?"_ No, this is not possible – guest271314 Oct 26 '15 at 17:18
  • after all, it's their own computers! – Mi-Creativity Oct 26 '15 at 17:19
  • The file input cannot accept values for security reasons. What you could possibly do is to validate the path upon submission. – Lucas Rodrigues Oct 26 '15 at 17:20
  • 2
    @LucasRodrigues validate `"C:\fakepath\myfile.ext"` ? – Paul S. Oct 26 '15 at 17:21
  • @LucasRodrigues: No you can't. All you can "validate" is the file name. – gen_Eric Oct 26 '15 at 17:23
  • 1
    @MaryE What is purpose ? Is requirement to restrict file selection to specific file type ? – guest271314 Oct 26 '15 at 17:23
  • @guest271314 I was asked to print out the path directory to the file that is chosen by user. I did my research and apprently it can not be done due to security reasons.http://stackoverflow.com/questions/33130219/writing-the-full-path-of-the-file-when-it-is-browsed-uisng-php so I am trying to find a way around it and define a known path that can be browsed and since I know file path I can print it out – Jack Oct 26 '15 at 17:26
  • @MaryE: How would you even define a *known* path? You have no idea what's on my computer. – gen_Eric Oct 26 '15 at 17:27
  • 1
    _"trying to find a way around it and define a known path that can be browsed"_ "browsed" by whom , what ? , How "browsed" ? – guest271314 Oct 26 '15 at 17:28
  • @RocketHazmat Let's assume I know. Would it be possible.I am working on a local server. – Jack Oct 26 '15 at 17:28
  • @MaryE: No, it's not possible. You cannot read/set the path on a *user's* computer when they upload a file. – gen_Eric Oct 26 '15 at 17:28
  • 1
    @RocketHazmat I know all about your `Net#LocalServer::HiddenDrive.$.Secret.Stash` directory – Paul S. Oct 26 '15 at 17:32
  • 2
    @PaulS.: I have no idea what you're talking about :-P (/*renames secret stash*/) – gen_Eric Oct 26 '15 at 17:33
  • @MaryE: Can you explain in the question *exactly* what you are trying to do? Maybe there's something you *can* do, but we can't suggest anything without knowing what your goal here is. – gen_Eric Oct 26 '15 at 17:35
  • 1
    @MaryE Is requirement for user to upload file to an interface , e.g., a `textarea` element, edit file, user then downloads same file , modified by edits, to overwrite original uploaded file at user filesystem ? – guest271314 Oct 26 '15 at 17:39
  • 1
    @RocketHazmat The GUI that I am designing should be able to let the user saves his/her data in a file. The user should be able to see the path of the file that he has chosen. This path should be printed out – Jack Oct 26 '15 at 17:40
  • @MaryE http://stackoverflow.com/questions/30563157/edit-save-self-modifying-html-document-format-generated-html-javascript ? – guest271314 Oct 26 '15 at 17:41
  • @RocketHazmat Since I could not do this, I have thought to restrict the user to save his/her data to a certain file that I allow them too. This way I know already the file path and I can print it out. – Jack Oct 26 '15 at 17:42
  • @MaryE: That's not possible. It *may* be with ActiveX, but then it would only work in IE. – gen_Eric Oct 26 '15 at 17:43
  • OK I am working with UBUNTU – Jack Oct 26 '15 at 17:45
  • @MaryE When user uploads file, `File` object should have property `name` , when user downloads same file , for example, after editing, could set `download` attribute of `a` element to file name of previously uploaded `File` object. If user clicks "Save" when `a` clicked , original file in user filesystem should be overwritten by new file with same file name – guest271314 Oct 26 '15 at 17:46
  • @guest271314 what is important to this project is to set the file path print out. For example: Desktop/mydirectory/text.txt – Jack Oct 26 '15 at 17:49
  • @MaryE Could add `input` for user to insert path. That would be user prerogative to input path on their filesystem. Cannot force user to disclose folders, files, file paths on user filesystem. Why is printing file path important ? – guest271314 Oct 26 '15 at 17:51
  • @guest271314 OK will do that..thanks – Jack Oct 26 '15 at 17:52
  • @guest271314 Because another GUI will read the data form this file and generate a graph from it – Jack Oct 26 '15 at 17:54
  • @guest271314 and it needs the file path and I need to save the path somehow – Jack Oct 26 '15 at 17:55
  • @guest271314: But the user would still have to browse to the folder to save the file. – gen_Eric Oct 26 '15 at 17:55
  • @MaryE: I don't think any of this is actually possible. You cannot read/write to files on the filesystem without the user selecting it in a OS-controlled popup. – gen_Eric Oct 26 '15 at 17:55
  • 1
    @RocketHazmat _"But the user would still have to browse to the folder to save the file."_ Yes, exact application not clear ; though if user decides to upload a certain file path on their filesystem, at their own discretion, could do so. Note: Not recommended – guest271314 Oct 26 '15 at 18:01

2 Answers2

0

No, you cannot restrict the client from choosing directories. The user will be able to choose from any directory from the computer. It's not up to you to decide what the user can view in his/her computer.

taxicala
  • 21,408
  • 7
  • 37
  • 66
  • Is there a way to have a default directory opened up when the browse button is hit? – Jack Oct 26 '15 at 17:20
  • 1
    @MaryE: No, there isn't. What directory would you even want? Mac OS X, Windows, and Linux all use *different* directory trees. – gen_Eric Oct 26 '15 at 17:21
  • I was asked to print out the path directory to the file that is chosen by user. I did my research and apprently it can not be done due to security reasons.http://stackoverflow.com/questions/33130219/writing-the-full-path-of-the-file-when-it-is-browsed-uisng-php – Jack Oct 26 '15 at 17:24
  • So I thought maybe I can define a known path and then I can print out that. – Jack Oct 26 '15 at 17:24
  • 1
    What is purpose of retrieving file path of selected file from user filesystem ? – guest271314 Oct 26 '15 at 17:26
  • I am working on a local server. So I want them to be able to save their work in a known directory. Like desktop. – Jack Oct 26 '15 at 17:28
  • 1
    _"am working on a local server. So I want them to be able to save their work in a known directory."_ `input type="file"` uploads file . Not certain what "save their work" means ? Can include `js` , description of application at Question ? – guest271314 Oct 26 '15 at 17:31
0

var inputs = document.querySelectorAll("input");
inputs[1].onclick = function() {
  console.log(inputs[0].value)
}
<label>Input file path (at own discretion): <input type="text" /></label>
<input type="button" value="click" />
guest271314
  • 1
  • 15
  • 104
  • 177