i want to correct my recursive function in php - php

I have a recursive function
first of all one array of page URLs are passed to this function
then for each link and finds links in those pages, if those are pdf links then saved to $li array, if not then saved to $next array.
actually, in some pages, same links repeating.
so every time I want to pass $next array with new links only.but now $next array is storing all links and loop is not ending.please help
here is my code
<?php
global $next;
$next == array();
global $counter;
$counter = 2;
global $p;
$next is an array of page links got from a page
next_iter = get_html1($next);
function get_html1($next){
global $li;
global $all_links;
global $p;
$p++;
echo "count is" . $p . '<br>';
error_reporting(E_ERROR | E_PARSE);
global $URL;
global $counter;
$next = array_unique($next);
print_r($next);
foreach ($next as $nw) {
$page = '';
if ((strpos($nw, $URL) !== false ) && (strpos($nw, 'http://') !== false || strpos($nw, 'https://') !== false)) {
$page = $nw;
$html1 = file_get_html($page);
} else if (strpos($nw, $URL) === false && strpos($nw, 'http://') === false && strpos($nw, 'https://') === false) {
$page = $URL . $nw;
$html1 = file_get_html($page);
}
if ($html1 !== false) {
foreach ($html1->find('a') as $links) {
if (!in_array($links->href, $all_links)) {
if ($links->href != '#' && $links->href != NULL && $links->href != '/' && strpos($links->href, 'javascript') === false) {
$a = strip_tags($links->plaintext);
$a = preg_replace("/\s| /", '', $a);
$a = trim($a);
if ((preg_match('/.*\.pdf$/i', $links->href)) || (strcmp($a, 'Download') == 0) || stripos($a, 'Download') !== false) {
// echo $links->href;
if ((strpos($links->href, 'http://') !== false || strpos($links->href, 'https://') !== false)) {
$lm = $links->href;
if (!in_array($lm, $li)) {
write_to_folder($lm);
$li[] = $lm;
}
$li[] = $links->href;
} else if ((strpos($links->href, 'http://') === false && strpos($links->href, 'https://') === false)) {
// echo $URL . $links->href . '<br>';
$lm = $URL . $links->href;
if (!in_array($lm, $li)) {
write_to_folder($lm);
$li[] = $lm;
}
}
} else {
if (strpos($links->href, 'mailto:') === false && strpos($links->href, 'tel:') === false && strpos($links->href, '.zip') === false) {
if (!in_array($links->href, $next1)) {
$next[] = $links->href;
}
}
}
}
}
}
}
}
$next = array_unique($next);
echo "<br>next array";
if (!empty($next)) {
if ($p <= $counter) {
$recursiv = get_html1($next);
} else {
echo "SUCCESSFULLY EXECUTED";
exit;
}
} else {
echo "SUCCESSFULLY EXECUTED";
exit;
}
}
?>

Related

copy a php file to every directory

I've a simple problem of copying a a php folder to some directories, bu the problem is I can't the solution for that, the idea is that I've an Online Manga Viewer script, and what I want to do is I want to add comments page to every chapter, the I dea that I came with, is, I create a separate comments page file and once a new chapter added the the comments file will be copied to the folder of the chapter :
Description Image:
http://i.stack.imgur.com/4wYE0.png
What I to know is how can I do it knowing that I will use Disqus commenting System.
Functions used in the script:
function omv_get_mangas() {
$mangas = array();
$dirname = "mangas/";
$dir = #opendir($dirname);
if ($dir) {
while (($file = #readdir($dir)) !== false) {
if (is_dir($dirname . $file . '/') && ($file != ".") && ($file != "..")) {
$mangas[] = $file;
}
}
#closedir($dir);
}
sort($mangas);
return $mangas;
}
function omv_get_chapters($manga) {
global $omv_chapters_sorting;
$chapters = array();
$chapters_id = array();
$dirname = "mangas/$manga/";
$dir = #opendir($dirname);
if ($dir) {
while (($file = #readdir($dir)) !== false) {
if (is_dir($dirname . $file . '/') && ($file != ".") && ($file != "..")) {
$chapter = array();
$chapter["folder"] = $file;
$pos = strpos($file, '-');
if ($pos === false) {
$chapter["number"] = $file;
} else {
$chapter["number"] = trim(substr($file, 0, $pos - 1));
$chapter["title"] = trim(substr($file, $pos + 1));
}
$chapters_id[] = $chapter["number"];
$chapters[] = $chapter;
}
}
#closedir($dir);
}
array_multisort($chapters_id, $omv_chapters_sorting, $chapters);
return $chapters;
}
function omv_get_chapter_index($chapters, $chapter_number) {
$i = 0;
while (($i < count($chapters)) && ($chapters[$i]["number"] != $chapter_number)) $i++;
return ($i < count($chapters)) ? $i : -1;
}
function omv_get_pages($manga, $chapter) {
global $omv_img_types;
$pages = array();
$dirname = "mangas/$manga/$chapter/";
$dir = #opendir($dirname);
if ($dir) {
while (($file = #readdir($dir)) !== false) {
if (!is_dir($dirname . $file . '/')) {
$file_extension = strtolower(substr($file, strrpos($file, ".") + 1));
if (in_array($file_extension, $omv_img_types)) {
$pages[] = $file;
}
}
}
#closedir($dir);
}
sort($pages);
return $pages;
}
/*function add_chapter_comment($dirname){
$filename = $dirname.'comments.php';
if (file_exists($filename)) {
} else {
copy('comments.php', .$dirname.'comments.php');
}
}*/
function omv_get_previous_page($manga_e, $chapter_number_e, $current_page, $previous_chapter) {
if ($current_page > 1) {
return $manga_e . '/' . $chapter_number_e . '/' . ($current_page - 1);
} else if ($previous_chapter) {
$pages = omv_get_pages(omv_decode($manga_e), $previous_chapter["folder"]);
return $manga_e . '/' . omv_encode($previous_chapter["number"]) . '/' . count($pages);
} else {
return null;
}
}
function omv_get_next_page($manga_e, $chapter_number_e, $current_page, $nb_pages, $next_chapter) {
if ($current_page < $nb_pages) {
return $manga_e . '/' . $chapter_number_e . '/' . ($current_page + 1);
} else if ($next_chapter) {
return $manga_e . '/' . omv_encode($next_chapter["number"]);
} else {
return null;
}
}
function omv_get_image_size($img) {
global $omv_img_resize, $omv_preferred_width;
$size = array();
$imginfo = getimagesize($img);
$size["width"] = intval($imginfo[0]);
$size["height"] = intval($imginfo[1]);
if ($omv_img_resize) {
if ($size["width"] > $omv_preferred_width) {
$size["height"] = intval($size["height"] * ($omv_preferred_width / $size["width"]));
$size["width"] = $omv_preferred_width;
}
}
return $size;
}
And thanks for all of you!
Include the following line in all of your pages in a small php statement, if it covers two folder paths, use this. Which I think in your case it does.
<?php
include('../../header.php');
?>
And then save this in the main root directory. Which in your diagram is called "Main Folder"

Hide group if user is not logged in and all items in group require login

I have a list of items, sorted into groups. When the user is not logged in, I print only the items that don't require login.
$previous_group = '';
foreach ($arr as $item) {
if($previous_group != $item['group']) {
// Add dividers
if($previous_group != '') echo '</ul>';
echo '<h3>'.$item['group'].'</h3>';
echo '<ul>';
}
$previous_group = $item['group'];
if($item['login_required'] !== 'true' || ($item['login_required'] == 'true' && $isLoggedIn != false)) {
echo '<li>'.$item['title'].'</li>';
}
}
echo '</ul>';
PhpFiddle
How can I hide the header for a group that doesn't have any items because the user isn't logged in? For example, the "food" category in the PhpFiddle example.
I could just go through the array twice, but is there a more efficient way to do it?
try
$previous_group = '';
foreach ($arr as $item) {
if($previous_group != $item['group'] && ($item['login_required'] == 'false' || $isLoggedIn == 'true')) {
// Add dividers
if($previous_group != ''){echo '</ul>';}
echo '<h3>'.$item['group'].'</h3>';
echo '<ul>';
}
$previous_group = $item['group'];
if($item['login_required'] !== 'true' || ($item['login_required'] == 'true' && $isLoggedIn != false)) {
echo '<li>'.$item['title'].'</li>';
}
}
echo '</ul>';
If you want just one iteration:
$previous_group = '';
$group_content = '';
$last_key = end(array_keys($arr));
foreach ($arr as $key => $item) {
if (!$previous_group) $previous_group = $item['group'];
if ($key === $last_key && $previous_group == $item['group'] && ($item['login_required'] !== 'true' || ($item['login_required'] == 'true' && $isLoggedIn != false))) $group_content .= '<li>'.$item['title'].'</li>';
if ($group_content) {
echo '<h3>'.$item['group'].'</h3>';
echo '<ul>';
echo $group_content;
echo '</ul>';
}
$previous_group = $item['group'];
$group_content = '';
}
if($item['login_required'] !== 'true' || ($item['login_required'] == 'true' && $isLoggedIn != false)) {
$group_content .= '<li>'.$item['title'].'</li>';
}
}
However, doing more than one iteration will give you cleaner code.

Short display images by date newer to older in PHP

I'm using the following function to read a directory and show images as a gallery, it works really well, but I need to display images ordering by date from newer to older.
Knows anyone how to do it using this method?
function getPictures() {
global $page, $per_page, $has_previous, $has_next;
if ( $handle = opendir("saved/2013") ) {
$lightbox = rand();
$count = 0;
$skip = $page * $per_page;
if ( $skip != 0 )
$has_previous = true;
while ( $count < $skip && ($file = readdir($handle)) !== false ) {
if ( !is_dir($file) && ($type = getPictureType($file)) != '' )
$count++;
}
$count = 0;
while ( $count < $per_page && ($file = readdir($handle)) !== false ) {
if ( !is_dir($file) && ($type = getPictureType($file)) != '' ) {
if ( ! is_dir('saved/2013') ) {
mkdir('saved/2013');
}
$strFileName = "saved/2013/".$file;
echo '<div id="imagen-t">';
echo '<p>' .date( "D d M Y g:i A", filemtime($strFileName)) . "</p>";
echo '<p><img src=saved/2013/'.$file.' alt="" width="220" /></p>';
echo '</div>';
$count++;
}
}
while ( ($file = readdir($handle)) !== false ) {
if ( !is_dir($file) && ($type = getPictureType($file)) != '' ) {
$has_next = true;
break;
}
}
}
}
Here is the result:
http://www.espigoplatja.com/whale/galeria/
Salutations,
Gonzalo
Have you looked at USort: http://php.net/manual/en/function.usort.php
It allows you to sort an array by values with user defined comparison function.
An example
<?php
function my_sort($a,$b)
{
if ($a==$b) return 0;
return ($a<$b)?-1:1;
}
$a=array(4,2,8,6);
usort($a,"my_sort");
$arrlength=count($a);
for($x=0;$x<$arrlength;$x++)
{
echo $a[$x];
echo "<br>";
}
?>
By the way I try implementing the sample you provide and it displays the same date for all imagenes:
# SETTINGS
$per_page = 6;
$page = $_GET['page'];
$has_previous = false;
$has_next = false;
function getPictures() {
global $page, $per_page, $has_previous, $has_next;
if ( $handle = opendir("saved/2013") ) {
$lightbox = rand();
$count = 0;
$skip = $page * $per_page;
if ( $skip != 0 )
$has_previous = true;
while ( $count < $skip && ($file = readdir($handle)) !== false ) {
if ( !is_dir($file) && ($type = getPictureType($file)) != '' )
$count++;
}
function my_sort($a,$b)
{
if ($a==$b) return 0;
return ($a<$b)?-1:1;
}
$count = 0;
while ( $count < $per_page && ($file = readdir($handle)) !== false ) {
if ( !is_dir($file) && ($type = getPictureType($file)) != '' ) {
if ( ! is_dir('saved/2013') ) {
mkdir('saved/2013');
}
$a=array($file);
usort($a,"my_sort");
//$strFileName = "saved/2013/".$file;
$strFileName = "saved/2013/".$a[$x];
echo '<div id="imagen-t">';
//echo '<p>' .date( "D d M Y g:i A", filemtime($strFileName)) . "</p>";
echo '<p>' .date( "D d M Y g:i A", filemtime($strFileName)) . "</p>";
echo '<p><img src=saved/2013/'.$file.' alt="" width="220" /></p>';
echo '</div>';
$count++;
}
}
while ( ($file = readdir($handle)) !== false ) {
if ( !is_dir($file) && ($type = getPictureType($file)) != '' ) {
$has_next = true;
break;
}
}
}
}

giving error msg at wrong condition

<?php
$dir = 'dir';
$exclude = array('.','..','.htaccess');
$q = (isset($_GET['q']))? strtolower($_GET['q']) : '';
$res = opendir($dir);
if (strlen($q) < 3) {
echo "Search must be longer than 3 characters.<br><br>";
} else {
if (!( ($q == "mp3") || ($q == "wav") || ($q == ""))){
while(false!== ($file = readdir($res))) {
if(strpos(strtolower($file),$q)!== false &&!in_array($file,$exclude)) {
$nicefile = str_replace(".mp3", "", "$file");
$info = pathinfo($file);
if (($info["extension"] == "mp3") || ($info["extension"] == "wav")) {
echo "<a href='http://domainname/filename.php?name=$file'>$nicefile</a>";
echo "<br>";
}elseif(!isset($errorMsg)){
$errorMsg = "ERROR: File not found.<br><br>";
}else{}
}elseif(!isset($errorMsge)){
$errorMsge = "ERROR: File not found.<br><br>";
}else{}
}
if (isset($errorMsge)){echo $errorMsge;}
else{}
if (isset($errorMsg)){echo $errorMsg;}
else{}
}else{echo"ERROR: File not found.<br><br>";}
}
closedir($res);
?>
this is like a search script and the problem is #
if(strpos(strtolower($file),$q)!== false &&!in_array($file,$exclude))
{
it shows the error msg whether there is results or file named like search query or not.
any idea how to fix that?

Displaying Sub Folder Name From A folder using php

I want to display the subfolder names in a folder contained .
How can i do this using php code?
This is the code:
function read_folder_directory($dir)
{
$listDir = array();
if($handler = opendir($dir)) {
while (($sub = readdir($handler)) !== FALSE) {
if ($sub != "." && $sub != ".." && $sub != "Thumb.db" && $sub != "Thumbs.db") {
if(is_file($dir."/".$sub)) {
$listDir[] = $sub;
}elseif(is_dir($dir."/".$sub)){
$listDir[$sub] = $this->read_folder_directory($dir."/".$sub);
}
}
}
closedir($handler);
}
return $listDir;
}
When iam using this i get all file names also.I need only folder names.
Remove the is_file part
function read_folder_directory($dir)
{
$listDir = array();
if($handler = opendir($dir)) {
while (($sub = readdir($handler)) !== FALSE) {
if ($sub != "." && $sub != ".." && $sub != "Thumb.db" && $sub != "Thumbs.db") {
if(is_dir($dir."/".$sub)){
$listDir[$sub] = $this->read_folder_directory($dir."/".$sub);
}
}
}
closedir($handler);
}
return $listDir;
}

Categories