【问题标题】:Upload file (with raw bytes) using PHP使用 PHP 上传文件(带有原始字节)
【发布时间】:2013-07-29 23:18:37
【问题描述】:

使用常规的 HTML 表单,我可以将图像上传到 URL,它会上传并返回正常。但是,当我在 PHP 中执行此操作时

      $bytes = download_attachment($timeline_item_id, $attachment);
      $image = imagecreatefromstring($bytes);
        imagejpeg($image, 'tempfile.jpg');

        $files[] = '@tempfile.jpg;filename=tempfile.jpg;type=image/jpeg';
      $post = array('authenticationToken' => 'AUTH_TOKEN','media-content'=>@"tempfile.jpg");
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,"https://api-ssl-5.tenthbit.com/send/media");
        curl_setopt($ch, CURLOPT_POST,1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
        $result=curl_exec ($ch);
        curl_close ($ch);

它不起作用....有什么想法吗?我不确定 imagecreatefromstring 是否正确。但是它创建了一个带有有效图像的 tempfile.jpg 文件。$result 是“true”,而不是来自 FORM 的 JSON 响应

参考表格:

<form action="https://api-ssl-5.tenthbit.com/send/media" method="post"
enctype="multipart/form-data">
    <input type="text" name="authenticationToken" id="file" value="AUTH">
<input type="file" name="media-content" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>

【问题讨论】:

    标签: php file file-upload curl


    【解决方案1】:

    你的方法很好。但是你没有提到你得到了什么错误。

    我假设您使用的是旧的 PHP 版本,它不再支持 '@ImageName'。 如果您使用的是 PHP 版本 5.5.9 。另外,我假设图像路径是正确的。因此,您可以从下面的代码块中获得帮助。

    $headers = array("Content-Type:multipart/form-data"); 
    $file = new CurlFile('image_path', 'mime_type', 'image_name');
    $post = array('authenticationToken' => 'AUTH_TOKEN','file_array'=>$file);
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,"https://api-ssl-5.tenthbit.com/send/media");
    curl_setopt($ch, CURLOPT_POST,1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    curl_setopt($ch, CURLOPT_INFILESIZE, $filesize);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $result=curl_exec ($ch);
    curl_close ($ch);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-14
      • 1970-01-01
      • 2012-04-12
      • 1970-01-01
      • 2016-04-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多