Folder Select dialog on server using PHP/Javascript/Ajax

I need a way to display an open for the user to show a list of folders on the server. I know, sounds dangerous but this is my local server and I am writing a php script to add a number (prepend) to each file in a certain folder. I could hard code the folder name but there are hundreds of folders so that rules that out. It is a windows machine and I have wampserver setup and running.

The folders are located on a different drive than the server. I wrote a program to do this in a stand alone exe file using Delphi but needed to modify it and no longer have delphi. So, in short

1: The script would be PHP and use ajax or javascript to allow me to choose a folder.
2: Once choosen, I need to have that folder name returned into a variable ie: $folder_name =
3: Once I have the folder name I need to be able to rename each file with a number so "thisfile.mp3" would become "01 - thisfile.mp3"

Finally, there are two filetypes of each file in the folders. There is an .mp3 and an .cdg file both of the same name.
So the might be 100 songs all .mp3 and 100 .cdg files with the same name as the song.mp3 files. eg:

song3.cdg  etc.....

Some of the files may have a number on them already or something else that I need to strip first.
I need to be able to strip characters from the beginning of the file in one operation (if I need to) then run a second op which would append the numbers to each file as above.

This will not be done over the web but just on my local machine (and the files are on a usb hard drive).
Any assistance would be appreciated.
LVL 20
Mark BradyPrincipal Data EngineerAsked:
Who is Participating?
Matthew KellyCommented:
Do you think the mapped drive option will work? Otherwise you will need to code the "popup window" in PHP (something like this:

I don't see anything from searching that would allow a web file browser dialog box to access server files.
Matthew KellyCommented:
Would this code help you at all?

It recursively loops through a given folder outputting the contents. You could expand the code to programmatically rename the files, by recursively looping through and renaming the files in a specified folder, etc based on button clicks. Let me know if this helps get you started.
Mark BradyPrincipal Data EngineerAuthor Commented:
Thanks but that code does not open anything to display a list of folders to choose from. If you were to put a form input field on a page like <input type="file"> etc.... that opens a dialog to select files right? Well I don't need to select files, I can do that manually with php script. What I need is a way to tell my script what path the files are in. Your code has the file path hard coded and I can't use that for over 100 folders.
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Matthew KellyCommented:
Correct, you would have to hard code the main directory you want the user to select folders from. I was thinking you could create your own pop-up window that would display the list of folders in that directory, and make the folders clickable links, so the user can select which folder to use, etc. That will allow you to display the folder and contents from the server in a different computers browser, as PHP would be creating the folder list. This may even be more beneficial for you as you can add functionality to the select folder page. It would require you to code a php page that looked like a select file dialog box.

From googling around I did not see how to have the browser select file dialog box display a server folder.

The only possibility I could think to use the browser select file dialog box, since you mentioned this was all local, of would be to map a drive on your computer to the folder on the server, so you could then pick that drive as if it were a local drive from the browser. You could map the same drive (same drive letter) on the server so the php script could loop through and rename the files, etc.

Hope that helps. Let me know if you need help writing the PHP code based on the recursive loop example.
Matthew KellyCommented:
Just putting this out here now just in case it is an issue you haven't resolved, or if you haven't had the PHP code access the network drive yet:

Specifically for Apache on Windows, to access network resources, the user Apache runs as will need to have permissions to those resources. The Apache service defaults to LOCAL SYSTEM user account which does not have network access permissions. To do this:

1. Create a new Windows user. This user account will be used to run the Apache service.
2. Give that user permission to the folders you want access to. It will also need Administrator priviledges on the server Apache runs on. If this is not secure enough for you I am sure there is a list of exact permissions the windows user needs, but start off with Admin group just to make sure it works.
3. go to the Services (Start -> Control Panel -> Admin Tools -> Services) and change the user Apache logs in on to the user account
4. Restart Apache and try to access the resource.
Mark BradyPrincipal Data EngineerAuthor Commented:
Thanks for your suggestions. I can code the PHP loop etc... What I need help with mainly is the coding of the javascript popup window so the user (Me) can select a folder.I will then use that information to put into a variable to use in my code.
Mark BradyPrincipal Data EngineerAuthor Commented:
I've been busy over Christmas but have just looked at the link you sent. It looks like that system could work. I don't want to purchase someone else's code because it isn't what I need but the idea behind it would work. I have also been thinking, it doesn't need to select file directory(s) from the server, it could work by choosing the folders from the client side so instead of putting the folders I wish to work with locally, in the root or 'www' directory, we could use javascript to show a tree like in the link you sent, and from there obviously the code would need to return the filepath so surely there is a way to return that value to a script ?  I'm starting to think this is a difficult problem to sort out and it maybe easier to reinstall Borland Delphi and code up an .exe file to do it.

I appreciate your input though....
Mark BradyPrincipal Data EngineerAuthor Commented:
This is not the solution I was looking for but I do appreciate your input
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.