【问题标题】:sending file using php CURL on IIS server not working在 IIS 服务器上使用 php CURL 发送文件不起作用
【发布时间】:2015-04-24 22:12:54
【问题描述】:

我完全被这种情况所困扰, 问题1: 我想将文件从一台服务器发送到另一台服务器,我正在使用 curl 发送文件,这是通过 curl 发送文件的标准或更常见的代码 `$url = $callthis; $filename = $_FILES['orig_file_name']['name']; $filedata = $_FILES['orig_file_name']['tmp_name']; $filesize = $_FILES['orig_file_name']['size']; if ($filedata != '') {

    $headers = array("Content-Type:multipart/form-data"); // cURL headers for file uploading
    $postfields = array("orig_file_name" => "@$filedata", "filename" => $filename, 'user_id' => $this->Session->read('userid'),
                'artwork_type'=>$this->request->data['artwork_type'],
                'description'=>$this->request->data['description'],
                'itemstable_id'=>$this->request->data['itemstable_id'],
                'upload_date'=>$this->request->data['upload_date'],
                'brand_Approved'=>'New');
    $ch = curl_init();
    $options = array(
        CURLOPT_URL => $url,
        CURLOPT_HEADER => true,
        CURLOPT_POST => 1,
        CURLOPT_HTTPHEADER => $headers,
        CURLOPT_POSTFIELDS => $postfields,
        CURLOPT_INFILESIZE => $filesize,
        CURLOPT_RETURNTRANSFER => true
    ); // cURL options
    curl_setopt_array($ch, $options);
    curl_exec($ch);
    if(!curl_errno($ch))
    {

       $info = curl_getinfo($ch);
       debug($info);
        if ($info['http_code'] == 200)
           return $this->redirect(array('action' => 'uploadedart'));     
    }
    else
    {

        $errmsg = curl_error($ch);
         $this->Session->setFlash(__($errmsg));
    }
    curl_close($ch);
}`

fyi,curl 已安装在服务器上并启用 这段代码在我的本地 PC 上运行完美,我希望在 linux 系统上也一样,但是我的代码将在 amazone IIS 服务器上实现,所以当我上传代码然后我测试它给我 0 状态代码时,当我谷歌那个他们说您的 URL 不正确,在我的情况下是正确的,所以问题是在文件名之前的 curl 请求中放置了“@”符号,并且在 php curl 文档中他们说必须使用 @ 符号才能告诉接收服务器它是一个物理文件而不是纯文本,当我删除这个@符号时,我从接收端收到 200 个响应代码,但文件没有保存在所需的目录中.....

