duan7772
2014-01-22 14:59
浏览 126
已采纳

获取通过cURL POST发送的文件的内容

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?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douliao5942 2014-01-22 15:42
    已采纳

    For the first part before the update, the problem exists because when posting a file to a page, regardless of the method in which it happens (ie <input type='file />, curl, etc) it is always available to PHP via the $_FILES variable, which is an associative array, in your case, should be $_FILES['array'] containing the information of the temporary file location. The array should be similar to:

    Array 
    (
        [array] => Array
        (
                [name] => array.txt
                [type] => encoding type
                [tmp_name] => /tmp/path/file
                [error] => 0 (if no error)
                [size] => [filesize]
        )
    )
    

    From there, to access the file, you'd want to move it from the tmp directory to one you have permission to access. This is accomplished with move_uploaded_file. An example of usage with this would be:

    $upDir = 'uploads/';
    move_uploaded_file($_FILES['array']['tmp_name'], $upDir. $_FILES['array']['name']);
    

    From there, the file will be on the server under the relative path to the php file in uploads/array.txt, and you can do what you will with it there. ^^

    点赞 打赏 评论
  • dtfbj24048 2014-01-22 15:25

    If your trying to post a file the data will be available on $_FILES. you can check using the following code

    var_dump($_FILES);
    

    if you get your file information from $_FILES then you can replace your check "if(isset($_POST['array']))" with

    if(count($_FILES))
    {
        ...
    }
    else
    {
        echo 'Not found';
    }
    

    to access files content try:

    $tmp = $_FILES['array'];
    $string = file_get_contents($tmp['tmp_name']);
    
    点赞 打赏 评论

相关推荐 更多相似问题