-
-
Save TLWebdesign/77f214631a0e3c994fd2dcc062b7542a to your computer and use it in GitHub Desktop.
Upload Script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
I've edited the original to be able to upload a field called "photo". | |
I also opted for not including the full path in my database so there is a bit more seperation | |
between database and file location. | |
Since these images are extension specific I opted to place them in the media folder like this: | |
'/media/com_[[[component]]]/images/[[[views]]]/'. This way i can keep my uploads seperate. | |
I also use the default componentHelper::randomKey(48); to generate a random key i append to the | |
filename to prevent double filenames being uploaded. | |
I also use custom code snippet for the model This way i can add a "width" for the image to resize to: | |
[CUSTOMCODE=phpModelPhotoUpload+1200]. You'll find [[[args0]]] in the model method. | |
The original code doesn't account for "Save & New". I've added code to take this into account. | |
On save create the folder if it does not exist. And i also remove the old file. | |
Also i created a script for deleting the images when the item is permanently deleted form the trash. | |
The script gets the items that contain an "photo" string (eg. filename.extension) from the database | |
and loops through them searching the database again for each item it finds and checks if there are | |
more items withthe same image as the one being deleted. If no more items contain the same image, it | |
will delete the image. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// DELETE METHOD CODE FOR DELETING PHOTO | |
$db = JFactory::getDbo(); | |
$query = $db->getQuery(true); | |
$query->select($db->quoteName(array('id','photo'))); | |
$query->from($db->quoteName('#__[[[component]]]_[[[view]]]')); | |
$query->where($db->quoteName('id') . ' IN (' . implode(',', $pks) . ')'); | |
$query->where($db->quoteName('photo') . ' NOT LIKE ' . $db->quote('')); | |
$db->setQuery($query); | |
$results = $db->loadObjectList(); | |
foreach ($results as $result) { | |
// CHECK IF IT IS THE LAST INSTANCE OF THIS IMAGE | |
$query = $db->getQuery(true); | |
$query->select($db->quoteName('id')); | |
$query->from($db->quoteName('#__[[[component]]]_[[[view]]]')); | |
$query->where($db->quoteName('photo') . ' LIKE ' . $db->quote($result->photo)); | |
$query->where($db->quoteName('id') . ' NOT LIKE ' . $result->id); | |
$db->setQuery($query); | |
$db->execute(); | |
$num_rows = $db->getNumRows(); | |
if ($num_rows == 0) { | |
$path = JPATH_ROOT.'/media/com_[[[component]]]/images/[[[views]]]/'; | |
$this->removeFile($path.$result->photo); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php if (isset($this->item->photo) && [[[Component]]]Helper::checkString($this->item->photo)) : ?> | |
jQuery('<img src="/media/com_[[[component]]]/images/[[[views]]]/<?php echo $this->item->photo; ?>" class="photo-img" alt="photo">').insertAfter(jQuery('#jform_photo').closest('.control-group')); | |
<?php endif; ?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Uploads file and places it in apropriate place.. | |
* | |
* @return package or false on failure. | |
*/ | |
protected function myUploadFunction($input) | |
{ | |
// get the array of values | |
$userfile = $input->files->get('jform', null, 'array'); | |
if (is_array($userfile)) | |
{ | |
$userfile = array_values($userfile)[0]; | |
} | |
// Make sure that file uploads are enabled in php. | |
if (!(bool) ini_get('file_uploads')) | |
{ | |
JError::raiseWarning('', JText::_('File uploads are disabled in PHP.')); | |
return false; | |
} | |
// If there is no uploaded file, we have a problem... | |
if (!is_array($userfile)) | |
{ | |
JError::raiseWarning('', JText::_('There is no uploaded file.')); | |
return false; | |
} | |
// Is the PHP tmp directory missing? | |
if ($userfile['error'] && ($userfile['error'] == UPLOAD_ERR_NO_TMP_DIR)) | |
{ | |
JError::raiseWarning( | |
'', | |
JText::_('Error') | |
); | |
return false; | |
} | |
// Is the max upload size too small in php.ini? | |
if ($userfile['error'] && ($userfile['error'] == UPLOAD_ERR_INI_SIZE)) | |
{ | |
JError::raiseWarning( | |
'', | |
JText::_('File to large') | |
); | |
return false; | |
} | |
// Check if there was a different problem uploading the file. | |
if ($userfile['error'] || $userfile['size'] < 1) | |
{ | |
if (($userfile['error'] == UPLOAD_ERR_NO_FILE)) { | |
JError::raiseNotice('', JText::_('No (new) image uploaded.')); | |
} else { | |
JError::raiseWarning('', JText::_('Error while uploading!')); | |
} | |
return false; | |
} | |
// Get classes | |
jimport('joomla.filesystem.file'); | |
jimport('joomla.filesystem.folder'); | |
// Clean the name | |
$userfile['name'] = JPath::clean($userfile['name']); | |
// Build the appropriate paths. | |
$config = JFactory::getConfig(); | |
$tmp_dest = $config->get('tmp_path') . '/' . $userfile['name']; | |
$tmp_src = $userfile['tmp_name']; | |
// Move uploaded file. | |
$p_file = JFile::upload($tmp_src, $tmp_dest, false, true); | |
// Was the package uploaded? | |
if (!$p_file) | |
{ | |
JError::raiseWarning('', JText::_('Upload failed!')); | |
return false; | |
} | |
// check that this is a valid file | |
$package = $this->checkUpload($userfile, $tmp_dest); | |
return $package; | |
} | |
/** | |
* Check a file and verifies it as a allowed file format file | |
* | |
* @param string $archivename The uploaded package filename or import directory | |
* | |
* @return array of elements | |
* | |
*/ | |
protected function checkUpload($archive, $tmp_dest) | |
{ | |
// get file format | |
$fileFormat = strtolower(pathinfo($archive['name'], PATHINFO_EXTENSION)); | |
// get fileFormat key | |
$allowedFormats = array('jpg','jpeg','png','gif'); | |
// check the extension | |
if (!in_array($fileFormat, $allowedFormats)) | |
{ | |
// Cleanup the import files | |
$this->removeFile($tmp_dest); | |
$this->errorMessage = JText::_('File format not allowed.'); | |
return false; | |
} | |
$resizedImage = $this->resizeImage([[[arg0]]],$tmp_dest); | |
if ($resizedImage) | |
{ | |
$tmp_dest = $resizedImage; | |
} | |
// set directory | |
$archive['full_path'] = $tmp_dest; | |
// set format | |
$archive['format'] = $fileFormat; | |
return $archive; | |
} | |
/** | |
* Clean up temporary uploaded file | |
* | |
* @param string $package Name of the uploaded file | |
* | |
* @return boolean True on success | |
* | |
*/ | |
protected function removeFile($package) | |
{ | |
// Is the package file a valid file? | |
if (is_file($package)) | |
{ | |
JFile::delete($package); | |
} | |
elseif (is_file(JPath::clean($package))) | |
{ | |
// It might also be just a base filename | |
JFile::delete(JPath::clean($package)); | |
} | |
} | |
/** | |
* Resize image | |
* | |
* @param string $width Width of the file to resize too. | |
* | |
* @return $tmp_dest on success | |
* | |
*/ | |
protected function resizeImage($width = 800, $tmp_dest) | |
{ | |
$image = imagecreatefromstring(file_get_contents($tmp_dest)); | |
if (is_resource($image)) | |
{ | |
$old_width = imageSX($image); | |
$old_height = imageSY($image); | |
$new_width = $old_width; | |
$new_height = $old_height; | |
if ($old_width > $old_height && $old_width > $width) | |
{ | |
$new_width = $width; | |
$new_height = $old_height*($width/$old_width); | |
} | |
elseif ($old_width < $old_height && $old_height > $width) | |
{ | |
$new_height = $width; | |
$new_width = $old_width*($width/$old_height); | |
} elseif ( $old_width > $width && $old_height > $width ) | |
{ | |
$new_height = $width; | |
$new_width = $width; | |
} | |
if ($new_height != $old_height && $new_width != $old_width) | |
{ | |
$image = imagescale($image, $new_width, $new_height); | |
if (is_resource($image)) | |
{ | |
$tmp_dest = JFile::stripExt($tmp_dest) . '.jpg'; | |
imagejpeg($image, $tmp_dest, 60); | |
imagedestroy($image); | |
} | |
} | |
} | |
return $tmp_dest; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// upload the image | |
if ($imageArray = $this->myUploadFunction($input)) | |
{ | |
// store the banner path to the database | |
$randomid = [[[Component]]]Helper::randomkey(48); | |
$path = '/media/com_[[[component]]]/images/[[[views]]]/'; | |
$data['photo'] = 'photo'.$randomid.$imageArray['name']; | |
// create folder if not exist | |
JFolder::create(JPATH_ROOT . $path); | |
// move the file into place | |
$final_dest = JPATH_ROOT . $path . $data['photo']; | |
JFile::move($imageArray['full_path'], $final_dest); | |
// get old record | |
$oldItem = $this->getTable(); | |
$oldItem->load( $input->getInt('id') ); | |
// CHECK IF IT IS THE LAST INSTANCE OF THIS IMAGE | |
$db = JFactory::getDbo(); | |
$query = $db->getQuery(true); | |
$query->select($db->quoteName('id')); | |
$query->from($db->quoteName('#__[[[component]]]_[[[view]]]')); | |
$query->where($db->quoteName('photo') . ' LIKE ' . $db->quote($oldItem->photo)); | |
$query->where($db->quoteName('id') . ' NOT LIKE ' . $input->getInt('id')); | |
$db->setQuery($query); | |
$db->execute(); | |
$num_rows = $db->getNumRows(); | |
if ($num_rows == 0) | |
{ | |
// remove old file | |
$this->removeFile( JPATH_ROOT . $path . $oldItem->photo ); | |
} | |
} | |
if ($input->get('task') === 'save2copy') | |
{ | |
$origTable = clone $this->getTable(); | |
$origTable->load($input->getInt('id')); | |
$data['photo'] = $origTable->photo; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment