Reputation: 1312
I have a situation where a webserver posts a JSON array which includes some parameters as well as a document data within it. I get the data, content-type and filename. I convert this JSON into an associative array so that I can work with it. An example of the converted associative array is given below.
[dealer] => Array
(
[fullName] => John Doe
[email] => [email protected]
[resume] => Array
(
[file] => Array
(
[data] => UEsDBBQABgAIAAAAIQCVRr6H0AEAAB8IAAATAAgCW0NvbnRlbnRfVHlwZXNdLnhtbCCiBAIooAACAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAC0VU1v4bWxQSwECLQAUAAYACAAAACEAh7oKST4BAACpAgAAFAAAAAAAAAAAAAAAAADhdQAAd29yZC93ZWJTZXR0aW5ncy54bWxQSwECLQAUAAYACAAAACEAhub8n6scAADr+gAAGgAAAAAAAAAAAAAAAABRdwAAd29yZC9zdHlsZXNXaXRoRWZmZWN0cy54bWxQSwUGAAAAABQAFAAiBQAANJQAAAAA
[fileName] => John_resume.docx
[contentType] => application/octet-stream
)
)
I am trying to write this to the file system and am able to do so but the file is written as a text file and not as a binary file. The code I have in place to do this is also provided below.
$json = file_get_contents('php://input');
$document = json_decode($json, true, 32, JSON_BIGINT_AS_STRING);
$resumeFileData = $document['resume']['file']['data'];
$filePath = $folderPath . DS . $document['resume']['file']['fileName'];
$resumeFile = new File($filePath, true);
$resumeFile->write($resumeFileData);
$resumeFile->close();
What am I doing incorrectly? I am using CakePHP 2.5.4
Thanks in advance
Upvotes: 0
Views: 678
Reputation: 2877
try this.
$resumeFileData = base64_decode($document['resume']['file']['data']);
Upvotes: 1