Jump to content

Recommended Posts

I wondered if it was possible to do the following: 

 

when one includes "subdir/file.php" and that file doesn't exist, the file "../folder0/subdir/file.php" is included instead.

 

Is that possible? How? This doesn't look like it can be solved with a .htaccess trick, but I don't see how it can be solved.

 

Thanks in advance!

Link to comment
https://forums.phpfreaks.com/topic/83002-include-on-missing-files/
Share on other sites

You can do an if/else test using file_exists()

Not an option in my case, since I'm using a ton of files that already have include() everywhere. It's an impossible work to go replace them all.

 

I'd really need to make include() do this, somehow. It might be impossible of course...

You could add the paths to the files into your include_path.

 

See set_include_path().

Oh, this is interesting. I looked on php.net for a similar function but didn't find one.

 

The syntax of this command isn't entirely clear though, it should be of this form to do what I described above, right?

$fullpath = dirname(__FILE__);
$newpath = str_replace("folder83","folder0",$fullpath);
set_include_path($fullpath.':'.$newpath);

providing my script is in /var/www/vhosts/domain.com/httpdocs/folder83 and my files in /var/www/vhosts/domain.com/httpdocs/folder0(/subdir)

 

Now, often my files are included as include("./file.php");

How should I configure the path to cover these too? (include ../folder0/file.php instead)

there's a file exists function in php

Of course... but as pointed out above, after it being suggested twice before you, that doesn't help in this case.

The actual inclusion code is given, I can only modify what include() does. And the set_include_path was a good suggestion, I just need to know now how to configure it to include(./../folder0/file.php) when (./file.php) is called but nonexistent.

there's a file exists function in php

Of course... but as pointed out above, after it being suggested twice before you, that doesn't help in this case.

 

alrite mate he was only trying to help no need to snap back at him.

 

anyway why will the file not exist that you are including? can u elaberate a little more

Sorry. I'll elaborate a bit more. I have a lot of subfolders (call them httpdocs/forum1,httpdocs/forum2,...,httpdocs/forum10 for simplicity), which are all running more or less the same software (95% of the files are identical in every folder).

 

This means I have to update 10 boards every time I want to change something. Since they only differ in a few files, I would like to make a "httpdocs/forum0" folder which has the standard files, and then delete all unmodified files from the /forum1,...,/forum10 folders.

 

Then, my intention is that when e.g. include("./includes/include1.php"); is called, the following happens (without using file_exists(), the include statement include("./file.php"); is given):

-> if ./includes/include1.php exists, include it

-> otherwise, include (./../forum0/includes/include1.php) instead.

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.