Solved

How to copy files from a client computer to a server using php.

Posted on 2011-03-08
19
985 Views
Last Modified: 2012-05-11
I am about to start a project that allows the user to open a webpage (from a server in our LAN) to copy a file from his/her computer to another computer (or the server).

I would like to use html, php, apache, from a windows 2008 server and maybe js.

The idea is that the user would be able to get to the webpage from any place in the world via VPN, and upload a file to the target computer.  But the uploading of the file should be "automatic"; that is, I should not have to go through the standard upload file procedure.  The program will simply look in a pre-specified directory and ask which file to process it finds there. "processing" requires renaming the file as it is copied or moved.

The user will have the option of:
1) moving the file: which means the file must be copied (and renamed) then deleted from the client computer
2) copy the file: which means the file must be copied (and renamed) AND renamed on the client computer
3) copy and leave the file: the file must be copied (and renamed) the original file is left alone on the client computer

The question is....how can I do this?  I am pretty sure that php will not let me do the remote file manipulation, so I am looking for a solution to do this.

I do not want to use any pre-installed client side programs to achieve this.

I know the names/ips of the client computers and their user login and pw (if that helps).

So, my crazy idea would be....
1) SOMEHOW the server would check the source directory from the client computer
2) the user would complete the options
3) the server would copy the files from client to the server. How? I was thinking the php will create a batch file that would then be executed via php file.  It would be that batch file that would do the file manipulation.

Anybody have any other ideas? (I'm not looking for code....yet)
0
Comment
Question by:MoeMoe7
  • 10
  • 8
19 Comments
 
LVL 7

Expert Comment

by:Gewgala
Comment Utility
It sounds like you are asking for a lot of features that are simply not available to the web world.  For example, you can't have a website manipulate files on someone's machine.  At least, not that I am aware of.  This violates security protocols.  It sounds like what you are wanting is an actual application, either some sort of active x control or an actual program running on someone's computer.  Going this route you would indeed be able to accomplish what you wish, but it would not be strictly web.
0
 
LVL 5

Expert Comment

by:onemadeye
Comment Utility
YOU: So, my crazy idea would be....

That bold part is right ... :)
0
 

Author Comment

by:MoeMoe7
Comment Utility
One thing I really really want to get away from is from installing anything on the client computers.  This is just an administrative nightmare for me when I have to apply changes and the like.

Thus, I was hoping that a php + batch + js approach might do the trick.  The php would handle the user interaction, the batch would handle the remote file/folder accesses, and js might handle some client side interaction...which I don't think would be necessary unless it would be used to execute a batch file also.

Obviously this would only work within our network using our computers as the batch file must be explicit in it's file path definitions, but I can control that from within.

This would definitely not work from an unknown computer.

What do you guys thinks?   errr...gals too.
0
 
LVL 7

Expert Comment

by:Gewgala
Comment Utility
You still wouldn't be able to get PHP to execute a batch file.  You would need some kind of service or app running in the background on the user's machine that would look for a db value change periodically for example.  That db flag value could be executed by PHP, certainly.  But the service or app would then execute the batch file or similar command(s) depending on the new info it grabbed from a db.
0
 

Author Comment

by:MoeMoe7
Comment Utility
php can't execute a batch file?  I thought it could (although I have not actually tried it.  ;)

So, you mean the code below would not work to execute a batch file?

     $filename_bat = "c:\Batch\get_users.bat";
 
     if ( file_exists($filename_bat) )
     {
           exec($filename_bat,$output);
           echo "<pre>".var_export($output, TRUE) . "</pre>\\n";
     }
     else
     {
           echo "FILE DOES NOT EXIST";
     }

this code was lifted from another post here on experts exchange: ID:22637935

would then do all the copying/moving work.
0
 
LVL 7

Expert Comment

by:Gewgala
Comment Utility
Well I'm referring to the security of the web again.  You are accessing a file on the c drive in your above example.  This is the c drive of the actual server hosting the PHP code, it is not the c drive of the user's machine.  You can't have PHP on a server access the C: on a user's machine.  You can have it access a file on the same machine the code is running on but I believe you have to grant permissions to allow file access outside the directory tree of the website itself.

Unless I'm mistaken, it sounds like what you really want is for PHP to execute the batch file on the user's machine, right?  

...Or... maybe a light just illuminated my brain and I now understand what you want, and that is that you want the batch file on the server to be executed, and the server has access to the user computers that exist on the same network, and running that batch file will cause file manipulation on individual user computers which was executed by the server?
0
 
LVL 7

Expert Comment

by:Gewgala
Comment Utility
If the last part of my previous post (the light in my brain bit) is in fact what you're after, then I believe that should work fine simply because you are on a local intranet where security settings could be manipulated to allow this to work (if they need to be manipulated at all).
0
 

Author Comment

by:MoeMoe7
Comment Utility
zactly!  the batch file would be on the server and it (the .bat) would manipulate the files on the client computer.
0
 
LVL 7

Expert Comment

by:Gewgala
Comment Utility
Ok then, to sum up your original 3 TODO's:

