Search code examples
phpbase64netsuite

Netsuite File upload in PHP


The goal here is to upload a file in the Netsuite File Cabinet using PHP Netsuite client

What I have so far:

$file = new File();

$file->folder = new RecordRef();
$file->folder->internalId = XXX;

$file->name = 'filename.png';
$file->fileType = MediaType::_PNGIMAGE;
$file->attachFrom = FileAttachFrom::_computer;

$content = base64_encode(file_get_contents($request->file('file')));
// Outputs a base64 string that can be converted back to an image using any base64=>image tool
$file->content = $content;

$request = new AddRequest();
$request->record = $file;

$addResponse = NetSuite::add($request); // SUCCESS

The request almost works as the file is created, in the right folder, BUT the content is unreadable

Content unreadable :(

When you download the file from Netsuite File Cabinet and try to open it you got an error saying the file is corrupted. When opening it in a text editor the content is the exact base64 string that was uploaded, and can be converted back to an image in any base64=>image tool

Any suggestion ?


Solution

  • It turns out that the library is actually handling the base64 encoding itself

    - $content = base64_encode(file_get_contents($request->file('file')));
    + $content = file_get_contents($request->file('file'));
    

    Thanks @Lawrence !