Subversion Repositories php-qbpwcf

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 liveuser 1
<?php
2
 
3
namespace Guzzle\Http\Message;
4
 
5
use Guzzle\Common\Version;
6
use Guzzle\Common\Exception\InvalidArgumentException;
7
use Guzzle\Http\Mimetypes;
8
 
9
/**
10
 * POST file upload
11
 */
12
class PostFile implements PostFileInterface
13
{
14
    protected $fieldName;
15
    protected $contentType;
16
    protected $filename;
17
    protected $postname;
18
 
19
    /**
20
     * @param string $fieldName   Name of the field
21
     * @param string $filename    Local path to the file
22
     * @param string $postname    Remote post file name
23
     * @param string $contentType Content-Type of the upload
24
     */
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
 
107
    /**
108
     * @deprecated
109
     * @codeCoverageIgnore
110
     */
111
    public function getCurlString()
112
    {
113
        Version::warn(__METHOD__ . ' is deprecated. Use getCurlValue()');
114
        return $this->getCurlValue();
115
    }
116
 
117
    /**
118
     * Determine the Content-Type of the file
119
     */
120
    protected function guessContentType()
121
    {
122
        return Mimetypes::getInstance()->fromFilename($this->filename) ?: 'application/octet-stream';
123
    }
124
}