Using insertAtPath to create a new document

I am trying to us the insertAtPath() method in src/Services/DocumentService.php to create/upload a new pdf file. Here is my code:

$fileData = array("name" => "temp_name", "tmp_name" => "pdf");
$path = "test.pdf";
$documentService->insertAtPath($new_patient['pid'], $path, $fileData);

…and here is the error I’m getting:

PHP Warning:  mime_content_type(pdf): Failed to open stream: No such file or directory in /Users/jmlockl2/Desktop/openemr/library/sanitize.inc.php on line 112

Warning: mime_content_type(pdf): Failed to open stream: No such file or directory in /Users/jmlockl2/Desktop/openemr/library/sanitize.inc.php on line 112
OpenEMR API Error: Attempt to upload unsecure patient document was declined

I am not quite sure what I should be including in the $fileData array. Any help appreciated!

hi @lumbee, you need to check on a couple settings

// Ensure filetype is allowed
        if ($GLOBALS['secure_upload'] && !isWhiteFile($fileData["tmp_name"])) {
            error_log("OpenEMR API Error: Attempt to upload unsecure patient document was declined");
            return false;
        }

in Admin->Globals->Security

in Admin->System->Files

Thanks for the reply @stephenwaite. Here are the settings on my server so pdf’s should be accepted…yes?

hi @lumbee, can try to upload one in the app to verify

@stephenwaite yes, same error:

PHP Warning:  mime_content_type(pdf): Failed to open stream: No such file or directory in /Users/jmlockl2/Desktop/openemr/library/sanitize.inc.php on line 112

Warning: mime_content_type(pdf): Failed to open stream: No such file or directory in /Users/jmlockl2/Desktop/openemr/library/sanitize.inc.php on line 112
OpenEMR API Error: Attempt to upload unsecure patient document was declined

meant here:

you prob can and your path needs some work in your script

I see, my bad :upside_down_face:

Good call, getting this error when I try to upload a document via the app/UI:

Fatal error : Uncaught OpenEMR\Common\Database\SqlQueryException: Failed to execute statement. Error: Unknown column ‘uuid’ in ‘field list’ Statement: SELECT uuid FROM documents WHERE uuid = ? in /Users/jmlockl2/Sites/openemr/src/Common/Database/QueryUtils.php:28 Stack trace: #0 /Users/jmlockl2/Sites/openemr/src/Common/Uuid/UuidRegistry.php(313): OpenEMR\Common\Database\QueryUtils::fetchRecordsNoLog(‘SELECT uuid F…’, Array) #1 /Users/jmlockl2/Sites/openemr/src/Common/Uuid/UuidRegistry.php(105): OpenEMR\Common\Uuid\UuidRegistry->getUnusedUuidBatch(1) #2 /Users/jmlockl2/Sites/openemr/library/classes/Document.class.php(1016): OpenEMR\Common\Uuid\UuidRegistry->createUuid() #3 /Users/jmlockl2/Sites/openemr/controllers/C_Document.class.php(313): Document->createDocument(‘24021’, ‘3’, ‘test.pdf’, ‘application/pdf’, ‘%PDF-1.3\n%\xC4\xE5\xF2\xE5\xEB…’, ‘’, 1, false, ‘/private/var/tm…’) #4 /Users/jmlockl2/Sites/openemr/library/classes/Controller.class.php(126): C_Document->upload_action_process(‘24021’, ‘3’) #5 /Users/ in /Users/jmlockl2/Sites/openemr/src/Common/Database/QueryUtils.php on line 28`

ok @lumbee, what OpenEMR version?

v6.1.0-dev, and my php version is 7.3.24

it works on the dev demos, have you updated off of the master branch lately?

I’m actually on a custom branch so it must be something going on locally with that branch. Thanks for helping me sort through this. I’ll dig deeper into my local branch to see if I can figure things out.

OK, I resolved my issues with the code base and can now upload documents using the UI/application. Here is what I am seeing at the moment:

My code:

$fileData = array("name" => "temp_name", "tmp_name" => "application/pdf");
$file = "/Users/jmlockl2/Sites/test.pdf";
$documentService->insertAtPath($new_patient['pid'], $file, $fileData);
var_dump($documentService);

The result:

(base) jmlockl2@C02ZM744MD6M custom % php sand_box.php
PHP Warning: Undefined array key “provider_id” in /Users/jmlockl2/Sites/crisisprep-6/src/Services/EncounterService.php on line 328

Warning: Undefined array key "provider_id" in /Users/jmlockl2/Sites/crisisprep-6/src/Services/EncounterService.php on line 328
PHP Warning:  Undefined array key "authUser" in /Users/jmlockl2/Sites/crisisprep-6/library/forms.inc on line 59

Warning: Undefined array key "authUser" in /Users/jmlockl2/Sites/crisisprep-6/library/forms.inc on line 59
PHP Warning:  Undefined array key "authProvider" in /Users/jmlockl2/Sites/crisisprep-6/library/forms.inc on line 63

Warning: Undefined array key "authProvider" in /Users/jmlockl2/Sites/crisisprep-6/library/forms.inc on line 63
PHP Warning:  mime_content_type(application/pdf): Failed to open stream: No such file or directory in /Users/jmlockl2/Sites/crisisprep-6/library/sanitize.inc.php on line 112

Warning: mime_content_type(application/pdf): Failed to open stream: No such file or directory in /Users/jmlockl2/Sites/crisisprep-6/library/sanitize.inc.php on line 112
OpenEMR API Error: Attempt to upload unsecure patient document was declined
object(OpenEMR\Services\DocumentService)#319 (0) {
}

Wondering if this could be file perms/rights issue to the documents folder in sites/defaults/documents? Here are the perms there: drwxr-xr-x 17 _www _www 544 Aug 25 13:07 documents

hi @lumbee, wondering if the webserver can’t read from the directory you’re providing as the source of the pdf.

@stephenwaite I am thinking that is the issue also. Any thoughts on that? Is the permissions for the documents folder enough, or is there a specific config option I need to add to apache?

…actually thinking about it more, it would not be an apache issue since this is failing while running my script. Adding a document via the app (in the web browser) works fine. So maybe its a PHP config issue :thinking:

apache only has access to it’s webroot and directories below and a temp folder unless you config it for other areas