• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 248
  • Last Modified:

php code to copy files from one internal server to another

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.

daver
0
David Bird
Asked:
David Bird
1 Solution
 
gr8gonzoConsultantCommented:
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}");
file_put_contents($file,$data);

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)
{
   if(file_exists($file))
   {
      readfile($file);
   }
}
0
 
Dave BaldwinFixer of ProblemsCommented:
You'll need something like what @gr8gonzo suggests because PHP on the web server will not have permissions on the document server.
0
 
David BirdPartnerAuthor Commented:
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.  

thanx
daver
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
gr8gonzoConsultantCommented:
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:
\\YOURDOCSERVER\SharedFiles

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

copy("\\\\YOURDOCSERVER\\SharedFiles\\abc.pdf","abc.pdf");

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.
0
 
David BirdPartnerAuthor Commented:
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.  

daver
0
 
InsoftserviceCommented:
<?php
$filename = "test.txt" ;
$dest = "/home/user/htdocs/upload/" . $filename ;
exec("scp $filename remoteuser@some.remove.server:$dest", $output) ;
print_r($output) ;
?>
0
 
gr8gonzoConsultantCommented:
@insoft - that is for Linux servers
0
 
David BirdPartnerAuthor Commented:
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.

daver
0
 
David BirdPartnerAuthor Commented:
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.
0
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.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now