Zend_Http_Client::setFileUpload PHP Method

setFileUpload() public method

Can be used in two ways: 1. $data is null (default): $filename is treated as the name if a local file which will be read and sent. Will try to guess the content type using mime_content_type(). 2. $data is set - $filename is sent as the file name, but $data is sent as the file contents and no file is read from the file system. In this case, you need to manually set the Content-Type ($ctype) or it will default to application/octet-stream.
public setFileUpload ( string $filename, string $formname, string $data = null, string $ctype = null ) : Zend_Http_Client
$filename string Name of file to upload, or name to save as
$formname string Name of form element to send as
$data string Data to send (if null, $filename is read and sent)
$ctype string Content type to use (if $data is set and $ctype is null, will be application/octet-stream)
return Zend_Http_Client
    public function setFileUpload($filename, $formname, $data = null, $ctype = null)
    {
        if ($data === null) {
            if (($data = @file_get_contents($filename)) === false) {
                /** @see Zend_Http_Client_Exception */
                require_once 'Zend/Http/Client/Exception.php';
                throw new Zend_Http_Client_Exception("Unable to read file '{$filename}' for upload");
            }
            if (!$ctype) {
                $ctype = $this->_detectFileMimeType($filename);
            }
        }
        // Force enctype to multipart/form-data
        $this->setEncType(self::ENC_FORMDATA);
        $this->files[] = array('formname' => $formname, 'filename' => basename($filename), 'ctype' => $ctype, 'data' => $data);
        $this->body_field_order[$formname] = self::VTYPE_FILE;
        return $this;
    }

Usage Example

Example #1
0
 /**
  * Add an media file to mobypicture.
  *
  * @param string $filepath
  * @param string $title
  * @param string $description
  * @param string $format
  * @param array $options
  * @throws Zend_Http_Client_Exception if HTTP request fails or times out
  * @throws HausDesign_Service_Mobypicture_Exception If username is not set
  * @throws HausDesign_Service_Mobypicture_Exception If file can't read.
  * @throws HausDesign_Service_Mobypicture_Exception If file is larger then 16M.
  * @return mixed
  */
 public function postMedia($filepath, $title, $description = '', $format = self::FORMAT_XML, $options = array())
 {
     if (null == $this->_username || null == $this->_password) {
         throw new HausDesign_Service_Mobypicture_Exception('Username and password must be set.');
     }
     if (!is_readable($filepath)) {
         throw new HausDesign_Service_Mobypicture_Exception('File can\'t be read.');
     }
     if (filesize($filepath) > 16777216) {
         throw new HausDesign_Service_Mobypicture_Exception('File can\'t be larger then 16M.');
     }
     if (strlen($title) > self::MAX_LENGTH_TITLE) {
         $title = substr($title, 0, self::MAX_LENGTH_TITLE);
     }
     if (strlen($description) > self::MAX_LENGTH_DESCRIPTION) {
         $title = substr($title, 0, self::MAX_LENGTH_DESCRIPTION);
     }
     $options['t'] = $title;
     if ($description) {
         $options['d'] = $description;
     }
     $this->_localHttpClient->resetParameters();
     $this->_localHttpClient->setUri(self::MOBYPICTURE_API);
     $this->_localHttpClient->setParameterPost('action', 'postMediaUrl');
     $this->_localHttpClient->setFileUpload($filepath, 'i');
     $this->_localHttpClient->setParameterPost('u', $this->_username);
     $this->_localHttpClient->setParameterPost('p', $this->_password);
     $this->_localHttpClient->setParameterPost('k', $this->_apiKey);
     $this->_localHttpClient->setParameterPost('format', $format);
     $this->_localHttpClient->setConfig(array('timeout' => 30));
     foreach ($options as $option => $value) {
         $this->_localHttpClient->setParameterPost($option, $value);
     }
     return $this->_parseContent($this->_localHttpClient->request('GET')->getBody(), $format);
 }
All Usage Examples Of Zend_Http_Client::setFileUpload