Open Journal Systems  3.3.0
PostFile.php
1 <?php
2 
3 namespace Guzzle\Http\Message;
4 
8 
12 class PostFile implements PostFileInterface
13 {
14  protected $fieldName;
15  protected $contentType;
16  protected $filename;
17  protected $postname;
18 
25  public function __construct($fieldName, $filename, $contentType = null, $postname = null)
26  {
27  $this->fieldName = $fieldName;
28  $this->setFilename($filename);
29  $this->postname = $postname ? $postname : basename($filename);
30  $this->contentType = $contentType ?: $this->guessContentType();
31  }
32 
33  public function setFieldName($name)
34  {
35  $this->fieldName = $name;
36 
37  return $this;
38  }
39 
40  public function getFieldName()
41  {
42  return $this->fieldName;
43  }
44 
45  public function setFilename($filename)
46  {
47  // Remove leading @ symbol
48  if (strpos($filename, '@') === 0) {
49  $filename = substr($filename, 1);
50  }
51 
52  if (!is_readable($filename)) {
53  throw new InvalidArgumentException("Unable to open {$filename} for reading");
54  }
55 
56  $this->filename = $filename;
57 
58  return $this;
59  }
60 
61  public function setPostname($postname)
62  {
63  $this->postname = $postname;
64 
65  return $this;
66  }
67 
68  public function getFilename()
69  {
70  return $this->filename;
71  }
72 
73  public function getPostname()
74  {
75  return $this->postname;
76  }
77 
78  public function setContentType($type)
79  {
80  $this->contentType = $type;
81 
82  return $this;
83  }
84 
85  public function getContentType()
86  {
87  return $this->contentType;
88  }
89 
90  public function getCurlValue()
91  {
92  // PHP 5.5 introduced a CurlFile object that deprecates the old @filename syntax
93  // See: https://wiki.php.net/rfc/curl-file-upload
94  if (function_exists('curl_file_create')) {
95  return curl_file_create($this->filename, $this->contentType, $this->postname);
96  }
97 
98  // Use the old style if using an older version of PHP
99  $value = "@{$this->filename};filename=" . $this->postname;
100  if ($this->contentType) {
101  $value .= ';type=' . $this->contentType;
102  }
103 
104  return $value;
105  }
106 
111  public function getCurlString()
112  {
113  Version::warn(__METHOD__ . ' is deprecated. Use getCurlValue()');
114  return $this->getCurlValue();
115  }
116 
120  protected function guessContentType()
121  {
122  return Mimetypes::getInstance()->fromFilename($this->filename) ?: 'application/octet-stream';
123  }
124 }
Guzzle\Http\Message\PostFile\$postname
$postname
Definition: PostFile.php:17
Guzzle\Http\Mimetypes
Definition: Mimetypes.php:9
Guzzle\Http\Message\PostFile\setContentType
setContentType($type)
Definition: PostFile.php:78
Guzzle\Http\Message\PostFile\$contentType
$contentType
Definition: PostFile.php:15
Guzzle\Http\Message\PostFile\getCurlValue
getCurlValue()
Definition: PostFile.php:90
Guzzle\Http\Message\PostFile\__construct
__construct($fieldName, $filename, $contentType=null, $postname=null)
Definition: PostFile.php:25
Guzzle\Http\Message
Definition: AbstractMessage.php:3
Guzzle\Common\Version\warn
static warn($message)
Definition: Version.php:23
Guzzle\Http\Message\PostFile\getFilename
getFilename()
Definition: PostFile.php:68
Guzzle\Http\Message\PostFile
Definition: PostFile.php:12
Guzzle\Http\Message\PostFile\setFilename
setFilename($filename)
Definition: PostFile.php:45
Guzzle\Http\Message\PostFile\setPostname
setPostname($postname)
Definition: PostFile.php:61
Guzzle\Common\Exception\InvalidArgumentException
Definition: lib/vendor/guzzle/guzzle/src/Guzzle/Common/Exception/InvalidArgumentException.php:5
Guzzle\Common\Version
Definition: Version.php:8
Guzzle\Http\Message\PostFile\getContentType
getContentType()
Definition: PostFile.php:85
Guzzle\Http\Message\PostFile\$fieldName
$fieldName
Definition: PostFile.php:14
Guzzle\Http\Message\PostFile\getPostname
getPostname()
Definition: PostFile.php:73
Guzzle\Http\Mimetypes\getInstance
static getInstance()
Definition: Mimetypes.php:930
Guzzle\Http\Message\PostFile\guessContentType
guessContentType()
Definition: PostFile.php:120
Guzzle\Http\Message\PostFile\getCurlString
getCurlString()
Definition: PostFile.php:111
Guzzle\Http\Message\PostFile\$filename
$filename
Definition: PostFile.php:16
Guzzle\Http\Message\PostFile\getFieldName
getFieldName()
Definition: PostFile.php:40
Guzzle\Http\Message\PostFileInterface
Definition: PostFileInterface.php:10
Guzzle\Http\Message\PostFile\setFieldName
setFieldName($name)
Definition: PostFile.php:33