Asked  7 Months ago    Answers:  5   Viewed   35 times

This bug has depleted my rational thinking power over the past 64 minutes and I really don't know what I should do.

When i use the following 3 lines in an included file:

var_dump(file_exists('G:/xampp/htdocs/myproject/public_html/includes/htmlPurifierAndMarkdown.php')); //retuurns true
var_dump(__FILE__);
include '../htmlPurifierAndMarkdown.php'; //gives error

i get the following output :

boolean true

string 'G:xampphtdocsmyprojectpublic_htmlincludesclassesgenral.class.php' (length=71)


( ! ) Warning: include(../htmlPurifierAndMarkdown.php) [function.include]: failed to open stream: No such file or directory in G:xampphtdocsmyprojectpublic_htmlincludesclassesgenral.class.php on line 4
Call Stack
#   Time    Memory  Function    Location
1   0.0005  345328  {main}( )   ..add.php:0
2   0.0036  382408  include( 'G:xampphtdocsmyprojectpublic_htmlincludesclassesevents.class.php' )    ..add.php:28
3   0.0041  398024  include( 'G:xampphtdocsmyprojectpublic_htmlincludesclassesstandardpost.class.php' )  ..events.class.php:2
4   0.0047  413928  include( 'G:xampphtdocsmyprojectpublic_htmlincludesclassesgenral.class.php' )    ..standardpost.class.php:2

In a nutshell:

  1. The first line tells that the file is not a part of my imagination and it actually exists.
  2. The second line tells the location of my calling script, on examining the paths closely, you'll notice that the file I'm trying to include resides in the parent directory
  3. Include function says that file dosent exist

so am i just imagining stuff or is this some serious php bug?

Update

when i access genral.class.php manually, no error is generated. do special rules apply when including from within an included file?

Update 2

When i set the include path to '../includes/htmlPurifierAndMarkdown.php' , which is the path relative to the main calling script, which in turn includes a script which includes genral.class.php it works, but since this new include is relative to only one of the many calling scripts, it breaks down for the others. Do special rules apply when including a file from within an included file??

 Answers

38

The __FILE__ constant returns the full path and filename of the file containing that line. It is possible for this file to be located inside a directory other than the current working directory. In your particular example you can use this:

include dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'htmlPurifierAndMarkdown.php';

An example of what might be going on is as follows:

  1. The file being executed is /example.com/public_html/index.php
  2. Current working directory is /example.com/public_html/
  3. The index file includes /example.com/public_html/includes/classes/genral.class.php
  4. The above file attempts to include ../htmlPurifierAndMarkdown.php
  5. The .. is translated relative to current working directory and not relative to the included file. Thus you end up with /example.com/htmlPurifierAndMarkdown.php and a nasty error message.
Saturday, May 29, 2021
 
insomiac
answered 7 Months ago
72

Common practice is to have a "common.php" or "includes.php" file that includes the include/include_once calls (for the sake of simplicity). e.g.

  • root
  • [lib]
    • a.php
    • b.php
    • includes.php
  • index.php

Then includes.php contains:

<?php
  include_once('a.php');
  include_once('b.php');
?>

Then in any script it's a matter of including the includes.php file.

However, to answer your original question, you can only include one file at a time, per call. You can use something like opendir and readdir to iterate over all files in a specific directory and include them as found (automated so-to-speak) or write out each include yourself based on the files you're creating.

Also, all setting the include path does is set a directory to look in when an include call is made. It's not a directory where the files should automatically be loaded (which is the impression I get from your post).

Wednesday, March 31, 2021
 
letrollpoilu
answered 9 Months ago
98

So if anyone ever stumbles on this forum because they are having the same issue let me explain what and why it went wrong.

If you include a function not in your directory(e.g c:// or file://) but instead include using http. The include can only return what was echoed in the file, but something like a variable or function will not be shown. So always include functions and variables through a directory

Wednesday, March 31, 2021
 
BetaRide
answered 9 Months ago
87

You could simply get the character length of the current directory, and remove them from your absolute list

setlocal EnableDelayedExpansion
for /L %%n in (1 1 500) do if "!__cd__:~%%n,1!" neq "" set /a "len=%%n+1"
setlocal DisableDelayedExpansion
for /r . %%g in (*.log) do (
  set "absPath=%%g"
  setlocal EnableDelayedExpansion
  set "relPath=!absPath:~%len%!"
  echo(!relPath!
  endlocal
)
Thursday, June 3, 2021
 
Fanda
answered 6 Months ago
66
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim watcher As New IO.FileSystemWatcher()

'For watching current directory
watcher.Path = **System.IO.Directory.GetCurrentDirectory()** 'Note how to obtain current directory
watcher.NotifyFilter = NotifyFilters.LastWrite

'When I pasted your code and created my own status.txt file using 
'right click->new->Text File on Windows 7 it appended a '.txt' automatically so the
'filter wasn't finding it as the file name was status.txt.txt renaming the file
'solved the problem
watcher.Filter = "status.txt" 

AddHandler watcher.Changed, AddressOf OnChanged

watcher.EnableRaisingEvents = True
End Sub

Private Shared Sub OnChanged(ByVal source As Object, ByVal e As IO.FileSystemEventArgs)
MessageBox.Show("Got it")
End Sub

From http://bartdesmet.net/blogs/bart/archive/2004/10/21/447.aspx

You may notice in certain situations that a single creation event generates multiple Created events that are handled by your component. For example, if you use a FileSystemWatcher component to monitor the creation of new files in a directory, and then test it by using Notepad to create a file, you may see two Created events generated even though only a single file was created. This is because Notepad performs multiple file system actions during the writing process. Notepad writes to the disk in batches that create the content of the file and then the file attributes. Other applications may perform in the same manner. Because FileSystemWatcher monitors the operating system activities, all events that these applications fire will be picked up

Friday, August 27, 2021
 
laconicdev
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share