Jump to content

[H E L P] - Need help with php-code


ztealmax

Recommended Posts

Hello all having a bit of problem with this code think anyone can help me?

 

<?php
  function in_dir ($file, $in_dir)
  {
      $dir    = realpath ($file);
      $in_dir = realpath ($in_dir);

      if (!is_dir ($file)) {
          $dir = dirname ($file);
      }

      do {
          if ($dir === $in_dir) {
              $is_in_dir = TRUE;
              break;
          }
      } while ($dir !== ($dir = dirname ($dir)));

      return (bool) @$is_in_dir;
  }
  $dir = $path;
echo $dir;
  $dh  = opendir($dir);
  while (false !== ($filename = readdir($dh))) 
{
   $pattern = '/\.news$/';
   if(preg_match($pattern,$filename))
   {
      /*    You can proceed because the file has the extension .news       */
   $filename = ereg_replace($dir, "", "$filename");
  $filename = ereg_replace(".news", "", "$filename");
echo "<a href='?content=read&contents=".$dir."".$filename.".news'>" . $filename . "</a><br>";
   }
}

?>

 

the problem im having that im getting this error message

 

readdir(): supplied argument is not a valid Directory resource ni news.php

 

if you need any more information please just ask, if you want all files just ask i will supply them

 

you can add me to msn if you want

martin@ztealmax.se

 

//Cheers

Martin "Ztealmax"

Link to comment
Share on other sites

think in this code im setting path to what i press on

<?php

$path = "news"; // path to the dir you want to scan

foreach(glob("$path/*", GLOB_ONLYDIR) as $catdir) {
// catogories start
$catogories = ("".$catdir.".description");
$catfile = fopen($catogories, 'r') /*or die('Could not open file!')*/;
$catdata = fread($catfile, filesize($catogories)) /*or die('Could not read file!')*/;
fclose($catfile); 
$catdir = ereg_replace("news/", "", "$catdir");
echo "<a href='".$_SERVER['PHP_SELF']."?content=news'><b>".$catdir."</b></a>  ".$catdata."<br>";

}

?>

if im being unclear please say so my english isnt the best ;)

 

i can send you the files if you want?

 

demo site here: http://demo.premierguild.com

 

Sincerely

Martin

Link to comment
Share on other sites

Hi

 

Suspect it might be where it thinks "news" is. Does the directory the script lives in have a subdirectory called "news"?

 

All the best

 

Keith

 

yea it has a directory called news like this:

 

News/

and in news i have subdirectorys called for example

News/Info

 

all php files are in the root

/sincerely

Martin

Link to comment
Share on other sites

Hi

 

What was the value of $news when echoed out? If necessary put some * either side to check if it is really empty or spaces.

 

All the best

 

Keith

 

the echo was empty didnt show up anything

 

think the fault is in this code

 while (false !== ($filename = readdir($dh)))[/Code]

Warning: readdir(): supplied argument is not a valid Directory resource in news.php on line 26

Link to comment
Share on other sites

news_catogories.php

<?php

$path = "news"; // path to the dir you want to scan

foreach(glob("$path/*", GLOB_ONLYDIR) as $catdir) {
// catogories start
$catogories = ("".$catdir.".description");
$catfile = fopen($catogories, 'r') /*or die('Could not open file!')*/;
$catdata = fread($catfile, filesize($catogories)) /*or die('Could not read file!')*/;
fclose($catfile); 
$catdir = ereg_replace("news/", "", "$catdir");
echo "<a href='".$_SERVER['PHP_SELF']."?content=news'><b>".$catdir."</b></a>  ".$catdata."<br>";

}

?>

news.php

<?php
  function in_dir ($file, $in_dir)
  {
      $dir    = realpath ($file);
      $in_dir = realpath ($in_dir);

      if (!is_dir ($file)) {
          $dir = dirname ($file);
      }

      do {
          if ($dir === $in_dir) {
              $is_in_dir = TRUE;
              break;
          }
      } while ($dir !== ($dir = dirname ($dir)));

      return (bool) @$is_in_dir;
  }
  
  $dir = $path;
echo $dir;
echo $path;
  $dh  = opendir($dir);
  
  while (false !== ($filename = readdir($dh))) 
{
   $pattern = '/\.news$/';
   if(preg_match($pattern,$filename))
   {
      /*    You can proceed because the file has the extension .news       */
   $filename = ereg_replace($dir, "", "$filename");
  $filename = ereg_replace(".news", "", "$filename");
echo "<a href='?content=read&contents=".$dir."".$filename.".news'>" . $filename . "</a><br>";
   }
}

