简体   繁体   English

php curl发布$ _FILES问题

[英]php curl post $_FILES issue

I'm trying to send a .txt file via php curl post request to get its contents but no success. 我正在尝试通过php curl post请求发送.txt文件以获取其内容,但没有成功。

The point is that if I make a var_dump($_FILES) as $result being $result the curl_exec($ch) response it shows me an empty array but if I try it with $_POST : 关键是如果我将var_dump($_FILES)作为$result作为$resultcurl_exec($ch)响应,它将显示一个空数组,但是如果我尝试使用$_POST

array(
    [uploaded_file] => 
        [name] => 'absolute/path/to/file.txt'
        [mime] => 'text/plain'
        [postname] => 'file.txt'
)

How can I pass that file as a $_FILES variable? 如何将该文件作为$ _FILES变量传递?

This is my curl send.php curl script: 这是我的curl send.php curl脚本:

$filedata = curl_file_create('../path/to/file.txt', 'text/plain', 'file.txt');

$array = array(
    'file' => $filedata
)

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://domain_name.com/url/to/script.php');
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $array);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
$result = curl_exec($ch);
curl_close($ch);    

This is the receive.php script: 这是receive.php脚本:

if(file_exists($_FILES['uploaded_file']['tmp_name']) && is_uploaded_file($_FILES['uploaded_file']['tmp_name'])){
    $contents = file_get_contents($_FILES['uploaded_file']['tmp_name']);
    $data = explode('/',$contents);
}

but it's never entering the if statement... 但它永远不会输入if语句...

You can't pass the file via cURL to make it appear in the $_FILES variable. 您无法通过cURL传递文件,以使其显示在$_FILES变量中。

That is because the $_FILES variable is used for uploaded files using an HTML form submission. 这是因为$_FILES变量用于使用HTML表单提交的上传文件。

For security reasons, the file must be encrypted first, and decrypted in the receiver page. 出于安全原因,必须先对文件进行加密,然后在接收方页面中将其解密。 I suggest you to use an encryption method that let you to choose the encryption's key, such as sha1 . 我建议您使用一种加密方法,让您选择加密的密钥,例如sha1

You can find more informations here: https://stackoverflow.com/a/15200804/2342558 您可以在这里找到更多信息: https : //stackoverflow.com/a/15200804/2342558

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM