Magento sort media folder files by filename - sorting

I want to sort media folder files by filename.I tried with
$collection = $this->getCollection($path)
->setCollectDirs(false)
->setCollectFiles(true)
->setCollectRecursively(false)
->setOrder('filename', Varien_Data_Collection::SORT_ORDER_ASC);
but it is not case sensitive.It sorts all upper case words first then lowercase. (Apple,Bat,apple)
Please help !!!

You have to rewrite lib/Varien/Data/Collection/Filesystem.php
and change function
protected function _usort($a, $b)
{
foreach ($this->_orders as $key => $direction) {
$result = $a[$key] > $b[$key] ? 1 : ($a[$key] < $b[$key] ? -1 : 0);
return (self::SORT_ORDER_ASC === strtoupper($direction) ? $result : -$result);
break;
}
}

Please try this:
- create a test.php file in magento root directory.
- change the name of media folder to $sub_dir variable.
<?php
require_once 'app/Mage.php';
Mage::app();
$file = new Varien_Io_File();
$sub_dir = "wysiwyg/new";
$dir = Mage::getBaseDir('media') . DS . $sub_dir . DS;
$file->open(array('path' => $dir));
$fileDetails = $file->ls();
$allFiles = array();
foreach ($fileDetails as $value) {
$allFiles[] = $value['text'];
}
echo "<pre>";
print_r($allFiles);
echo "</pre>";
?>

Related

Change download filename codeigniter

