0

I have been trying to upload file to using curl using laravel app end point.

Below code is on separate server.

$header = $this->getCurlHeader();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$this->url);
curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
// curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);   
$cfile = new \CURLFile($this->filePath . $this->csvFile,  'text/csv','text.csv');
//print_r($cfile);exit;
$postData = array('csv' => $cfile);         
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);                    
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
//curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$response = curl_exec($ch);
$hhtpCode = curl_getinfo($ch);
curl_close($ch);
//$this->dbg($hhtpCode);
$this->dbg(json_decode($response,1),1);

And receiving end in laravel app hosted on different server.

public function insert(Request $request, $feed=''){
     //return response()->json(var_dump($request->all()));
     //return response()->json("here in insert");
     return response()->json($_FILES);
     echo "here";
     dd($_FILES);
}

It returns me empty response. I am able to verify curl request using headers.

Any suggestion will be helpful. Thanks.

3 Answers 3

1

You can try something like this on the source server instead:

$target_url = 'https://mywebsite.com'; // Write your URL here
$dir = '/var/www/html/storage/test.zip'; // full directory of the file

$cFile = curl_file_create($dir);
$post = array('file'=> $cFile); // Parameter to be sent

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $target_url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result=json_decode(curl_exec($ch));
curl_close ($ch);

From the destination server, print the request sent.

public function insert(Request $request){
    dd($request->file);
}
Sign up to request clarification or add additional context in comments.

5 Comments

What about receiving script
Not able to receive any thing in receiving script
@ShakirBhatt I've updated the sample for destination server
Thanks. Tried dd($request->file); but it prints nothing. Can i get in touch if you don't mind.
I have the answer but i couldn't post it as I have been banned for posting.
0

please use curlfile method to upload files.

if(isset($_FILES) && !empty($_FILES['identity_doc']))
            {

                    foreach($_FILES['identity_doc'] as $key => $file)
                    {
                        $data['document'.$i]= new CURLFile($_FILES['identity_doc']['tmp_name'][$i], $_FILES['identity_doc']['type'][$i],$_FILES['identity_doc']['name'][$i]);
                    }

            }

1 Comment

Thanks, but don't have from-data. My file is at some path.
0
if(isset($_POST['submit']))
{
    $fileName=$_FILES['resume']['name']; 
    $type=$_FILES['resume']['type'];
    $tempPathname=$_FILES['resume']['tmp_name'];
    $handle = fopen($tempPathname, "r");
    $POST_DATA = fread($handle, filesize($tempPathname));

    $url="https://objectstorage.region-name.oraclecloud.com/p/par-id/n/namespace`enter code here`/b/bucket-name/o/images/".$fileName;
    $curl = curl_init();
    curl_setopt_array($curl, array(
         CURLOPT_URL => $url,
         CURLOPT_RETURNTRANSFER => true,
         CURLOPT_ENCODING => "",
         CURLOPT_MAXREDIRS => 10,
         CURLOPT_TIMEOUT => 30,
         CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
         CURLOPT_CUSTOMREQUEST => "PUT",
         CURLOPT_POSTFIELDS=>$POST_DATA ,
         CURLOPT_HTTPHEADER => array(
           'Content-Type: '.$type,
         ),
     ));
     $response = curl_exec($curl);
     $err = curl_error($curl);
    
     curl_close($curl);
     
     if ($response) {
        echo 'error';
     } else { 
         echo $url;
     }
}
?>
<form method="post" enctype="multipart/form-data">
    <input type="file" name="resume">
    <button type="submit" name="submit">submit</button>
</form>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.