curl_file_upload.phpt 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. --TEST--
  2. CURL file uploading
  3. --EXTENSIONS--
  4. curl
  5. --FILE--
  6. <?php
  7. function testcurl($ch, $name, $mime = '', $postname = '')
  8. {
  9. if(!empty($postname)) {
  10. $file = new CurlFile($name, $mime, $postname);
  11. } else if(!empty($mime)) {
  12. $file = new CurlFile($name, $mime);
  13. } else {
  14. $file = new CurlFile($name);
  15. }
  16. curl_setopt($ch, CURLOPT_POSTFIELDS, array("file" => $file));
  17. var_dump(curl_exec($ch));
  18. }
  19. include 'server.inc';
  20. $host = curl_cli_server_start();
  21. $ch = curl_init();
  22. curl_setopt($ch, CURLOPT_URL, "{$host}/get.inc?test=file");
  23. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  24. testcurl($ch, __DIR__ . '/curl_testdata1.txt');
  25. testcurl($ch, __DIR__ . '/curl_testdata1.txt', 'text/plain');
  26. testcurl($ch, __DIR__ . '/curl_testdata1.txt', '', 'foo.txt');
  27. testcurl($ch, __DIR__ . '/curl_testdata1.txt', 'text/plain', 'foo.txt');
  28. $file = new CurlFile(__DIR__ . '/curl_testdata1.txt');
  29. $file->setMimeType('text/plain');
  30. var_dump($file->getMimeType());
  31. var_dump($file->getFilename());
  32. curl_setopt($ch, CURLOPT_POSTFIELDS, array("file" => $file));
  33. var_dump(curl_exec($ch));
  34. $file = curl_file_create(__DIR__ . '/curl_testdata1.txt');
  35. $file->setPostFilename('foo.txt');
  36. var_dump($file->getPostFilename());
  37. curl_setopt($ch, CURLOPT_POSTFIELDS, array("file" => $file));
  38. var_dump(curl_exec($ch));
  39. try {
  40. curl_setopt($ch, CURLOPT_SAFE_UPLOAD, 0);
  41. } catch (ValueError $exception) {
  42. echo $exception->getMessage() . "\n";
  43. }
  44. $params = array('file' => '@' . __DIR__ . '/curl_testdata1.txt');
  45. curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
  46. var_dump(curl_exec($ch));
  47. curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
  48. $params = array('file' => '@' . __DIR__ . '/curl_testdata1.txt');
  49. curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
  50. var_dump(curl_exec($ch));
  51. curl_setopt($ch, CURLOPT_URL, "{$host}/get.inc?test=post");
  52. $params = array('file' => '@' . __DIR__ . '/curl_testdata1.txt');
  53. curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
  54. var_dump(curl_exec($ch));
  55. curl_close($ch);
  56. ?>
  57. --EXPECTF--
  58. string(%d) "curl_testdata1.txt|application/octet-stream|6"
  59. string(%d) "curl_testdata1.txt|text/plain|6"
  60. string(%d) "foo.txt|application/octet-stream|6"
  61. string(%d) "foo.txt|text/plain|6"
  62. string(%d) "text/plain"
  63. string(%d) "%s/curl_testdata1.txt"
  64. string(%d) "curl_testdata1.txt|text/plain|6"
  65. string(%d) "foo.txt"
  66. string(%d) "foo.txt|application/octet-stream|6"
  67. curl_setopt(): Disabling safe uploads is no longer supported
  68. string(0) ""
  69. string(0) ""
  70. string(%d) "array(1) {
  71. ["file"]=>
  72. string(%d) "@%s/curl_testdata1.txt"
  73. }
  74. "