So after researching a bit, I found out that it is possible to send a file through cURL to another page. Here is the code that is doing the sending part:
$url = 'http://someabc.com/api.php';
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
$postData = array(
'array' => "@".realpath('array.txt'),
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
$response = curl_exec($ch);
echo $response;
Receiving Part (api.php)
if(isset($_POST['array']))
{
$string = $_POST['array'];
echo $string;
}
else
{
echo 'Not found';
}
When I run the page containing the cURL request, I'm getting Not found
printed on the page.
Does this mean I'm capturing the data in a wrong way? If so, what would be the way to get the contents of array.txt
from api.php
page?
UPDATE
After using $_FILES
as suggested by Jon, I received the following array:
Array
(
[name] => array.txt
[type] => application/octet-stream
[tmp_name] => /tmp/phpPQZXf9
[error] => 0
[size] => 77413
)
Now I tried getting the contents of this file using:
$tmp = $_FILES['array'];
$string = file_get_contents($tmp['name']['tmp_name']);
but got the error Warning: file_get_contents(a) [function.file-get-contents]: failed to open stream: No such file or directory
which would mean I'm not referencing the file correctly. Where did I go wrong now?