【问题标题】:I want upload file DICOM using PHP我想使用 PHP 上传文件 DICOM
【发布时间】:2020-04-19 06:03:05
【问题描述】:

请查看下面我用来将文件上传到 orthanc 的代码。我不确定这个错误是来自 orthanc 配置还是因为 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);

这是我得到的错误。

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

【问题讨论】:

  • 似乎“DICOM 文件”已损坏或扩展名错误
  • 不,我已经下载了新的 DICOM 文件。

标签: php curl file-upload dicom


【解决方案1】:

你没有上传任何东西。自 PHP 5.5 起不鼓励使用 @ 上传文件的方案,自 PHP 5.6 起默认禁用(使用 CURLOPT_SAFE_UPLOAD 选项),自 PHP 7.0 起完全删除

替换

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

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

(我很确定 realpath 不是必需的,它只是减慢了代码速度)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-07
    • 2021-10-26
    • 2020-01-24
    • 2021-08-04
    • 2022-11-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多