A file is sent to the controller using the POST method. It needs to be sent to a third-party API using CURL.

Now I have implemented it like this:

public function actionAddAttachment() { $attachmentFile = UploadedFile::getInstanceByName('attachment'); if ($attachmentFile === null) { return ['success' => false, 'error' => 'File was not uploaded']; } $attachment = new Attachment(['fileResource' => $attachmentFile]); if ($attachment->upload()) { $result = Api::addAttachment($attachment); $attachment->unlink(); return ['success' => $result === true]; } return ['success' => false]; } 

Upload method:

 public function upload() { $this->fileName = $this->fileResource->baseName . '.' . $this->fileResource->extension; $this->path = 'attachments/' . $userToken . microtime() . '_' . $this->fileName; return $this->fileResource->saveAs($this->path); } 

The request is sent as follows:

 $cfile = new \CURLFile(realpath($attachment->path)); $cfile->setPostFilename($attachment->fileName); $data = ['file' => $cfile]; $curl_options = array( CURLOPT_URL => $url, CURLOPT_USERPWD => $pwd CURLOPT_VERBOSE => 1, CURLOPT_CUSTOMREQUEST => POST, CURLOPT_RETURNTRANSFER => true, CURLOPT_HTTPHEADER => $headers, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => false, CURLINFO_HEADER_OUT => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 90, CURLOPT_POSTFIELDS => $data, ); 

It turns out every time before sending the request, I save the file and after trying to send it, I delete it.
I tried to create a CURLfile based on the data of the $_FILES (tmpName, name, type) , but in this case the request cannot be made, CURL returns error number 26 - the file could not be loaded.

How can I implement sending a file directly from the $ _FILES array without saving it in Attachments?

    0