简体   繁体   中英

send file to server curl php

I have small php app, that have some files inside. I want to send one of the files from one server to another server with using curl.

I already have file on server, I just need to send it to another server.

I execute the following code:

$url = "http://localhost:3919/";
$myCurl = curl_init();
curl_setopt($myCurl, CURLOPT_URL, $url);
curl_setopt_array($myCurl, array(
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_POST => true,
                CURLOPT_HEADER => false,
                CURLOPT_SAFE_UPLOAD => true,
                // CURLOPT_HTTPHEADER => array(
                //  "Content-Type: multipart/form-data"),
                CURLOPT_POSTFIELDS => array(
                    "inputFile" => new CurlFile('@' . './Files/example.docx'))
            ));

$response = curl_exec($myCurl);
curl_close($myCurl);
return $response;

But it doesn't trigger the server. So what should I do wrong?

For example in other case (other app) I use this:

$file = $_FILES["inputFile"];
curl_setopt_array($myCurl, array(
    CURLOPT_URL => $url,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST => true,
    CURLOPT_HEADER => false,
    CURLOPT_SAFE_UPLOAD => true,
    CURLOPT_HTTPHEADER => array(
        "Content-Type: multipart/form-data"),
    CURLOPT_POSTFIELDS => array(
        "inputFile" => new CurlFile($file["tmp_name"], $file["type"], $file["name"]))
));

And it works perfectly.

maybe you need validate if exist files on your post and this a example complete:

<form enctype="multipart/form-data" encoding='multipart/form-data' method='post' action="form.php">
  <input name="uploadedfile" type="file" value="choose">
  <input type="submit" value="Upload">
</form>
<?
if ( isset($_FILES['uploadedfile']) ) {
 $filename  = $_FILES['uploadedfile']['tmp_name'];
 $handle    = fopen($filename, "r");
 $data      = fread($handle, filesize($filename));
 $POST_DATA = array(
   'file' => base64_encode($data)
 );
 $curl = curl_init();
 curl_setopt($curl, CURLOPT_URL, '<span style="color: red;">http://extserver.com/handle.php</span>');
 curl_setopt($curl, CURLOPT_TIMEOUT, 30);
 curl_setopt($curl, CURLOPT_POST, 1);
 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
 curl_setopt($curl, CURLOPT_POSTFIELDS, $POST_DATA);
 $response = curl_exec($curl);
 curl_close ($curl);
 echo "<h2>File Uploaded</h2>";
}
?>

this is a example with encrypt the file with base64.

your external server

$encoded_file = $_POST['file'];
$decoded_file = base64_decode($encoded_file);
/* Now you can copy the uploaded file to your server. */
file_put_contents('<span style="color: red;">subins</span>', $decoded_file);

Using the curl_file_create works for me on sending files in curl.

$target_url = 'http://localhost:3919/';

$post = array (
    'file' => curl_file_create('Location of the file')
);

$ch = curl_init ($target_url);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$msg=curl_exec ($ch);
$info = curl_getinfo($ch);

if($info['http_code'] === 200){
    $returnMessage = json_decode($msg,1);
} else {
    $returnMessage['file_type'] = 'error';
}
curl_close($ch);

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