Viewed   161 times

I am using include function and it is giving errors. That is file not found. In the root directory I have - index.php and config.php

index.php includes config.php and has some other data.

config.php has database details and includes function/function.php

There is a folder user and it has a file calculate.php in calculate.php I have included AJAX functionality and a file is loaded in it on AJAX call. File is cal2.php and it is located in user folder. Now this, cal2.php has a include function for config.php like:

include "../config.php";

When this cal2.php is loaded from calculate.php

function/function.php file is not loaded. It shows file not found for function/function.php

So, file structure is:

  • root
  • /index.php
  • /config.php
  • /user/calculate.php
  • /user/cal2.php
  • /function/function.php

How to proceed and not have function.php include error for cal2.php

 Answers

3

You should change config.php to use an absolute path to functions.php. If you have PHP 5.3 or greater, do it like this:

include(__DIR__.'/functions/functions.php');

If you are still using PHP 5.2 (or, god forbid, something earlier), you can do it this way:

$dir = dirname(__FILE__);
include($dir.'/functions/functions.php');

The __FILE__ magic constant always contains the value of the current PHP file name, so the dirname(__FILE__) call will give you the full filesystem path of the current script.

Saturday, August 20, 2022
 
2

Files are included if and when the include statement is reached at runtime. To very succinctly summarise what that means, the following file is never going to be included:

if (false) {
    include 'foo.php';
}
Sunday, August 14, 2022
 
neurino
 
1
  • Make sure the file exists: use os.listdir() to see the list of files in the current working directory
  • Make sure you're in the directory you think you're in with os.getcwd() (if you launch your code from an IDE, you may well be in a different directory)
  • You can then either:
    • Call os.chdir(dir), dir being the folder where the file is located, then open the file with just its name like you were doing.
    • Specify an absolute path to the file in your open call.
  • Remember to use a raw string if your path uses backslashes, like so: dir = r'C:Python32'
    • If you don't use raw-string, you have to escape every backslash: 'C:\User\Bob\...'
    • Forward-slashes also work on Windows 'C:/Python32' and do not need to be escaped.

Let me clarify how Python finds files:

  • An absolute path is a path that starts with your computer's root directory, for example 'C:Pythonscripts..' if you're on Windows.
  • A relative path is a path that does not start with your computer's root directory, and is instead relative to something called the working directory. You can view Python's current working directory by calling os.getcwd().

If you try to do open('sortedLists.yaml'), Python will see that you are passing it a relative path, so it will search for the file inside the current working directory. Calling os.chdir will change the current working directory.

Example: Let's say file.txt is found in C:Folder.

To open it, you can do:

os.chdir(r'C:Folder')
open('file.txt') #relative path, looks inside the current working directory

or

open(r'C:Folderfile.txt') #full path
Friday, December 23, 2022
 
peper0
 
4

You can't include php files relatively to your webroot that way, cause if you use the slash as first character, the reference will go much deeper than just your document root. So, instead of using your basepath, you could do something like this :

<?php 
   $path = $_SERVER['DOCUMENT_ROOT'];
   $path .= "/yourpath/yourfile.php";
   include_once($path);
?>
Friday, August 12, 2022
 
3

It is definitive an encoding problem.

Try this (if './çö' isn't UTF-8, dynamicly loaded for example):

unlink(mb_convert_encoding('./çö', 'UTF-8'))

Maybe you are using Windows? From here:

unlink(iconv('utf-8', 'cp1252', './çö'));

Anyway: You should avoid those filenames. If it comes from user: NEVER TRUST USER INPUT!

Friday, September 2, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :