php code to copy files from one internal server to another

Posted on 2012-08-20
Medium Priority
Last Modified: 2013-03-14
I have a web page that displays a list of files that users may download from my network. (This is all intranet stuff so at least at this stage I'm not concerned with hacking, injections etc). I want to be able to let the user click on the file link and then have the server side code copy the file from the document server to the download area on the web server.  The user doesn't need to know where the original document came from or what it's original file name is/was.  I've gotten the code functional up to the point where I try to do the actual copy between the servers. I have ftp on the web server but NOT the document server.  I just need to do the copy itself using some form of IO function(s) if possible.  I've already got the code functional to the remote client end once I've gotten the file into the download area.  I just can't get the file to copy from the document server to the download area.

In php, I've attempted to use copy, shell_exec() and exec().  copy just returns false, Shell_exec and exec return 0 files copied. Since I'm shelling out from the web server side, would I still have a rights issue that may be blocking the copy? If so, I'm not aware of where to read/learn about setting up security rights for this process and help would be appreciated.  
I have also written a console executable and run it from the command prompt on the web server (hard coded the copy). It runs and completes normally.  When I run exec("MoveDocument.exe"), nothing happens.  No return value and nothing copies.  

I'm running PHP 5.3.9 on Windows 2k8R2 IIS 7. I've also checked my configuratoin and php safe mode configuration is off.

I know just enough about PHP configurations and IIS configurations to be dangerous. So, if you are able to help, please be explicit enough in your responses to help me find the appropriate documentation pieces to learn about what your suggesting. In my world, I'm a general practitioner, not a specialist.  

Thanx in advance for any thoughts.

Question by:David Bird
LVL 35

Expert Comment

ID: 38314225
I would just use two PHP scripts - one that requests the file and one that responds to the request and sends the file back.

1. Script on web server creates a GET request to the document server and saves it locally:
$file = "abc.pdf";
$key = md5($file."s3cr3tKEE"); // Come up with your own variation to make it more secure
$data = file_get_contents("http://<document server>/filetransfer.php?file={$file}&key={$key}");

2. Script on document server (/filetransfer.php) that receives the request, validates it, and returns the file:
$file = $_GET["file"];
$key = $_GET["key"];
$checkKey = md5($file."s3cr3tKEE");
if($key == $checkKey)
LVL 84

Expert Comment

by:Dave Baldwin
ID: 38314286
You'll need something like what @gr8gonzo suggests because PHP on the web server will not have permissions on the document server.

Author Comment

by:David Bird
ID: 38314380
This is very close but <document server> is not a webserver so I can't assign the $data variable using $data = file_get_contents("http://<document server>/filetransfer.php?file={$file}&key={$key}");

My goal is to get a file from this member server <document server> and copy it TO a web server where it will then be sent to a remote client.  My apologizes for poorly communicating my issue.  

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 35

Expert Comment

ID: 38314388
In that case, assuming they are both Windows servers on the same network, I would set up a network share on the document server, like:

Then, on the web server, create a script that uses copy():


Just bear in mind that the web server process is running as a specific account and that account will need write privileges to the folder that you want to contain the transferred file, and will need to read privileges on the "SharedFiles" network share.

Author Comment

by:David Bird
ID: 38314407
Thank you. I will google a bit more to learn how to setup the web server process rights on the Webserver and docserver so it can access the files on the docserver. I will update you if/when I have a better understanding so the ticket doesn't go stale.  Thank you all for responding so quickly.  

LVL 15

Expert Comment

ID: 38315580
$filename = "test.txt" ;
$dest = "/home/user/htdocs/upload/" . $filename ;
exec("scp $filename remoteuser@some.remove.server:$dest", $output) ;
print_r($output) ;
LVL 35

Expert Comment

ID: 38315679
@insoft - that is for Linux servers

Accepted Solution

David Bird earned 0 total points
ID: 38322785
Still working the deal. I found where to add a domain user to the IIS process worker and the w3wp.exe worker process shows the user in task managers processes.  But I'm still not getting a file copy.  If there is a debugging tool I should/could be using to see what the error is other than pass fail, please advise.  Otherwise, I'm still actively pursuing a solution.  thanks all.


Author Closing Comment

by:David Bird
ID: 38984283
Still working but don't want to keep an open question for what may take me a while to resolve. Thanks to everyone for their help.

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you are a web developer, you would be aware of the <iframe> tag in HTML. The <iframe> stands for inline frame and is used to embed another document within the current HTML document. The embedded document could be even another website.
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses
Course of the Month16 days, 15 hours left to enroll

864 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