cURL / Mailing Lists / curl-and-php / Single Mail

curl-and-php

help with PUT and CURLOPT_READFUNCTION

From: James Goddard <james_goddard0511_at_yahoo.com>
Date: Thu, 1 May 2008 10:13:22 -0700 (PDT)

Hi!

I can successfully PUT a file using CURLOPT_PUT, CURLOPT_INFILE, and CURLOPT_INFILESIZE. However, for large files ( > ~100MB) my machine runs out of memory so I want to be able to "stream" the file to the webserver (instead of storing it in memory and then sending) via CURLOPT_READFUNCTION. Unfortunately, I can't get this to work and cannot find any working examples online. I've given both my working and non-working code below. Interestingly, method 1 below (which does not work) takes about 30 seconds to PUT, while method 2 (works) happens practically instantly.Any help is truly appreciated!

========= DOES NOT WORK =========

if ($method == 'PUT')
{
  $this->fp = fopen($file, 'r');
  $this->fileSize = filesize($file);

  curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT');
  curl_setopt($curl, CURLOPT_READFUNCTION, array($this, 'stream_file'));
  curl_setopt($curl, CURLOPT_UPLOAD, true);

  $request_headers[] = 'Content-Length: '.$this->fileSize;
  $request_headers[] = "Expect: ";
  $request_headers[] = "Transfer-Encoding: ";

  curl_setopt($this->curl, CURLOPT_HTTPHEADER, $request_headers);

  ...
}

protected function stream_file($curl, $fileData, $fileSize)
{
    return fread($this->fp, $this->fileSize);
}

* About to connect() to 127.0.0.1 port 8000 (#0)
* Trying 127.0.0.1... * connected
* Connected to 127.0.0.1 (127.0.0.1) port 8000 (#0)
> PUT /files/foo.mpeg HTTP/1.1
Host: 127.0.0.1:8000
Accept: */*
Accept-Encoding: gzip,deflate
Content-Length: 60153860

  % Total % Received % Xferd Average Speed Time Time Time Current
                                 Dload Upload Total Spent Left Speed
100 16384 0 0 0 16384 0 453 --:--:-- 0:00:36 --:--:-- 0* Empty reply from server
100 16384 0 0 0 16384 0 453 --:--:-- 0:00:36 --:--:-- 0* Connection #0 to host 127.0.0.1 left intact

========= WORKS =========

if ($method == 'PUT')
{
  $fp = fopen($file, 'r');
  $fileSize = filesize($file);
  curl_setopt($curl, CURLOPT_PUT, 1);
  curl_setopt($curl, CURLOPT_INFILE, $fp);
  curl_setopt($curl, CURLOPT_INFILESIZE, $fileSize); fclose($fp);
  ...
}

* About to connect() to 127.0.0.1 port 8000 (#0)
* Trying 127.0.0.1... * connected
* Connected to 127.0.0.1 (127.0.0.1) port 8000 (#0)
> PUT /files/foo.mpeg HTTP/1.1
Host: 127.0.0.1:8000
Accept: */*
Accept-Encoding: gzip,deflate
Content-Length: 60153860
Expect: 100-continue

  % Total % Received % Xferd Average Speed Time Time Time Current
                                 Dload Upload Total Spent Left Speed
  0 57.3M 0 0 0 0 0 0 --:--:-- 0:00:02 --:--:-- 0* HTTP 1.0, assume close after body
< HTTP/1.0 200 OK
< Content-Type: text/html
< Content-Length: 18
* HTTP/1.0 connection set to keep alive!
< Connection: keep-alive
< Keep-Alive: timeout=30, max=100
<
100 57.3M 0 18 100 57.3M 6 21.3M 0:00:02 0:00:02 --:--:-- 33.9M* Connection #0 to host 127.0.0.1 left intact

* Closing connection #0

       
---------------------------------
Be a better friend, newshound, and know-it-all with Yahoo! Mobile. Try it now.

_______________________________________________
http://cool.haxx.se/cgi-bin/mailman/listinfo/curl-and-php
Received on 2008-05-01