问题 2:我从 stackoverflow 中的一个人那里实现了另一个代码 ` $url = $callthis; $filename = $_FILES['orig_file_name']['name']; $filedata = $_FILES['orig_file_name']['tmp_name']; $filesize = $_FILES['orig_file_name']['size']; //调试($this->request->data);exit(); if ($filedata != ''){

    move_uploaded_file($filedata, APP."webroot".DS."upload".DS.$filename);
    $newfile=APP."webroot".DS."upload".DS.$filename;
    //exit;
    /* begin stuff */

    $postfields = array('user_id' => $this->Session->read('userid'),
                'artwork_type'=>$this->request->data['artwork_type'],
                'description'=>$this->request->data['description'],
                'itemstable_id'=>$this->request->data['itemstable_id'],
                'upload_date'=>$this->request->data['upload_date'],
                'brand_Approved'=>'New');

    $file_url = $newfile;  //here is the file route, in this case is on same directory but you can set URL too like "http://examplewebsite.com/test.txt"
    $eol = "\r\n"; //default line-break for mime type
    $BOUNDARY = md5(time()); //random boundaryid, is a separator for each param on my post curl function
    $BODY=""; //init my curl body
    $BODY.= '--'.$BOUNDARY. $eol; //start param header
    $BODY .= 'Content-Disposition: form-data; name="otherfields"' . $eol . $eol; // last Content with 2 $eol, in this case is only 1 content.
    $BODY .= serialize($postfields) . $eol;//param data in this case is a simple post data and 1 $eol for the end of the data
    $BODY.= '--'.$BOUNDARY. $eol; // start 2nd param,
    $BODY.= 'Content-Disposition: form-data; name="orig_file_name"; filename="'.$filename.'"'. $eol ; //first Content data for post file, remember you only put 1 when you are going to add more Contents, and 2 on the last, to close the Content Instance
    $BODY.= 'Content-Type: application/octet-stream' . $eol; //Same before row
    $BODY.= 'Content-Transfer-Encoding: base64' . $eol . $eol; // we put the last Content and 2 $eol,
    $BODY.= chunk_split(base64_encode(file_get_contents($file_url))) . $eol; // we write the Base64 File Content and the $eol to finish the data,
    $BODY.= '--'.$BOUNDARY .'--' . $eol. $eol; // we close the param and the post width "--" and 2 $eol at the end of our boundary header.



    $ch = curl_init(); //init curl
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                     'X_PARAM_TOKEN : 71e2cb8b-42b7-4bf0-b2e8-53fbd2f578f9' //custom header for my api validation you can get it from $_SERVER["HTTP_X_PARAM_TOKEN"] variable
                     ,"Content-Type: multipart/form-data; boundary=".$BOUNDARY) //setting our mime type for make it work on $_FILE variable
                );
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/1.0 (Windows NT 6.1; WOW64; rv:28.0) Gecko/20100101 Firefox/28.0'); //setting our user agent
    curl_setopt($ch, CURLOPT_URL, $url); //setting our api post url
    //curl_setopt($ch, CURLOPT_COOKIEJAR, $BOUNDARY.'.txt'); //saving cookies just in case we want
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); // call return content
    curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1); //navigate the endpoint
    curl_setopt($ch, CURLOPT_POST, true); //set as post
    curl_setopt($ch, CURLOPT_POSTFIELDS, $BODY); // set our $BODY 


    $response = curl_exec($ch); // start curl navigation

 print_r($response); //print response

this works fine but only with text files because it doeschunk_split(base64_encode(file_get_contents($file_url)))` 我的意思是读取要发送的文件,这在文本文件中可以正常工作,但在我的情况下,我想发送问题 2 中不起作用的图像因为它修改了图像内容,因此无法打开图像....

仅供参考,我不能发送 jquery/ajax 文件,我需要通过 php 发送,所以答案应该是有效的可行 curl 请求代码来发送文件或 cakephp httpsocket 发送文件

【问题讨论】:

    标签: php cakephp iis curl amazon


    【解决方案1】:

    请注意,PHP >=5.6 默认禁用 curl 的 @file 约定,因此如果您使用不同的版本,这可能是您的问题。

    要将现有代码与 PHP >=5.6 一起使用,您可以添加

    CURLOPT_SAFE_UPLOAD => false
    

    到您的选项数组。

    如果这是您的问题,请参阅更改日志了解更多信息: http://us3.php.net/manual/en/migration56.changed-functions.php

    否则,请明确一点:这不应该与您的 Web 服务器(IIS 与 Apache)直接相关,除非服务器使用不同的 php 配置,或者运行您的 Web 服务器服务的用户附加了权限影响你的脚本。

    所以您确定 php/curl 库在您的环境中正确运行?: 有时我只是在终端命令提示符下做一个“hello word”测试,例如:

    php -r "$ch = curl_init('http://www.google.com'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($ch); die($output);"
    

    这应该只是将 google.com 的代码转储到您的终端中。这行得通,对吧?

    对你的 url 做同样的事情......特别是如果你使用 SSL!您可能会遇到 SSL 问题,例如对等验证等!另请注意,如果上述内容适用于 https://your.url.com,则如果 SSL 库依赖项(对于 IIS 的情况为 libeay32.dll 和 ssleay32.dll)可能无法访问,则可能无法保证它在您的 Web 应用程序中有效。

    状态码 0 似乎表明根据上述测试会弹出一些东西,但它可能由于其他原因放弃了幽灵:

    例如,您确定代码中的 $filedata 是可访问路径吗?也许在设置这些帖子字段之前尝试一下:

    if(!is_file($filedata)) die('哪里是' . $filedata . '?');

    【讨论】:

    • 我的php版本是5.5
    【解决方案2】:

    @filename 在 PHP >= 5.5.0 中已被弃用:

    使用 new CurlFile 代替,不需要在接收端进行任何更改。

    【讨论】:

      猜你喜欢
      • 2012-01-12
      • 1970-01-01
      • 2013-05-13
      • 1970-01-01
      • 2019-07-17
      • 1970-01-01
      • 2020-11-04
      • 2017-10-06
      • 2018-03-14
      相关资源
      最近更新 更多