Recursive directory listing in ownCloud - php

I'm writing code that I'd like to recursively list directories. So far I've written code that lists level 1 directories. Does anyone have any ideas about how I can recurse infinitely?
$res = OC_Files::getDirectoryContent('');
$list = array();
foreach($res as $file) {
if ($file['type'] == 'dir') {
$res1 = OC_Files::getDirectoryContent('/'.$file['name']);
foreach($res1 as $res2) {
if ($res2['type'] == 'file') $list[] = $file['name'].'/'.$res2['name'];
}
} else $list[] = $file['name'];
}
foreach($list as $entry)
echo $entry.'</br>';

Try this:
function traverse_directory($dir) {
$res = OC_Files::getDirectoryContent($dir);
$list = array();
foreach($res as $file) {
if ($file['type'] == 'dir') {
traverse_directory($dir . '/' . $file['name'])
} else {
$list[] = $file['name'];
}
}
foreach($list as $entry) {
echo $entry.'</br>';
}
}
traverse_directory('');

Related

How to find name of the file with particular extesion in array

I have multiples values in array and i want get one of name which in array.
Ex: - $val = array('test.mp4','abc.avi','xyz.3gp','abc.csv');
Now i want get the name of 'abc.csv' in $name.
Please help me to find the value which is ending with extension .csv.
I think this simple code can help
$val = array('test.mp4','abc.avi','xyz.3gp','abc.csv');
$extension = '.csv';
$name = '';
foreach ($val as $value) {
if (strpos($value, $extension) !== false) {
$name[] = $value;
}
}
print_r($name);
Try this
$list = array('test.mp4','abc.avi','xyz.3gp','abc.csv');
$name = '';
foreach ($list as $item) {
if (strrpos($item, '.csv') === 3) {
$name = $item;
}
}
echo $name;
$val = array('test.mp4', 'abc.avi', 'xyz.3gp', 'abc.csv');
foreach ($val as $value) {
$tmp = strrpos($value, ".csv");
echo substr($value, 0, $tmp );
}
foreach ($val as $value) {
$extension = explode(".", $value);
if($extension[1] == "csv"){
echo $value;
}
}
foreach ($val as $item) {
$file_extensions = explode(".", $value);
if(end($file_extensions) == 'csv'){
$allCSVFilse[] = $item;
}
}
print_r($allCSVFiles);

PHP FTP recursive directory listing

I'm trying to make a recursive function to get all the directories and sub directories from my ftp server in an array.
I tried a lot of functions I've found on the web. The one that works best for me is this one:
public function getAllSubDirFiles() {
$dir = array(".");
$a = count($dir);
$i = 0;
$depth = 20;
$b = 0;
while (($a != $b) && ($i < $depth)) {
$i++;
$a = count($dir);
foreach ($dir as $d) {
$ftp_dir = $d . "/";
$newdir = ftp_nlist($this->connectionId, $ftp_dir);
foreach ($newdir as $key => $x) {
if ((strpos($x, ".")) || (strpos($x, ".") === 0)) {
unset($newdir[$key]);
} elseif (!in_array($x, $dir)) {
$dir[] = $x;
}
}
}
$b = count($dir);
}
return $dir ;
}
The problem with this function is it wont allow the directory to have a "." in it's name and every file that is located in the root directory will be considered a directory as well. So I adjusted the function and got this:
public function getAllSubDirFiles($ip, $id, $pw) {
$dir = array(".");
$a = count($dir);
$i = 0;
$depth = 20;
$b =0;
while (($a != $b) && ($i < $depth)) {
$i++;
$a = count($dir);
foreach ($dir as $d) {
$ftp_dir = $d . "/";
$newdir = ftp_nlist($this->connectionId, $ftp_dir);
foreach ($newdir as $key => $x) {
if (!is_dir('ftp://'.$id.':'.$pw.'#'.$ip.'/'.$x)) {
unset($newdir[$key]);
} elseif (!in_array($x, $dir)) {
$dir[] = $x;
}
}
}
$b = count($dir);
}
return $dir ;
}
This works pretty good but and gives the result I want. but it's so slow it's unusable.
I also tried working with ftp_rawlist but it has the same drawback of being horribly slow.
public function getAllSubDirFiles() {
$dir = array(".");
$a = count($dir);
$i = 0;
$depth = 20;
$b = 0;
while (($a != $b) && ($i < $depth)) {
$i++;
$a = count($dir);
foreach ($dir as $d) {
$ftp_dir = $d . "/";
$newdir = $this->getFtp_rawlist('/' . $ftp_dir);
foreach ($newdir as $key => $x) {
$firstChar = substr($newdir[$key][0], 0, 1);
$a = 8;
while ($a < count($newdir[$key])) {
if ($a == 8) {
$fileName = $ftp_dir . '/' . $newdir[$key][$a];
} else {
$fileName = $fileName . ' ' . $newdir[$key][$a];
}
$a++;
}
if ($firstChar != 'd') {
unset($newdir[$key]);
} elseif (!in_array($fileName, $dir)) {
$dir[] = $fileName;
}
}
}
$b = count($dir);
}
return $dir;
}
public function getFtp_rawlist($dir) {
$newArr = array();
$arr = ftp_rawlist($this->connectionId, $dir);
foreach ($arr as $value) {
$stringArr = explode(" ", $value);
$newArr[] = array_values(array_filter($stringArr));
}
return $newArr;
}
I've been stuck on this problem for the last couple of days and I'am getting desperate. If any one has any suggestion please let me know
If your server supports MLSD command and you have PHP 7.2 or newer, you can use ftp_mlsd function:
function ftp_mlsd_recursive($ftp_stream, $directory)
{
$result = [];
$files = ftp_mlsd($ftp_stream, $directory);
if ($files === false)
{
die("Cannot list $directory");
}
foreach ($files as $file)
{
$name = $file["name"];
$filepath = $directory . "/" . $name;
if (($file["type"] == "cdir") || ($file["type"] == "pdir"))
{
// noop
}
else if ($file["type"] == "dir")
{
$result = array_merge($result, ftp_mlsd_recursive($ftp_stream, $filepath));
}
else
{
$result[] = $filepath;
}
}
return $result;
}
If you do not have PHP 7.2, you can try to implement the MLSD command on your own. For a start, see user comment of the ftp_rawlist command:
https://www.php.net/manual/en/function.ftp-rawlist.php#101071
If you cannot use MLSD, you will particularly have problems telling if an entry is a file or folder. While you can use the ftp_size trick, calling ftp_size for each entry can take ages.
But if you need to work against one specific FTP server only, you can use ftp_rawlist to retrieve a file listing in a platform-specific format and parse that.
The following code assumes a common *nix format.
function ftp_nlst_recursive($ftp_stream, $directory)
{
$result = [];
$lines = ftp_rawlist($ftp_stream, $directory);
if ($lines === false)
{
die("Cannot list $directory");
}
foreach ($lines as $line)
{
$tokens = preg_split("/\s+/", $line, 9);
$name = $tokens[8];
$type = $tokens[0][0];
$filepath = $directory . "/" . $name;
if ($type == 'd')
{
$result = array_merge($result, ftp_nlst_recursive($ftp_stream, $filepath));
}
else
{
$result[] = $filepath;
}
}
return $result;
}
For DOS format, see: Get directory structure from FTP using PHP.
I've build an OOP FTP Client library that's can help you on this a lot, using just this code you can retrieve a list of only the directories with addition useful information like (chmod, last modified time, size ...).
The code :
// Connection
$connection = new FtpConnection("localhost", "foo", "12345");
$connection->open();
// FtpConfig
$config = new FtpConfig($connection);
$config->setPassive(true);
$client = new FtpClient($connection);
$allFolders =
// directory, recursive, filter
$client->listDirectoryDetails('/', true, FtpClient::DIR_TYPE);
// Do whatever you want with the folders
This code a variation of Martin Prikryl one. It is slower but do not have any failures with whitespaces. Use this code only if you have any problems with the code above.
function ftp_list_files_recursive($ftp_stream, $path){
$lines = ftp_nlist($ftp_stream, $path);
$result = array();
foreach ($lines as $line) {
if (ftp_size($ftp_stream, $line) == -1) {
$result = array_merge($result, ftp_list_files_recursive($ftp_stream, $line));
}
else{
$result[] = $line;
}
}
return $result;
}

Remove duplicates in foreach

I'm working with my code which displays all the folders and subfolders in my directory.
I have a simple problem.. some result are duplicates or repeated and I don't want to display it.
How can i do this?
<?php
$dir = 'apps/';
$result = array();
if (is_dir($dir)) {
$iterator = new RecursiveDirectoryIterator($dir);
foreach (new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::CHILD_FIRST) as $file) {
if (!$file->isFile()) {
$result = $file->getPath()."<br>";
echo $result;
}
}
}
?>
Try this
<?php
$dir = 'apps/';
$result = array();
if (is_dir($dir)) {
$iterator = new RecursiveDirectoryIterator($dir);
foreach (new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::CHILD_FIRST) as $file) {
if (!$file->isFile()) {
$path = $file->getPath();
if(in_array($path, $result)) {
continue ;
}
$result = $path."<br>";
echo $result;
}
}
}
?>
You can use hash array for checking if path already in list
<?php
$dir = 'apps/';
$result = array();
$hash=array();
if (is_dir($dir)) {
$iterator = new RecursiveDirectoryIterator($dir);
foreach (new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::CHILD_FIRST) as $file) {
if (!$file->isFile()) {
$path = $file->getPath();
if(isset($hash[$path])) {
continue ;
}
$hash[$path]=1;
$result[] = $path;
echo $path."<br>";
}
}
}
?>
use array_unique()
<?php
$dir = 'apps/';
$result = array();
if(is_dir($dir)){
$iterator = new RecursiveDirectoryIterator($dir);
foreach(new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::CHILD_FIRST) as $file){
if(!$file->isFile()){
$result[] = $file->getPath();
}
}
$uniqueResult = array_unique($result);
if(!empty($uniqueResult)){
foreach($uniqueResult as $v){ // don't use 'for' use 'foreach' here.
echo $v.'<br>';
}
}
}

How to Update Categories that Already Exists in Magento Database Using Custom Script

I have Already Created The Categories in Magento by using the custom script,
but issue is when I again run the same URL my script creates another set of same category. as i want to update the existing category and insert the new categories if exist in the csv.
My csv structure is:
Category 1,Description,Category 2,Description,Category 3,Description,Category 4,Description
1,COSTUMES,1,ADULTS,1,MENS
1,COSTUMES,1,ADULTS,1,MENS,2,ASIAN
1,COSTUMES,1,ADULTS,1,MENS,3,BIKER
1,COSTUMES,1,ADULTS,1,MENS,1,CAPES & ROBES
1,COSTUMES,1,ADULTS,1,MENS,5,CAVE PEOPLE
Thanking All of You in Advance For Your Answer
Below is my code to create multi level categories:
define('MAGENTO', realpath(dirname(__FILE__)));
require_once MAGENTO . '/app/Mage.php';
umask(0);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$count = 0;
$file = fopen('export/web categories.csv', 'r');
function odd($var)
{
$odd = array();
foreach ($var as $k => $v) {
if ($k % 2 !== 0) {
$odd[$k] = $v;
}
}
return $odd;
}
function even($var)
{
$even = array();
foreach ($var as $k => $v) {
if ($k % 2 == 0) {
$even[$k] = $v;
}
}
return $even;
}
function strcount($str,$sy){
$ch= explode($sy, $str);
return count($ch);
}
$pos=0;
$row_config= array();
$test=array();
$catgoryID=array();
$parID = 1;
while (($line = fgetcsv($file)) !== FALSE) {
if(!in_array($valtest,$row_config)){
if($count == 0){
$count++;
continue;
}
$count++;
$filterd_data=array_filter($line);
$odd_cell_data=odd($filterd_data);
$even_cell_data=even($filterd_data);
$config=array();
$string='';
$intialID=$even_cell_data[0];
foreach($odd_cell_data as $key=>$val){
if(!in_array($val,$config)){
$config[] = $val;
$data['general']['name'] =$val;
$data['general']['meta_title'] = "";
$data['general']['meta_description'] = "";
$data['general']['is_active'] = "";
$data['general']['url_key'] = "";
$data['general']['is_anchor'] = 0;
$data['general']['position'] =1 ;
$storeId = 0;
$string .=$val.'~';
if(!array_key_exists($string, $row_config)){
$catID=createCategory($data, $storeId);
$catgoryID[$string]=$catID;
$row_config[$string]=$parID;
} else {
$parID =$row_config[$string] ;
$row_config[$string]=$row_config[$string];
}
if( strcount($string,'~')==2){
$parID=1;
}
$int[$string]=$parID;
assignCat($catgoryID[$string],$parID);
$parID = $catgoryID[$string];
sleep(0.5);
unset($data);
}
}
}
}
//This Function is used for create each category
function createCategory($data, $storeId) {
$category = Mage::getModel('catalog/category');
$category->setStoreId($storeId);
if (is_array($data)) {
$category->addData($data['general']);
if (!$category->getId()) {
$parentId = $data['category']['parent'];
if (!$parentId) {
if ($storeId) {
$parentId = Mage::app()->getStore($storeId)->getRootCategoryId();
} else {
$parentId = Mage_Catalog_Model_Category::TREE_ROOT_ID;
}
}
$parentCategory = Mage::getModel('catalog/category')->load($parentId);
$category->setPath($parentCategory->getPath());
} if ($useDefaults = $data['use_default']) {
foreach ($useDefaults as $attributeCode) {
$category->setData($attributeCode, null);
}
}
$category->setAttributeSetId($category->getDefaultAttributeSetId());
if (isset($data['category_products']) && !$category->getProductsReadonly()) {
$products = array();
parse_str($data['category_products'], $products);
$category->setPostedProducts($products);
} try {
$category->save();
$category = Mage::getModel('catalog/category')->load($category->getId());
$category->setPosition($data['general']['position']);
$category->addData($data['general']);
$category->save();
echo "Suceeded <br /> ";
} catch (Exception $e) {
echo "Failed <br />";
}
}
return $category->getId();
}
//This Function is used for moving category under respective parent
function assignCat($id, $parent){
$category = Mage::getModel( 'catalog/category' )->load($id);
Mage::unregister('category');
Mage::unregister('current_category');
Mage::register('category', $category);
Mage::register('current_category', $category);
$category->move($parent);
return;
}

php echo results in alphabetical order

This code below will echo the keyword in each file, Is it possible to get the results (Keyword from each file) to display in alphabetical order.
<?php
$files = (glob('{beauty,careers,education,entertainment,pets}/*.php', GLOB_BRACE));
$selection = $files;
$files = array();
$keywords = $matches[1];
foreach ($selection as $file) {
if (basename($file) == 'index.php') continue;
if (basename($file) == 'error_log') continue;
$files[] = $file;
}
foreach($files as $file) {
$title = str_replace('-', ' ', pathinfo($file, PATHINFO_FILENAME));
$content = file_get_contents($file);
if (!$content) {
echo "error reading file $file<br>";
}
else {
preg_match("/keywords = \"(.*?)\"/i", $content, $matches);
$keywords = $matches[1];
}
$results .= '<li>'.$keywords.'</li>';
}
?>
<?=$results?>
Use sort() to sort the keyword array:
$keywords = array();
// ...
$keywords[] = $matches[1];
sort($keywords);
use sort() on $keywords. There's some spelling mistakes and unused varaibles. You need to not build your $results HTML until all the files are processes, so move that $results = '' out of the foreach loop that processes the files, then sort, then foreach the keywords and build up $results.
<?php
$selection = (glob('{beauty,careers,education,entertainment,pets}/*.php', GLOB_BRACE));
$files = array();
// $keywords = $matches[1];
foreach ($selection as $file) {
if (basename($file) == 'index.php') continue;
if (basename($file) == 'error_log') continue;
$files[] = $file;
}
foreach($files as $file) {
//$title = str_replace('-', ' ', pathinfo($file, PATHINFO_FILENAME));
$content = file_get_contents($file);
if (!$content) {
echo "error reading file $file<br>";
}
else {
preg_match("/keywords = \"(.*?)\"/i", $content, $matches);
$keywords[] = $matches[1];
}
}
$results = '';
sort($keywords); //comment this out to see the effects of the sort()
foreach($keywords as $_k) {
$results .= '<li>'.$_k.'</li>';
}
?>
<?=$results?>

Categories