Link to home
Start Free TrialLog in
Avatar of nfinitegpu
nfinitegpu

asked on

How to upload to remote directory

I'm trying to create a php script that will upload to remote server and remote directory

i tried something like this but it didn't worked

<form action="upload.php" method="post" ENCTYPE="multipart/form-data">
File: <input type="file" name="file" size="30">
<input type="submit" value="Upload!">
</form>


<?php

$uploaddir = "http://www.remotesite.com/uploads";
if(is_uploaded_file($_FILES['file']['tmp_name']))

{

move_uploaded_file($_FILES['file']['tmp_name'],$uploaddir.'/'.$_FILES['file']['name']);

}

print "Your file has been uploaded successfully!";

?>

I get following error

Warning: move_uploaded_file(http://www.remotesite.com/uploads/suka.txt) [function.move-uploaded-file]: failed to open stream: HTTP wrapper does not support writeable connections. in /var/www/vhosts/miproperty.com.au/httpdocs/media/upload.php on line 14

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpy9FH08' to 'http://www.remotesite.com/uploads/suka.txt' in /var/www/vhosts/miproperty.com.au/httpdocs/media/upload.php on line 14
Your file has been uploaded successfully!
Avatar of babuno5
babuno5
Flag of India image

your code should look some thing like this to work


<form action="http://www.remotesite.com/upload.php" method="post" ENCTYPE="multipart/form-data">
File: <input type="file" name="file" size="30">
<input type="submit" value="Upload!">
</form>


<?php

$uploaddir = "/uploads";
if(is_uploaded_file($_FILES['file']['tmp_name']))

{

move_uploaded_file($_FILES['file']['tmp_name'],$uploaddir.'/'.$_FILES['file']['name']);

}

print "Your file has been uploaded successfully!";

?>
Avatar of nfinitegpu
nfinitegpu

ASKER

No no you didn't get my point.The upload file is on http://www.localserver.com/upload.php and writable directory is on http://www.remotesite.com/uploads/
ASKER CERTIFIED SOLUTION
Avatar of babuno5
babuno5
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
No no you didn't get my point.The upload file is on http://www.localserver.com/upload.php and writable directory is on http://www.remotesite.com/uploads/
It is possible as long as directory is writable!
nfinitegpu:

babuno5 is correct.  If anyone was able to directly upload to another site/domain it would open up all kinds of security issues.  Babuno5's suggestion to upload it then FTP it is the way.  That's also part of the reason that all uploads go to a temp directory on your server and have to be explicitly moved to the final folder.
Avatar of Michael Worsham
From what it sounds like, you want to use a local php script on one site to upload content to another site. Under basic PHP guidelines, this won't be happening as EE Export babuno5 stated, as you cannot just point an move_uploaded_file() function to a remote site. That's not how this function works.

Reference:
http://www.php.net/move_uploaded_file
http://forums.devshed.com/php-development-5/move-uploaded-file-to-remote-server-441823.html
http://www.phpbuilder.com/board/archive/index.php/t-10318628.html
Ok,i will put question another way.Is there a function in PHP or ASP that allows to upload to remote directory
There isn't a function directly that can upload to a remote site. You are going to have to build out the PHP functionality in a way similar to the snippet I have provided below.

Reference:
http://phpsense.com/php/php-ftp-functions.html
http://us2.php.net/manual/en/ftp.examples-basic.php

$connection = ftp_connect($server);
$login = ftp_login($connection, $ftp_user_name, $ftp_user_pass);
 
   if (!$connection || !$login) { 
       die('Connection attempt failed!'); 
   }
   $upload = ftp_put($connection, $dest, $source, $mode);
   if (!$upload) { 
      echo 'FTP upload failed!'; 
   }
 
ftp_close($connection); 

Open in new window

There is no ftp service running on remotehost.So that is not a solution
Is there Apache & WebDAV configured on the remote host available?

http://php-webdav.pureftpd.org/project/php-webdav