Hướng dẫn php include full path - php bao gồm đường dẫn đầy đủ

Nếu máy chủ của bạn không điền vào "document_root", bạn có thể cần cái này


    require(str_repeat('../',(substr_count(getenv('SCRIPT_URL'),'/')-1))."/path/to/file.php");

Tôi sử dụng dòng mã này. Nó quay trở lại "trên cùng" của cây trang web, sau đó đi đến tệp mong muốn. Ví dụ: giả sử tôi có cây này: domain.com/aaa/index.php domain.com/bbb/ccc/ddd/index.php domain.com/_resource/function.php For example, let's say i have this file tree: domain.com/aaa/index.php domain.com/bbb/ccc/ddd/index.php domain.com/_resources/functions.php
For example, let's say i have this file tree:
domain.com/aaa/index.php
domain.com/bbb/ccc/ddd/index.php
domain.com/_resources/functions.php

Tôi có thể bao gồm tệp function.php từ bất cứ nơi nào tôi, chỉ bằng cách sao chép dán

require(str_repeat('../',(substr_count(getenv('SCRIPT_URL'),'/')-1))."/_resources/functions.php");

Nếu bạn cần sử dụng mã này nhiều lần, bạn có thể tạo một hàm trả về "str_repeat ('../', (subr_count (getEnv ('script_url'), '/')-1))" "Phần. Sau đó, chỉ cần chèn chức năng này vào tệp đầu tiên bạn bao gồm. Tôi có một tệp "khởi tạo.php" mà tôi bao gồm ở đầu mỗi trang PHP và chứa chức năng này. Lần tiếp theo tôi phải bao gồm các tệp, trên thực tế tôi chỉ sử dụng hàm (có tên là "path_back"):

require(path_back()."/_resources/another_php_file.php");

Trong lập trình PHP, việc lấy đường dẫn tuyệt đối, document root hay base url sẽ là 1 chút khó khăn cho những bạn mới làm quen với PHP. Vì vậy bài viết này sẽ hướng dẫn 1 số snippets để lấy absolute path, document root và base URL.

Lấy đường dẫn tuyệt đối (absolute path)

Giả sử code hoặc script của bạn đặt trong

require(str_repeat('../',(substr_count(getenv('SCRIPT_URL'),'/')-1))."/_resources/functions.php");
9, thì đoạn code snippet sau sẽ trả về kết quả như sau:

/var/www/path/example.com/httpdocs/path/directory.

Chú ý là kết quả không có dấu

require(path_back()."/_resources/another_php_file.php");
0 ở cuối.

Snippet sẽ trả về document root mà không có script filename:

$doc_root = preg_replace("!${_SERVER['SCRIPT_NAME']}$!", '', $_SERVER['SCRIPT_FILENAME']);

Ví dụ sẽ trả về dạng

require(path_back()."/_resources/another_php_file.php");
1.

Chú ý là kết quả không có dấu

require(path_back()."/_resources/another_php_file.php");
0 ở cuối.

Snippet sẽ trả về document root mà không có script filename:

Ví dụ sẽ trả về dạng

require(path_back()."/_resources/another_php_file.php");
1.

// base directory
$base_dir = __DIR__;

// server protocol
$protocol = empty($_SERVER['HTTPS']) ? 'http' : 'https';

// domain name
$domain = $_SERVER['SERVER_NAME'];

// base url
$base_url = preg_replace("!^${doc_root}!", '', $base_dir);

// server port
$port = $_SERVER['SERVER_PORT'];
$disp_port = ($protocol == 'http' && $port == 80 || $protocol == 'https' && $port == 443) ? '' : ":$port";

// put em all together to get the complete base URL
$url = "${protocol}://${domain}${disp_port}${base_url}";

echo $url; // = http://example.com/path/directory

Chú ý là kết quả không có dấu

require(path_back()."/_resources/another_php_file.php");
0 ở cuối.

Snippet sẽ trả về document root mà không có script filename:

Ví dụ sẽ trả về dạng

require(path_back()."/_resources/another_php_file.php");
1.freetuts.net, không được copy dưới mọi hình thức.

Ví dụ sẽ trả về dạng require(path_back()."/_resources/another_php_file.php"); 1.freetuts.net, không được copy dưới mọi hình thức.

# Đường dẫn tương đối là gì?

Đường dẫn tương đối là đường dẫn có điểm xuất phát từ thư mục hiện tại đang đứng. Ví dụ bạn đang ở folder

require(str_repeat('../',(substr_count(getenv('SCRIPT_URL'),'/')-1))."/_resources/functions.php");
4 và bạn muốn trỏ tới một file tên là
require(str_repeat('../',(substr_count(getenv('SCRIPT_URL'),'/')-1))."/_resources/functions.php");
5 nằm trong thư mục public thì lúc này đường dẫn chúng ta sẽ là
require(str_repeat('../',(substr_count(getenv('SCRIPT_URL'),'/')-1))."/_resources/functions.php");
6.

require(str_repeat('../',(substr_count(getenv('SCRIPT_URL'),'/')-1))."/_resources/functions.php");
7. 

Để di chuyển lùi một folder trong đường dẫn tương đối thì ta sử dụng ký tự : Cho cấu trúc folder như sau

Ví dụ: Cho cấu trúc folder như sau: Cho cấu trúc folder như sau

Bài viết này được đăng tại [free tuts .net]

Giả sử ta đang ở file cate.php nằm trong thư mục course. Bây giờ muốn truy xuất qua file

require(str_repeat('../',(substr_count(getenv('SCRIPT_URL'),'/')-1))."/_resources/functions.php");
8 nằm trong thư mục
require(str_repeat('../',(substr_count(getenv('SCRIPT_URL'),'/')-1))."/_resources/functions.php");
9 thì lúc này đường dẫn tương đối sẽ là:

Còn truy xuất file

require(str_repeat('../',(substr_count(getenv('SCRIPT_URL'),'/')-1))."/_resources/functions.php");
8 nằm trong thư mục
require(path_back()."/_resources/another_php_file.php");
1 sẽ cùng cấp nên đường dẫn lúc này là:

Khi các bạn sử dụng PHP để đọc hay viết file thì phải sử dụng đường dẫn tương đối nhé.

# Đường dẫn tuyệt đối là gì?

Đường dẫn tuyệt đối là đường dẫn có đầu đủ cấu trúc của URL  của một website.: Các đường dẫn tuyệt đối sau

  • https://freetuts.net
  • https://freetuts.net/hoc-php

Ví dụ: Các đường dẫn tuyệt đối sau: Các đường dẫn tuyệt đối sau

require(str_repeat('../',(substr_count(getenv('SCRIPT_URL'),'/')-1))."/_resources/functions.php");
7.

Tương tự với đường dẫn tương đối bạn muốn trở về một folder thì hãy sử dụng ký tự

Ví dụ: 3 sẽ tương đương với
require(path_back()."/_resources/another_php_file.php");
4
require(path_back()."/_resources/another_php_file.php");
3 sẽ tương đương với
require(path_back()."/_resources/another_php_file.php");
4

require(str_repeat('../',(substr_count(getenv('SCRIPT_URL'),'/')-1))."/_resources/functions.php");
53 sẽ tương đương với
require(path_back()."/_resources/another_php_file.php");
4: 
require(path_back()."/_resources/another_php_file.php");
3 sẽ tương đương với
require(path_back()."/_resources/another_php_file.php");
4