1) SOMEHOW the server would check the source directory from the client computer

You could have file upload controls to allow the user to do specify which files they want, and when they click submit you would only grab the full paths from each of the files (not the files themselves) and use them in your batch file creation before the batch file is executed.

2) the user would complete the options

3) the server would copy the files from client to the server. How? I was thinking the php will create a batch file that would then be executed via php file.  It would be that batch file that would do the file manipulation.

Yes, your batch file would be customized for each user (perhaps named uniquely, or in a unique user folder on the server) and then executed, and the batch file would actually be what manipulates the files like you had originally speculated.  It could be customized with which files to grab/delete etc. by the info gathered in step #1 and execute depending on options specified in step #2.

I think you pretty much have it.  This sounds like it would work fine.
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 7

Expert Comment

by:Gewgala
Comment Utility
Regarding your step #1, I don't recall off the top of my head if PHP will give you the full path information of files in fileupload controls.  It might just give you the filenames only.  Regardless though, you could have a pre-defined source directory in a database that is looked up by ip address (or user login name, etc) when visiting your page.  This would make it so you take the source path in the db and concatenate the filenames that were specified by the user and bam, you have the full path for each file.
0
 

Author Comment

by:MoeMoe7
Comment Utility
Well, the way I have thought of it...I want to force the users to keep the working files  in a very specific directory (I have access to a very large whip in my office for just such occasions), so the need to know what that path is is not really important as I can simply keep that in an XML file.

As for the upload control itself, it would not really work in my case because, well, I need to have the php file NOT ask the user to select a file...thus ANYTHING in that directory is automatically selected.  So, I think I need to have to:

1) php will run a batch file that gets the names of the file(s) in the client computer and reports it back to the php file ****or CAN a batch file do that? otherwise the batch file will have to dump the output to a text file that the php file will then have to parse to get the filenames....phew!

2) then the user will have to set options...

yada yada yada....

but it is step 1 that worries me now.

In reality I was hoping for a less round-about way of doing this without installing on the client computer.


0
 
LVL 7

Expert Comment

by:Gewgala
Comment Utility
Yeah then you would in fact need PHP to execute a batch file on page load that does just that -- reads the files in the directory, dumps to a text file, and then PHP reads the text file to get the actual file names.

Although a mouth full it is, the only real trick would be to get PHP to know when to read the contents of that text file.  As you suspected, there's no way for the batch file to tell PHP that it's ready.  You would have to have javascript on your page that executes an ajax request for example on regular intervals of say 3 - 5 seconds for PHP to check to see if the specified text file is ready to go.  It wouldn't have to be ajax, you could have an iframe on the page for example that does the regular interval ping to cause php to check, and then depending on what php returns to that iframe javascript could then tell the parent document to reload or notify to display the next step(s) to the user.  I personally would go the ajax route.

It seems like JavaScript will in fact be necessary afterall :)
0
 

Author Comment

by:MoeMoe7
Comment Utility
Ok, so get this:

User goes to web page..php executes
10 php deletes previous dump file (if there)
20 php executes batch file
30 php pauses 10 seconds
35 meanwhile, batch file reads remote directory and creates dump file
40 php resumes a looks for dump file...not there? goto 30
50 php reads dump file
60 php deletes dump file
70 code continues.....

I really miss old time basic...it seemed so basic!

Anyway, the above should work and no bs, sorry js needed.   No?
0
 

Author Comment

by:MoeMoe7
Comment Utility
I guess I should be careful about the log-ins then.  I would not want the code getting confused about which dump file is which.

0
 
LVL 7

Accepted Solution

by:
Gewgala earned 500 total points
Comment Utility
The only reason I mentioned js is in case you wanted to display something to the user while they waited for the batch file to check their source directory and for PHP to read it's contents.  The step "30 php pauses 10 seconds" in particular.  If you're going to have PHP continue execution within the same browser request then the user would just see a white screen until it was done.  That is indeed the most basic route and with that the least amount of potential headaches.  If however you want the user to know what's going on while they waited, then you would either need ajax or an iframe that does the work.

In regards to not getting the dump files mixed up, that's why I had mentioned perhaps you should use the ip address in the names of the batch files.  If each user always has their same unique computer than that could be a good way to never get mixed up.

As far as your text specs/mockup goes, that looks like it'll do the job.
0
 

Author Comment

by:MoeMoe7
Comment Utility
Excellent point!  I think I will display something first like "Please wait while I retrieve file info from your computer"
then if there is a failure after 30 seconds, it would display a failure message.

Thanks for all the good input!!
0
 
LVL 7

Expert Comment

by:Gewgala
Comment Utility
Happy coding :)
0
 

Author Comment

by:MoeMoe7
Comment Utility
Thank you for your help, but as you know coding involves a lot of gnashing of teeth, pulling of hair, pounding of desktops and swearing.

Seldom is it "happy".....only when it is done...and then you realize how you SHOULD have done it!
0
 
LVL 7

Expert Comment

by:Gewgala
Comment Utility
lol.  All too true!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now