简体   繁体   中英

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.

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 :

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?

This is my curl send.php curl script:

$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:

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...

You can't pass the file via cURL to make it appear in the $_FILES variable.

That is because the $_FILES variable is used for uploaded files using an HTML form submission.

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 .

You can find more informations here: https://stackoverflow.com/a/15200804/2342558

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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