Home > Net >  How to upload the file directly to Cloudinary without first saving it in the public folder
How to upload the file directly to Cloudinary without first saving it in the public folder

Time:12-14

As briefly written in the title of this question, I want to save the xml file that I generate directly from the controller directly in Cloudinary without saving it locally (in the public folder).

As I set up the code (since I use the saveXML() method) I have to save it locally first so that when it is saved on Cloudinary it takes the name of the file saved thanks to the saveXML method and then saves it on Cloudinary.

So I want the file to be saved directly to Cloudinary without being saved in the public folder.

Is it possible to do this?

This is my code (the controller):

 public function fatturaxml($id)
    {      

        $xml = new SimpleXMLElement('<?xml version="1.0" encoding="utf-8"?><mydoc></mydoc>');

        $xml->addAttribute('version', '1.0');
        $xml->addChild('datetime', date('Y-m-d H:i:s'));

        $person = $xml->addChild('person');
        $person->addChild('firstname', 'Mkenny');
        $person->addChild('secondname', 'Mary');
        $person->addChild('telephone', '000000000');
        $person->addChild('email', '[email protected]');

        $xml->saveXML('myxml.xml');

        $response = Response::make($xml->asXML(), 200);
        $response->header('Cache-Control', 'public');
        $response->header('Content-Description', 'File Transfer');
        $response->header('Content-Disposition', 'attachment; filename=test.xml');
        $response->header('Content-Transfer-Encoding', 'binary');
        $response->header('Content-Type', 'text/xml');

        cloudinary()->uploadFile('myxml.xml', ['folder' => 'FolderTest',])->getSecurePath();
    }

I tried to write too

cloudinary()->uploadFile($xml, ['folder' => 'FolderTest',])->getSecurePath();

but I get the following error: Missing required parameter - file

CodePudding user response:

a quick search reveals this in the documentation: Parameter: File
Type: String
Description part 2:
the actual data (byte array buffer). For example, in some SDKs, this could be an IO input stream of the data (e.g., File.open(file, "rb")).

that should mean that you can do something like cloudinary()->uploadFile($xml, ['folder' => 'FolderTest',])->getSecurePath();

Edit

Reading the SimpleXMLElement docs the asXML() method whith should return the xml contents.

If the filename isn't specified, this function returns a string on success and false on error.

I would try rewriting it as cloudinary()->uploadFile($xml->asXML(), ['folder' => 'FolderTest',])->getSecurePath();

Since this is a string, it should work (I haven't tried this)

  • Related