Sunil Jose
Sunil Jose

Reputation: 325

File upload using curl is not working

I was trying to upload file using curl in symfony but uploading is not working, code is given below.

public function filePassingAction(Request $request, $guid) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "http://192.168.0.41/lis/web/app_dev.php/interface/file-upload");
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, array(
            'file' => '@'.$this->getParameter('directory1').'/file.pdf',
        ));
        $result = curl_exec($ch);
        curl_close($ch);
        ($result);
        return new JsonResponse($result);
    }

receiving action

public function fileUploadAction(Request $request) {
        $file = $request->files->get('file');
        // Generate a unique name for the file before saving it
        $fileName = md5(uniqid()) . '.' . $file->guessExtension();

        // Move the file to the directory where brochures are stored
        $file->move(
                $this->getParameter('directory2'), $fileName
        );

        return new JsonResponse();
    }

in service.yml

directory1: '%kernel.root_dir%/../web/uploads/directory1'
directory2: '%kernel.root_dir%/../web/uploads/directory2'

Upvotes: 2

Views: 2661

Answers (1)

Suf_Malek
Suf_Malek

Reputation: 666

Use

realpath

$file_name_with_full_path = $this->getParameter('directory1').'/file.pdf';

curl_setopt($ch, CURLOPT_POSTFIELDS, array(
   'file' => '@'.realpath($file_name_with_full_path),
));

For php 5.5+

$file_name_with_full_path = $this->getParameter('directory1').'/file.pdf';

curl_setopt($ch, CURLOPT_POSTFIELDS, array(
   'file' => curl_file_create($file_name_with_full_path),
));

Upvotes: 4

Related Questions