I am having two websites one is A
and another one is B
I have an image uploading form in A
which allows user to upload files.A also has a database recording path of image file is actually has to go to the B
How to do this?. i am owning both the websites. Any idea?.
-
1You can Use CURL for this .– ShubCommented Nov 6, 2014 at 6:32
-
Do you have documentation regarding this?.– arunwebberCommented Nov 6, 2014 at 6:33
Add a comment
|
3 Answers
You can simply do this by using PHP FTP see below code-
$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);
-
consider i uploaded a file from a form but in the place of source what i have to receive from the form?. Commented Nov 7, 2014 at 14:05
-
source will be $_FILES["your_file_name_given_in_form"]["tmp_name"]; Commented Nov 8, 2014 at 5:21
Try this code:
if (version_compare(phpversion(), '5.5.0', '>='))
{
$postData['file'] = new CURLFile($fileData['tmp_name'], $fileData['type'] ,$fileData['name']);
}
else
{
$postData['file'] = '@'.$fileData['tmp_name'];
$postData['file_info']['name'] = $fileData['name'];
}
$url = 'http://your-site.com/';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
$output= curl_exec($ch);
if ($output === false)
{
//handle the error
}
curl_close ($ch);
Note: If you have php 5.5 and higher it is recommended to use CURLFile.
The following code may help.
<?php
$target="http://youraddress.tld/example/upload.php";
# http://php.net/manual/en/curlfile.construct.php
// Create a CURLFile object / procedural method
$cfile = curl_file_create('resource/test.png','image/png','testpic'); // try adding
// Create a CURLFile object / oop method
#$cfile = new CURLFile('resource/test.png','image/png','testpic'); // uncomment and use if the upper procedural method is not working.
// Assign POST data
$imgdata = array('myimage' => $cfile);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $target);
curl_setopt($curl, CURLOPT_USERAGENT,'Opera/9.80 (Windows NT 6.2; Win64; x64) Presto/2.12.388 Version/12.15');
curl_setopt($curl, CURLOPT_HTTPHEADER,array('User-Agent: Opera/9.80 (Windows NT 6.2; Win64; x64) Presto/2.12.388 Version/12.15','Referer: http://someaddress.tld','Content-Type: multipart/form-data'));
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // stop verifying certificate
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true); // enable posting
curl_setopt($curl, CURLOPT_POSTFIELDS, $imgdata); // post images
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); // if any redirection after upload
$r = curl_exec($curl);
curl_close($curl);
?>
-
In this no authentication is required from another server? Commented Jan 16, 2016 at 5:39