?>

 

this is my index.php

<?php
// language start
$language = ("config/language.cfg");
$lanfile = fopen($language, 'r') /*or die('Could not open file!')*/;
$landata = fread($lanfile, filesize($language)) /*or die('Could not read file!')*/;
fclose($lanfile); 
include("languages/$landata.php"); 
// language end
?>

<?php
// sitename start
$sitename = ("config/sitename.cfg");
$sitenamefile = fopen($sitename, 'r') /*or die('Could not open file!')*/;
$sitenamedata = fread($sitenamefile, filesize($sitename)) /*or die('Could not read file!')*/;
fclose($sitenamefile); 
define('sitename',$sitenamedata."");
// sitename end
?>
<?php
// welcome start
$welcome = ("config/welcome.cfg");
$welcomefile = fopen($welcome, 'r') /*or die('Could not open file!')*/;
$welcomedata = fread($welcomefile, filesize($welcome)) /*or die('Could not read file!')*/;
fclose($welcomefile); 
define('welcome',$welcomedata."");
// welcome end
?>
<?php
// sitetag start
$sitetag = ("config/sitetag.cfg");
$sitetagfile = fopen($sitetag, 'r') /*or die('Could not open file!')*/;
$sitetagdata = fread($sitetagfile, filesize($sitetag)) /*or die('Could not read file!')*/;
fclose($sitetagfile); 
define('sitetag',$sitetagdata."");
// sitetag end
?>
<?php
// disclaimer start
$disclaimer = ("config/disclaimer.cfg");
$disclaimerfile = fopen($disclaimer, 'r') /*or die('Could not open file!')*/;
$disclaimerdata = fread($disclaimerfile, filesize($disclaimer)) /*or die('Could not read file!')*/;
fclose($disclaimerfile); 
define('disclaimer',$disclaimerdata."");
// disclaimer end
?>
<?php
// theme start
$theme = ("config/theme.cfg");
$themefile = fopen($theme, 'r') /*or die('Could not open file!')*/;
$themedata = fread($themefile, filesize($theme)) /*or die('Could not read file!')*/;
fclose($themefile); 
$themes = ("themes/");
define('theme',$themes.$themedata."/");
@include_once("$themes/$themedata/theme.php"); 
// theme end
?>

<?php
$url = $_SERVER['SERVER_NAME'];
define("url", $_SERVER['SERVER_NAME']);
?>


<? echo $header ?>
<? include("menus/mainmenu.php");?>
<?php
if(isset($_GET['content'])){
include($_GET['content'].".php");
} else {
include($_GET['content']."");
echo "".$welcomedata." ";
}
?>

<? echo $footer ?>




 

Link to comment
Share on other sites

hmm where would i turn that on? ;)

 

damn im such a noob at this

 

/Martin

 

path thats assigned in news_catogories.php is "news/"

but when i click a link within "news/" it should become the new path for "news.php"

for example i click info so the new path should now be "news/info"

let me know if im bad at explaining!

 

 

//martin

Link to comment
Share on other sites

Hi

 

Not sure if I am missing something, but news_catogories.php doesn't seem to link to news.php. If you do go to news.php then there is nothing in news.php to set the variable $path. An exception to this would be if register_globals was turned on, where you could set $path in the query string. This used to be on by default and is now off by default as it makes it rather too easy to write insecure code.

 

Or do you have an overall script that includes news.php, news_categories.php, etc?

 

All the best

 

Keith

Link to comment
Share on other sites

ok i cant find whats wrong or where to change it :(

 

demo site is: demo.premierguild.com

ftp is the same as a messaged you about

register globals is on as far as iknow!

 

what this code should do is somehow get the real path of the link i clicked, does it make any sence?

 

 

//Cheers

Martin

Link to comment
Share on other sites

Hi

 

Right, looking at that you have index.php.

 

That takes a parameter of the other script to include. When you click on one of your news menu it passes ?content=news_catagories, causing it to include news_categories.php . This works fine.

 

However when you get a list of the various news categories it passes content=news, causing it to include news.php . However neither index.php nor news.php set the value of $path (it is set in news_categories.php, which isn't being included this time round). Hence no argument for readdir.

 

You need to pass across a value for $path. How you do this I am not sure, possibly an extra parameter (possibly changing news_categories.php with the line echo "<a href='".$_SERVER['PHP_SELF']."?content=news&path=somepath'><b>".$catdir."</b></a>  ".$catdata."<br>";.

 

However this is pretty insecure (so is passing over the script name to include in content=news). I would make very certain you vaidate the input before doing the includes.

 

All the best

 

Keith

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.