0

Let's say I have a folder called: "test" and inside this folder i have a file called "example.php" Now, the relative path to this file will be: "/test/example.php"

But, is there any way to create a relative path to all files inside a folder. So for example, I need to create a function that will apply to any file inside a specific folder, so instead of creating a list of relative path to every file in this folder. I could just create a single relative path that will point to a folder, and then the function will execute the action to every file inside this folder.

I've tried this: "/test/", I also tried this: "/test" but it doesnt work. Any suggestions will be highly appreciated.

Thanks.

EDIT: To be more specific, I need to create a link in the header that will be displayed differently depends of the current page. So if for example the current page will be home.php, or about.php the link in the header should be: "contacts" But if the current page will be shop.php, or staff.php the link in the header should be: "prices" . So, i created a php include file for the header, and inside i created an if/else statemet:

<?php if($currentpage==$type1) { ?>

    <a href="http://example.com/contacts.php">contacts</a>

  }else if (currentpage==$type2) { ?> 

    <a href="http://example.com/prices.php">prices</a>

<?php } ?>

Then, i put all the type1 pages inside the folder "type1", and all the type2 pages inside the "type2" folder. And then on every page file I added this code:

<?php
     $currentpage = basename($_SERVER['SCRIPT_FILENAME']);
     $type1="/type1/";
     $type2="/type2/";
?>   
  • possible duplicate of [How to read a list of files from a folder using PHP?](http://stackoverflow.com/questions/720751/how-to-read-a-list-of-files-from-a-folder-using-php) – halloei Jul 02 '14 at 14:14
  • Can you show some code? I'm not really sure what you're trying to do. – oliakaoil Jul 02 '14 at 14:16
  • @oliakaoil I've added an edit to the question, so you can take a look, thanks – user3797923 Jul 02 '14 at 15:06
  • @halloei I've tried all the suggestions from your link, but it still doesnt work for me... thank you anyway. – user3797923 Jul 02 '14 at 15:34

2 Answers2

0

scandir("/test/") function will return array of all files from desired folder. Than you can iterate trough array to catch all files (and folders) inside.

Nebojsa Susic
  • 1,220
  • 1
  • 9
  • 12
0

If you need to determine the immediate parent folder of the currently executing script, you could do something like this, given your current environment:

$immediate_parent = '';
$ruri_parts = explode('/' , $_SERVER['REQUEST_URI'] );
array_pop( $ruri_parts );

if( count( $ruri_parts ) )
  $immediate_parent = array_pop( $ruri_parts ); 
oliakaoil
  • 1,615
  • 2
  • 15
  • 36
  • I have 50+ pages, so that will add me 50+ lines of code on each page. And if you add all of them together I will have more than 2500 extra lines of code, I think, there should be a simplest solution to that... thanks – user3797923 Jul 02 '14 at 15:59
  • So you want to determine the value of `$current_type` based on the immediate parent folder of the requested php file? – oliakaoil Jul 02 '14 at 16:04
  • yes, thats right, I mean that will solve the problem. – user3797923 Jul 02 '14 at 16:10