Convert file name before force download.
I am trying to be able to convert the file name of the stored file name to the orignal file name before download
So when user clicks on a file to download instead of showing
post_1486965530_jeJNHKWXPMrwRpGBYxczIfTbaqhLnDVO.php
Like in image below
It will just rename the downloaded file something like
config.php
Question how to only change the downloaded filename when click on image.
public function downloads($id) {
$this->db->where('attachment_id', $id);
$query = $this->db->get('attachments');
if ($query->num_rows() == 0) {
return false;
}
$path = '';
$file = '';
foreach ($query->result_array() as $result) {
$path .= FCPATH . 'uploads/';
// This gives the stored file name
// This is folder 201702
// Looks like 201702/post_1486965530_jeJNHKWXPMrwRpGBYxczIfTbaqhLnDVO.php
$stored_file_name .= $result['attachment_name'];
// Out puts just example "config.php"
$original .= $result['file_name'];
}
force_download($path . $stored_file_name, NULL);
}
How about that
(imho your foreach loop doesn't make any sense)
public function downloads($id) {
$this->db->where('attachment_id', $id);
$query = $this->db->get('attachments');
if ($query->num_rows() == 0) {
return false;
}
$path = '';
$file = '';
foreach ($query->result_array() as $result) {
$path .= FCPATH . 'uploads/';
// This gives the stored file name
// This is folder 201702
// Looks like 201702/post_1486965530_jeJNHKWXPMrwRpGBYxczIfTbaqhLnDVO.php
$stored_file_name .= $result['attachment_name'];
// Out puts just example "config.php"
$original .= $result['file_name'];
}
force_download("your_filename.txt",file_get_contents($path . $stored_file_name));
}
Simple like that!
force_download(
$filenameWithFileExtension,
file_get_contents($fileToDownload),
mime_content_type($fileToDownload)
);
For this situation use force_download() something like this -
$file_name = "using md5 to convert it on normal text and stroe variable";
$file_path = "It's your file path, where have file in your drive"
$file_path = 'uploads/'.$path;
force_download($file_name, file_get_contents($file_path));
It's simple and working fine. Just need to store your file path and file name store in 2 different variables and pass them in force_download().
Hope it's work.
Good Luck.

how to use this recursive code in codeigniter

db_connection.php
<?php
define('_HOST_NAME', 'localhost');
define('_DATABASE_USER_NAME', 'root');
define('_DATABASE_PASSWORD', '');
define('_DATABASE_NAME', 'test_database');
$dbConnection = new mysqli(_HOST_NAME, _DATABASE_USER_NAME, _DATABASE_PASSWORD, _DATABASE_NAME);
if ($dbConnection->connect_error) {
trigger_error('Connection Failed: ' . $dbConnection->connect_error, E_USER_ERROR);
}
$_GLOBAL['dbConnection'] = $dbConnection;
?>
function.php
<?php
function categoryParentChildTree($parent = 0, $spacing = '', $category_tree_array = '') {
global $dbConnection;
$parent = $dbConnection->real_escape_string($parent);
if (!is_array($category_tree_array))
$category_tree_array = array();
$sqlCategory = "SELECT id,name,parent_id FROM tbl_categories WHERE parent_id = $parent ORDER BY id ASC";
$resCategory=$dbConnection->query($sqlCategory);
if ($resCategory->num_rows > 0) {
while($rowCategories = $resCategory->fetch_assoc()) {
$category_tree_array[] = array("id" => $rowCategories['id'], "name" => $spacing . $rowCategories['name']);
$category_tree_array = categoryParentChildTree($rowCategories['id'], ' '.$spacing . '- ', $category_tree_array);
}
}
return $category_tree_array;
}
?>
index.php
<?php
require_once 'db_connection.php';
require_once 'functions.php';
$categoryList = categoryParentChildTree();
foreach($categoryList as $key => $value){
echo $value['name'].'<br>';
}
?>
can anyone help me to translating this so i can use on codeigniter? recursive function
sorry im new on codeigniter
please help

export attributes to csv from magento

I found a working code that exports the attributes to the browser interface,
like this:
this is the code:'
<?php
chdir(dirname(__FILE__));
require_once 'app/Mage.php';
Mage::app();
umask(0);
$productModel = Mage::getModel('Mage_Catalog_Model_Product');
$categoryModel = Mage::getModel('Mage_Catalog_Model_Category');
$resource = Mage::getModel('core/resource');
$db = $resource->getConnection('core_write');
$attributes = Mage::getSingleton('eav/config')
->getEntityType(Mage_Catalog_Model_Product::ENTITY)->getAttributeCollection();
foreach($attributes as $attribute) {
#if(!$attribute->getIsUserDefined()) continue;
if($attribute->getEntityTypeId() != 4) continue;
if($attribute->getFrontendInput() != 'select' && $attribute->getFrontendInput() != 'multiselect') continue;
echo $attribute->getFrontendLabel(). ' (' . $attribute->getAttributeCode() . ')' . "\n";
foreach ($attribute->getSource()->getAllOptions(false) as $option) {
if(empty($option['label'])) continue;
echo "," . $option['label']."\n";
}
echo "================,=================\n";
}
I would like to export this data to a csv, but I dont know how.
As another question I would also like to export the attribute sets and not only the attributes, is this possible?
Thanks in advance!

Magento $category->getURL() issue for adding rel="alternate"

Attempting to work out a solution to incorporate rel="alternate" tags into a multi-language Mage store. Note: categories and products have customised URL keys for each store
The below code works properly and gives me the correct full URL for a product, in the format of http://www.storename.com/storecode/product.phtml
$url = Mage::getModel('catalog/product')->setStoreId($storeId)->load($product->getId())->getProductUrl();
But the below does not work for categories. It gives the same store code for each URL, instead of the correct store code for each store in the foreach:
$url = Mage::getModel('catalog/category')->setStoreId($storeId)->load($category->getId())->getUrl();
In head.phtml I've so far got:
<?php
$url = '';
$pageType = Mage::app()->getFrontController()->getRequest()->getControllerName();
foreach (Mage::app()->getWebsites() as $website) {
foreach ($website->getGroups() as $group) {
$stores = $group->getStores();
foreach ($stores as $store) {
$storeId = $store->getStoreId();
switch ($pageType) {
case 'product':
$product = Mage::registry('current_product');
$url = Mage::getModel('catalog/product')->setStoreId($storeId)->load($product->getId())->getProductUrl();
break;
case 'category':
$category = Mage::registry('current_category');
/* Below is the code that isn't working properly */
$url = Mage::getModel('catalog/category')->setStoreId($storeId)->load($category->getId())->getUrl();
break;
default:
$url = 'def';
break;
}
echo '<link rel="alternate" href="' . $url . '" hreflang="' . $store->getConfig('general/locale/code') . '"/>' . "\n";
}
}
}
?>
Any advice or help would be appreciated.
Here's the quickest solution I found:
$url = $store->getCurrentUrl(false) . Mage::getModel('catalog/category')->setStoreId($storeId)->load($category->getId())->getUrlKey() . '.html';

Check if a file exist on remote server and save th url path in a magento attribute

Hi im trying to check if a file exist on a remote server and then save the url path to the file in a attribute on the product in magento.
This is how far i have got . i use some code from another function i have that imports images to my magento store. But i have never tested to only get the url path .
<?php
ini_set('display_errors', 1);
require_once('app/Mage.php');
Mage::app();
$_productCollection = Mage::getModel('catalog/product')->getCollection();
$url = "http://www.imonline.se/media42/trailers/199151abe/swinks/hkmedia/";
foreach($_productCollection as $product)
{
//if(file_exists($serverurl))
{
$thispro = Mage::getModel('catalog/product')->load($product->getId());
$attributeValue = $thispro->getFaktaId();
$imgupdate = $thispro->getImgupdate();
if($thispro->getFaktaId()=="" || $thispro->getImage()=="no_selection")
{
if($attributeValue != "")
{
copy($url.$attributeValue );
$im = $attributeValue.".mp4";
if(file_exists)
{
$product->setFilmUrl($url.$im);
$product->save();
}
}
}
}
}
?>
file_exists - Checks whether a file or directory exists
To Check if Remote File Exists using PHP
$url = $url.$im;
$exist = file_exists_remote($url);
if($exist) {
echo 'File Exists';
$product->setFileUrl($url.$im);
$product->save();
} else {
echo 'File not found';
}

Resources