php - File upload using curl is not working

343

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'
192

Answer

Solution:

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),
));

People are also looking for solutions to the problem: while submitting large files using jquery ajax all input fields get empty on server site php

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.