Skip to content
Browse files

Merge pull request #712 from dlpetrie/patch-5

adds in ftp upload option in the upload class
  • Loading branch information...
2 parents 592aa4c + 0b71a17 commit ac11106980a0dc0bc1e1004d2f7c623689308760 @dhrrgn dhrrgn committed Dec 6, 2011
Showing with 78 additions and 43 deletions.
  1. +4 −4 classes/ftp.php
  2. +74 −39 classes/upload.php
View
8 classes/ftp.php
@@ -54,8 +54,8 @@ public static function factory($config = 'default', $connect = true)
*
* $ftp = static::forge('group');
*
- * @param string Ftp filename
- * @param array array of values
+ * @param string|array The name of the config group to use, or a configuration array.
+ * @param bool Automatically connect to this server.
* @return Ftp
*/
public static function forge($config = 'default', $connect = true)
@@ -71,8 +71,8 @@ public static function forge($config = 'default', $connect = true)
/**
* Sets the initial Ftp filename and local data.
*
- * @param string Ftp filename
- * @param array array of values
+ * @param string|array The name of the config group to use, or a configuration array.
+ * @param bool Automatically connect to this server.
* @return void
*/
public function __construct($config = 'default')
View
113 classes/upload.php
@@ -49,6 +49,7 @@ class Upload
const UPLOAD_ERR_MOVE_FAILED = 109;
const UPLOAD_ERR_DUPLICATE_FILE = 110;
const UPLOAD_ERR_MKDIR_FAILED = 111;
+ const UPLOAD_ERR_FTP_FAILED = 112;
/* ---------------------------------------------------------------------------
* STATIC PROPERTIES
@@ -58,29 +59,31 @@ class Upload
* @var array default configuration values
*/
protected static $_defaults = array(
- 'auto_process' => false,
+ 'auto_process' => false,
// validation settings
- 'max_size' => 0,
- 'max_length' => 0,
- 'ext_whitelist' => array(),
- 'ext_blacklist' => array(),
- 'type_whitelist' => array(),
- 'type_blacklist' => array(),
- 'mime_whitelist' => array(),
- 'mime_blacklist' => array(),
+ 'max_size' => 0,
+ 'max_length' => 0,
+ 'ext_whitelist' => array(),
+ 'ext_blacklist' => array(),
+ 'type_whitelist' => array(),
+ 'type_blacklist' => array(),
+ 'mime_whitelist' => array(),
+ 'mime_blacklist' => array(),
// save settings
- 'path' => '',
- 'prefix' => '',
- 'suffix' => '',
- 'extension' => '',
- 'create_path' => true,
- 'path_chmod' => 0777,
- 'file_chmod' => 0666,
- 'auto_rename' => true,
- 'overwrite' => false,
- 'randomize' => false,
- 'normalize' => false,
- 'change_case' => false
+ 'path' => '',
+ 'prefix' => '',
+ 'suffix' => '',
+ 'extension' => '',
+ 'create_path' => true,
+ 'path_chmod' => 0777,
+ 'file_chmod' => 0666,
+ 'auto_rename' => true,
+ 'overwrite' => false,
+ 'randomize' => false,
+ 'normalize' => false,
+ 'change_case' => false,
+ 'ftp_mode' => 'auto',
+ 'ftp_permissions' => null
);
/**
@@ -106,6 +109,11 @@ class Upload
* @var bool indicator of valid uploads
*/
protected static $valid = false;
+
+ /**
+ * @var object Ftp object
+ */
+ protected static $with_ftp = false;
/* ---------------------------------------------------------------------------
* STATIC METHODS
@@ -418,6 +426,19 @@ public static function process($config = array())
// ---------------------------------------------------------------------------
/**
+ * Upload files with Ftp
+ *
+ * @param string|array The name of the config group to use, or a configuration array.
+ * @param bool Automatically connect to this server.
+ */
+ public static function with_ftp($config = 'default', $connect = true)
+ {
+ static::$with_ftp = \Ftp::forge($config, $connect);
+ }
+
+ // ---------------------------------------------------------------------------
+
+ /**
* save uploaded file(s)
*
* @param mixed if int, $files element to move. if array, list of elements to move, if none, move all elements
@@ -629,30 +650,44 @@ public static function save()
// move the uploaded file
if ( ! static::$files[$key]['error'])
{
- if( ! @move_uploaded_file($file['file'], static::$files[$key]['saved_to'].static::$files[$key]['saved_as']) )
+ // check if file should be uploaded with ftp
+ if (static::$with_ftp)
{
- static::$files[$key]['error'] = true;
- static::$files[$key]['errors'][] = array('error' => static::UPLOAD_ERR_MOVE_FAILED);
+ $uploaded = static::$with_ftp->upload($file['file'], static::$config['path'].static::$files[$key]['saved_as'], static::$config['ftp_mode'], static::$config['ftp_permissions']);
+
+ if ( ! $uploaded)
+ {
+ static::$files[$key]['error'] = true;
+ static::$files[$key]['errors'][] = array('error' => static::UPLOAD_ERR_FTP_FAILED);
+ }
}
else
{
- @chmod(static::$files[$key]['saved_to'].static::$files[$key]['saved_as'], static::$config['file_chmod']);
- }
-
- // after callback defined?
- if (array_key_exists('after', static::$callbacks) and ! is_null(static::$callbacks['after']))
- {
- // get the callback method
- $callback = static::$callbacks['after'][0];
-
- // call the callback
- if (is_callable($callback))
+ if( ! @move_uploaded_file($file['file'], static::$files[$key]['saved_to'].static::$files[$key]['saved_as']) )
+ {
+ static::$files[$key]['error'] = true;
+ static::$files[$key]['errors'][] = array('error' => static::UPLOAD_ERR_MOVE_FAILED);
+ }
+ else
+ {
+ @chmod(static::$files[$key]['saved_to'].static::$files[$key]['saved_as'], static::$config['file_chmod']);
+ }
+
+ // after callback defined?
+ if (array_key_exists('after', static::$callbacks) and ! is_null(static::$callbacks['after']))
{
- $result = call_user_func_array($callback, array(&static::$files[$key]));
- if (is_numeric($result))
+ // get the callback method
+ $callback = static::$callbacks['after'][0];
+
+ // call the callback
+ if (is_callable($callback))
{
- static::$files[$key]['error'] = true;
- static::$files[$key]['errors'][] = array('error' => $result);
+ $result = call_user_func_array($callback, array(&static::$files[$key]));
+ if (is_numeric($result))
+ {
+ static::$files[$key]['error'] = true;
+ static::$files[$key]['errors'][] = array('error' => $result);
+ }
}
}
}

0 comments on commit ac11106

Please sign in to comment.
Something went wrong with that request. Please try again.