Hi guys,
I'm having a problem with uploading file to another server, I guess move_uploaded_file function doesn't work remotely, atleast for me.
Whats wrong here? pls lend me ur hands for a while.. 🙂

TIA

  $numoffile = 2; 
  $file_dir  = "http://www.mysite.com/upload_files/"; 
  if ($_POST) 
{ 


for ($i=0;$i<$numoffile;$i++) 
{ 
      if (trim($_FILES['myfiles']['name'][$i])!="") 
  { 
	    $filenameDB = $_FILES['myfiles']['name'][$i]; 
		$Extenz = substr($filenameDB, -3, 3);


if($Extenz=='zip')
{

    $newfile = $file_dir.$_FILES['myfiles']['name'][$i]; 
    @move_uploaded_file($_FILES['myfiles']['tmp_name'][$i], $newfile); 

    $j++; 

}

}
}

}

    I could be mistaken, but I think you'd have to use CURL to get that file to upload to a remote location like that.

      Hi Stasonis,
      thanks for ur reply, I'm not really familier with CURL, if u know abt it pls clear that to me, thanks.

        I've never personally used CURL, but I know it would allow you to generate an HTTP request to upload that file to the remote server (Assuming the remote server is set up to receive uploaded files correctly)

          You can't write a file into a web server in this manner.

          To write a file to a web server, you typically need to use some other (non-HTTP) protocol to transfer it.

          You'd need to invoke whatever is needed to perform that transfer. Use move_uploaded_file locally to put it into another temporary directory before you do this.

          You might you FTP, in which case you could use the fopen wrapper for FTP. Alternatively, you might have used the operating system's facilities to map the remote server into the local filesystem, in which case you MIGHT use a NFS path on Unix, or a UNC path or mapped drive letter on Windows. This would be entirely up to your systems administrator and it would be their job to set it up and ensure that it worked correctly.

          Mark

            thanks for ur reply Mark,
            darn, I was really hoping things would be little easiler if this trick could be done using (HTTP) protocol to transfer it(php upload), mapping to remote server is not a good idea, since the webserver has lotz of rules, thats the reason thought to save my file on my server. Ah.. thanks anyway guys... I wonder if there anyone who figureout a way to do this trick without FTP/SSH.

              If you have PHP available on both servers, you could engineer some system whereby one PHP script posts the data to another. However, this would require quite a lot of work, particularly on security and binary correctness.

              I don't know how hard it is to create a HTTP multipart POST with PHP, but I do wonder why, if you have PHP on both sites, people don't simply upload to the target server in the first place?

              Mark

                Thanks for the reply, To answer ur question:
                I cannot upload to the webserver because that is allowed for websites only(not allowed for uploads/downloads), beside it will take all the web bandwidth for file uploading/downloading..

                The 2nd server is mine, based on unix system. I use for upload/downloads only. However, I thought it could be done in php easily, using some functions.. but it seems like I was wrong.
                Well, thanks.. and I'm going to leave this topic open to hear what others have to say for this, if anyone find a solution for this problem, it would be great to know the answer.

                  Here's a way.

                  <?php
                  // run this on your local machine
                  // set the url to the remote machines address
                  // sample.txt is a file on your local machine
                  $file = 'sample.txt';
                  include "http://remotemachine/getremotefile.php?file=$file";
                  ?>
                  <?php
                  // run this on the remote machine
                  // set url to your local machines address
                  $getfile = $_GET['file'];
                  print $getfile;
                  $data = file_get_contents("http://localmachine/$getfile");
                  $fp = fopen($getfile, 'wb');
                  fwrite($fp, $data);
                  fclose($fp);
                  ?>

                    There are several obvious flaws with samudasu's solution above - not only is it obfuscated in the extreme, but it probably won't work and also presents several major security problems.

                    Because he hasn't made any attempt to explain how it works, you should not use it - at least not without a thorough explanation of how it should work.

                    In my opinion, most uses of including a remote file with include() are errors.

                    Mark

                      Mark: For someone who can't understand 8 lines of PHP code you sure are opinionated. Obfuscated, i hardly think so. Each line is self explanatory. Before you go running your mouth off you should try to understand what i posted.

                      This code isn't meant to be secure. It's just shows a simple method of calling a script to download a file. If someone were to use this on a live server then i'd expect them to have enough sense to make it more secure.

                      Actually there is one line that i should have changed when i first posted that might throw some people off. file_get_contents("http://localmachine:8080/$getfile")
                      should have been file_get_contents("http://localmachine/$getfile");
                      Port 8080 is another webserver running on my test machine and i forgot to change that when i posted.

                        Ok, let me clarify this:

                        1. You are using include() to include via a URL. This smells bad to me - it smells either of not understanding what include() does on remote files, or self-writing code (which I don't like).

                        If that's the whole of getremotefile.php, then it's even more weird, as it doesn't appear to be echo'ing anything - therefore you're including an empty file - so include() as an RPC mechanism? A bit strange, I think, in the extreme.

                        1. You have a script called getremotefile.php which seems to be doing more or less the opposite - it's actually used to PUT a remote file. But not quite,

                        2. It's an obfuscated callback mechanism - there are three HTTP requests happening concurrently, when surely a maximum of two are required?

                        3. You're relying on the file being in a directory served by the web server are you? This is totally unclear.


                        I think it is obfuscated. You're abusing include() to do something totally different. You're making three requests instead of two, and this is probably totally unclear to most people (indeed, it was to me before I looked at it more carefully).

                        I'm going to offer an alternative implementation here:

                        <?php
                        function TestSend()
                        {
                        	$filename = "test.txt";
                        	$url = "http://localhost:9001/put.php?filename=" . urlencode($filename);
                        	$content = file_get_contents($filename);
                        	$opts = array ('http' => array (
                        		'method' => 'POST', 
                        		'content' => $content,
                        		'header' => "Content-type: application/octet-stream"));
                        	$ctx = stream_context_create($opts);
                        	$f = fopen($url, "r", false /* use_include_path*/, $ctx);
                        	$result = stream_get_contents($f);
                        	fclose($f);
                        	echo "Post result: $result\n";
                        }
                        TestSend();
                        
                        ?>
                        
                        <?php
                        /*
                         * Recieve a remote file via a raw HTTP POST.
                         *
                         * Security NOTE: this should do some authorisation and
                         * perform better checks on the filename.
                         */
                        
                        function DoPut()
                        {
                        	$filename = $_GET['filename'];
                        	// Some lame checks - NOT SUITABLE FOR PRODUCTION USE!
                        	assert(strchr($filename, '/') === FALSE);
                        	assert(strchr($filename, "\\") === FALSE);
                        	assert(strchr($filename, '.php') === FALSE);
                        	global $HTTP_RAW_POST_DATA;
                        	assert(isset($HTTP_RAW_POST_DATA));
                        	$f = fopen("uploads/" . $filename, "wb");
                        	fwrite($f, $HTTP_RAW_POST_DATA);
                        	fclose($f);
                        	echo "ok";
                        }
                        DoPut();
                        
                        ?>
                        

                        I've tested this in a recent PHP 5.1.2+ snapshot version and it appears to work. Your mileage may vary, but the above is tested and requires only two HTTP requests, not three.

                        I'm using raw post data to send the file.

                        Mark

                          Congradulations Mark. It's about time you came up with a solution. Ok, let me clarify some things to you.

                          1. There are no rules that say you can't use a remote url with include(). Even the php manual has an example of this. Actually the file with the include does echo something back from the result of getremotefile.php. In this case just the file name but it could echo anything the user wanted (error messages, etc.) You need to remember that the page with the include may have other functions to perform beside what i'm showing.

                          This could have done this with a form or even a url but i decided to do a one line include instead. Really the only thing this page needs to do is send a filename to getremotefile.php.

                          1. You're not seeing clearly that http://remotemachine/getremotefile.php?file=$file is run on the "REMOTE" machine. If you read the code for getremotefile.php you will see that it it opens a connection and gets the chosen file (which is remote as far as getremotefile.php is concerned) from your local machine. It doesn't PUT anything.

                          2. It's obfuscated in your eyes since you still haven't figured out how it works.
                            I never said this was the only way to do it. I'm just showing a method that you couldn't show before. That's what the original poster asked for and oh yes, IT DOES WORK.

                          3. The user of the script knows the location of the file they want to send. It's up to he/she to choose the correct path.

                          Abusing include, i don't see it.

                          Goaman: Sorry, but if you don't have php on both servers then neither of the two methods mentioned above will be of any use. I'm sure you've realized that already.

                            I didnt read anyone elses posts but the first one, so I dont know if anyone has said this, but.......
                            just put another uploader.php on the remote site and just use the form action="remoteSite/uploader.php"

                              samudasu wrote:

                              1. There are no rules that say you can't use a remote url with include().

                              No, but you are abusing it. You could have achieved the same with file_get_contents(). Please do not use include() when file_get_contents() will do.

                              2. You're not seeing clearly that http://remotemachine/getremotefile.php?file=$file is run on the "REMOTE" machine.

                              No, I can see where it runs.

                              But it seems like an excessively complicated way of doing it:

                              • User connects to machine "local"
                              • Local machine connects to machine "remote" can calls "getremotefile.php"
                              • Remote machine then connects BACK to local machine???!

                              If you read the code for getremotefile.php you will see that it it opens a connection and gets the chosen file (which is remote as far as getremotefile.php is concerned) from your local machine. It doesn't PUT anything.

                              Well, from my perspective it puts the file into its own filesystem on "remotemachine". The difficulty I have is understanding why you need to do this.

                              I'm concerned that the OP may have one or more directions of traffic between localmachine and remotemachine firewalled. Your solution requires 2 additional HTTP requests. Mine only requires one.

                              3. It's obfuscated in your eyes since you still haven't figured out how it works.

                              Yes I have. I figured it out a long time ago, and it's nonsense. Do not use include() to do RPC!!! If you must use this method, at least use file_get_contents or something.

                              Mark

                                1. Sorry but you are wrong. Include did exactly what i wanted it to do. Evaluate a file and return the result. What's your hangup with include? Did include() steal your girl or something? It seems that's all you can talk about.

                                2. My script may seem excessive to you but i'll bet you'd be surprised to know my sript is nearly 2x as fast as your bloated crapware, not to mention the fact that your script won't run on php 4.* which most of the real world is still using. If you don't believe me then test it. And test it on a real machine not Windows which is probably what you try to develop on. By the way, i actually did run your code and it's SLOOOOWWWWWWWWW.

                                3. It's not complicated at all. It's rather simple. Your code is cute and packed up into nice little functions but it's bloated and slow.

                                4. Your mention of worrying about being firewalled is utter nonsense. If port 80 is blocked (which is what both our methods are using) from incoming or outgoing traffic it's not going to matter. I think you need to do some further reading on the http protocol and tcp/ip while you're at it.

                                5. Basically you need to get a life and stop critiquing my code. No one asked your opinion in the first place. Do you need to prove that you're a good coder or something? It's not working!

                                  woo.. Thanks for the reply guys, where to begin.. oh yes..

                                  I wouldn't be talking abt uploading to remote machine using php if I don't have php running on both servers, I have php on both srvs. Both server has php 4.x.x.x.

                                  It looks like when you upload a file that saves on a localmachine(A) first and then it must be manually grabbed from the localmachine(A) to save again in a remotemachine(😎, am I right guys? Isn't there any ways to send the file streight to my remote machine folder without saving the file on a webserver?

                                    Hi Goaman, sorry about all the drama but it really pisses me off when someone says my code doesn't work without even understanding it (yes, he understands now but when he first attacked my post he didn't) and then tries to play my mother saying i shouldn't do this or that.
                                    Just for your info, my method was just meant to be a demonstration of one way it could be done. If you're not looking to save a local copy then (as much as i hate to say it because he's such a blow hard) MarkR's method will probably work better in this particular case. My code could also be easily adapted to do the same using a tempfile but i wouldn't recommend it. So to answer your question, no, you don't have to save the file on the local server. You should be able to send it directly from the tmp upload directory.

                                      Write a Reply...