Posted By

stephanepericat on 03/08/10


Tagged

curl php couchdb


Versions (?)

Upload an attachment to CouchDb


 / Published in: PHP
 

Here is how to upload an attachment to a CouchDb document using PHP and cURL.

  1. /**
  2. * author: Stephane P. Pericat
  3. * date: 2010-03-08
  4. */
  5.  
  6.  
  7. <?php
  8.  
  9. class Couch {
  10. static $url = "http://localhost:5984";
  11.  
  12. static function upload($path, $rev, $filepath, $filename, $content_type) {
  13. $ch = curl_init();
  14. $fullpath = self::$url.$path.$filename.'?rev='.$rev;
  15.  
  16. $data = file_get_contents($filepath.$filename);
  17.  
  18. $options = array(
  19. CURLOPT_URL => $fullpath,
  20. CURLOPT_RETURNTRANSFER => true,
  21. CURLOPT_CUSTOMREQUEST => 'PUT',
  22. CURLOPT_HTTPHEADER => array (
  23. "Content-Type: ".$content_type,
  24. ),
  25. CURLOPT_POST => true,
  26. CURLOPT_POSTFIELDS => $data
  27. );
  28. curl_setopt_array($ch, $options);
  29.  
  30. $process = curl_exec($ch);
  31. curl_close($ch);
  32. return $process;
  33. }
  34. }
  35.  
  36. $test = Couch::upload('/test/upload/', '1-a6eacb015eaedf5bcfb253430281bb65', '/var/www/test/', 'chrome.jpg', 'image/jpeg');
  37.  
  38. var_dump($test);
  39.  
  40. ?>

Report this snippet  

You need to login to post a comment.