Skip to content

Commit

Permalink
Properly handle multi-file uploads and error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark-H committed Nov 9, 2011
1 parent cd34dfb commit 3c39112
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 12 deletions.
2 changes: 1 addition & 1 deletion _build/build.transport.php
Expand Up @@ -26,7 +26,7 @@ function getSnippetContent($filename = '') {
define('PKG_RELEASE','dev1');

$root = dirname(dirname(__FILE__)).'/';
$sources= array (
$sources = array (
'root' => $root,
'build' => $root .'_build/',
'resolvers' => $root . '_build/resolvers/',
Expand Down
Expand Up @@ -51,17 +51,32 @@

/* Handle file uploads */
if (is_array($d['image'])) {
$images = array();
$nr = 0;
while (count($d['image']['name']) > $nr) {
$images[] = array(
'name' => $d['image']['name'][$nr],
'type' => $d['image']['type'][$nr],
'tmp_name' => $d['image']['tmp_name'][$nr],
'size' => $d['image']['size'][$nr],
'error' => $d['image']['error'][$nr],
);
$nr++;
}

/* @var bdlImage $img */
foreach ($d['image'] as $i) {
$img = $modx->newObject('bdlImage',array('listing' => $listing->get('id')));
$response = $img->handleUpload($i,$listing->get('id'));
if (is_string($response)) {
$img->set('image',$response);
$img->save();
}
else {
$hook->addError('message',$modx->lexicon('bdlistings.error.fileupload').' '.$response['error']);
return false;
foreach ($images as $i) {
if (!empty($i['name'])) {
$img = $modx->newObject('bdlImage',array('listing' => $listing->get('id')));
$response = $img->handleUpload($i,$listing->get('id'));
if (is_string($response)) {
$img->set('image',$response);
$img->save();
}
else {
$hook->addError('message',$modx->lexicon('bdlistings.error.fileupload',array('file' => $i['name'])).' '.$response['error']);
return false;
}
}
}
}
Expand Down
9 changes: 8 additions & 1 deletion core/components/bdlistings/lexicon/en/default.inc.php
Expand Up @@ -42,9 +42,16 @@
$_lang['bdlistings.error.invalid'] = 'Invalid request.';
$_lang['bdlistings.error.sortinvalid'] = 'Requested sort is not valid: [[+errors]]';
$_lang['bdlistings.error.toodeep'] = 'Category [[+id]] is nested too deep, only two levels is allowed.';
$_lang['bdlistings.error.fileupload'] = 'File upload failed.';
$_lang['bdlistings.error.fileupload'] = 'Uploading [[+file]] failed.';
$_lang['bdlistings.error.filetoobig'] = 'File too big.';
$_lang['bdlistings.error.invalidext'] = 'Invalid file type: [[+ext]].';
$_lang['bdlistings.error.file.1'] = 'File too big: server does not allow uploads of this size.';
$_lang['bdlistings.error.file.2'] = 'File too big: exceeds specified max size.';
$_lang['bdlistings.error.file.3'] = 'Only part of the file was uploaded.';
$_lang['bdlistings.error.file.4'] = 'No file was uploaded.';
$_lang['bdlistings.error.file.6'] = 'Server misconfiguration.';
$_lang['bdlistings.error.file.7'] = 'Failed to write to disk.';
$_lang['bdlistings.error.file.8'] = 'Server misconfiguration.';

/* Fields, somewhat alphabetical */
$_lang['bdlistings.active'] = 'Active';
Expand Down
22 changes: 22 additions & 0 deletions core/components/bdlistings/model/bdlistings/bdlimage.class.php
Expand Up @@ -22,6 +22,14 @@
*
*/
class bdlImage extends xPDOSimpleObject {
/**
* Overrides xPDOSimpleObject's get method for specific image url/path handling.
*
* @param $k Key
* @param $format
* @param $formatTemplate
* @return string Resulting value
*/
public function get($k, $format = null, $formatTemplate= null) {
switch ($k) {
case 'imagepath':
Expand All @@ -47,6 +55,11 @@ public function get($k, $format = null, $formatTemplate= null) {
return $value;
}

/**
* @inherit xPDOObject::remove
* @param array $ancestors
* @return boolean
*/
public function remove(array $ancestors = array()) {
$filename = $this->get('imagepath');
if (!empty($filename)) {
Expand All @@ -57,6 +70,13 @@ public function remove(array $ancestors = array()) {
return parent::remove($ancestors);
}

/**
* Takes an array with name, tmp_name, size, error and type (the regular file post ;) ) and a listing ID to upload the file.
*
* @param array $file
* @param int $listing
* @return array|string
*/
public function handleUpload (array $file = array(), $listing = 0) {
$exts = $this->xpdo->getOption('bdlistings.allowed_extensions',null,'jpg,jpeg,png,gif,ico');
$exts = explode(',',$exts);
Expand All @@ -66,6 +86,8 @@ public function handleUpload (array $file = array(), $listing = 0) {
$extension = pathinfo($file['name'],PATHINFO_EXTENSION);
if (!in_array(strtolower($extension),$exts)) return array('error' => $this->xpdo->lexicon('bdlistings.error.invalidext',array('ext' => $extension)));

if ($file['error'] > 0) { return array('error' => $this->xpdo->lexicon('bdlistings.error.file.'.$file['error'])); }

/* New file upload */
$uploadPath = $this->xpdo->getOption('bdlistings.uploadpath');
if (empty($uploadPath)) $uploadPath = $this->xpdo->getOption('bdlistings.assets_path',null,$this->xpdo->getOption('assets_path')).'uploads/';
Expand Down

0 comments on commit 3c39112

Please sign in to comment.