Search code examples
phpcurlfile-uploaddicom

I want upload file DICOM using PHP


Please look into below code which i am using to upload file to orthanc. i am not sure that this error is from orthanc configuration or because of CURL

        $total = count($_FILES['file']['name']);
        $tmp_name = array();
        for ($i = 0; $i < $total; $i++) {
            $tmpFilePath = $_FILES['file']['tmp_name'][$i];
            array_push($tmp_name, $tmpFilePath);
        }

        $postfields = array();
        foreach ($tmp_name as $index => $file) {
            $file = '@' . realpath($file);
            $postfields["file_$index"] = $file;
        }

        $url = 'http://localhost/instances';
        $postfields = $postfields;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false); //require php 5.6^
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $postResult = curl_exec($ch);
        if (curl_errno($ch)) {
            //print curl_error($ch);
        }
        curl_close($ch);
        $json = json_decode($postResult, true);

        print_r($json);

Here is the error that i am getting.

Array ( [Details] => Cannot parse an invalid DICOM file (size: 28 bytes) [HttpError] => Bad Request [HttpStatus] => 400 [Message] => Bad file format [Method] => POST [OrthancError] => Bad file format [OrthancStatus] => 15 [Uri] => /instances )


Solution

  • you're not uploading anything. the @ scheme to upload files has been discouraged since PHP 5.5, disabled-by-default (with the CURLOPT_SAFE_UPLOAD option) since PHP 5.6, and completely removed since PHP 7.0

    replace

        foreach ($tmp_name as $index => $file) {
            $file = '@' . realpath($file);
            $postfields["file_$index"] = $file;
        }
    

    with

        foreach ($tmp_name as $index => $file) {
            $file = realpath($file);
            $postfields["file_{$index}"] = new CURLFile($file);
        }
    

    (and i'm pretty sure the realpath isn't required, it's just slowing down the code)