Viết tập lệnh PHP để lấy tên tệp hiện tại

Tên tệp đường dẫn đầy đủ hiện tại được giữ trong biến hệ thống $_SERVER['PHP_SELF']. Bạn có thể sử dụng chức năng basename() để lấy tên tệp mà không cần đường dẫn; . Ví dụ,

Chuyển hướng đến trang khác

Sử dụng hàm header(), theo sau là lệnh exit (không tiếp tục tập lệnh hiện tại). Ví dụ,

Để tải lại (làm mới) trang hiện tại, bạn có thể

Mảng in

Sử dụng hàm có sẵn

$a = array(-1 => 5);
echo $a[-1];
var_dump($a);
0 để nối các phần tử của mảng thành một chuỗi, e. g. ,

Mặt khác,

$a = array(-1 => 5);
echo $a[-1];
var_dump($a);
1 tách một chuỗi thành một mảng

$str1 = 'apple,orange,banana';
var_dump(explode(',', $str1));

$str2 = 'apple orange banana';
var_dump(explode(' ', $str2));

Chỉ số mảng phủ định so với. Chỉ mục vị trí chuỗi phủ định

Bạn có thể sử dụng khóa mảng âm trong một mảng kết hợp. Ví dụ,

$a = array(-1 => 5);
echo $a[-1];
var_dump($a);

Khóa mảng phủ định khác với chỉ mục vị trí phủ định cho chuỗi, chỉ mục này lập chỉ mục vị trí từ phía sau chuỗi. Ví dụ,

Just was curious to count files and lines in a project

  function DirLineCounter( $dir , $result = array('lines_html' => false, 'files_count' => false, 'lines_count' => false ), $complete_table = true )
  {

      $file_read = array( 'php', 'html', 'js', 'css' );
      $dir_ignore = array();

      $scan_result = scandir( $dir );

      foreach ( $scan_result as $key => $value ) {

          if ( !in_array( $value, array( '.', '..' ) ) ) {

              if ( is_dir( $dir . DIRECTORY_SEPARATOR . $value ) ) {

                  if ( in_array( $value, $dir_ignore ) ) {
                    continue;
                  }

                  $result = DirLineCounter( $dir . DIRECTORY_SEPARATOR . $value, $result, false );

              }
              else {

  function DirLineCounter( $dir , $result = array('lines_html' => false, 'files_count' => false, 'lines_count' => false ), $complete_table = true )
  {
0

  function DirLineCounter( $dir , $result = array('lines_html' => false, 'files_count' => false, 'lines_count' => false ), $complete_table = true )
  {
1

  function DirLineCounter( $dir , $result = array('lines_html' => false, 'files_count' => false, 'lines_count' => false ), $complete_table = true )
  {
2

  function DirLineCounter( $dir , $result = array('lines_html' => false, 'files_count' => false, 'lines_count' => false ), $complete_table = true )
  {
3

  function DirLineCounter( $dir , $result = array('lines_html' => false, 'files_count' => false, 'lines_count' => false ), $complete_table = true )
  {
4

  function DirLineCounter( $dir , $result = array('lines_html' => false, 'files_count' => false, 'lines_count' => false ), $complete_table = true )
  {
5

  function DirLineCounter( $dir , $result = array('lines_html' => false, 'files_count' => false, 'lines_count' => false ), $complete_table = true )
  {
6

  function DirLineCounter( $dir , $result = array('lines_html' => false, 'files_count' => false, 'lines_count' => false ), $complete_table = true )
  {
7

  function DirLineCounter( $dir , $result = array('lines_html' => false, 'files_count' => false, 'lines_count' => false ), $complete_table = true )
  {
8

  function DirLineCounter( $dir , $result = array('lines_html' => false, 'files_count' => false, 'lines_count' => false ), $complete_table = true )
  {
9

      $file_read = array( 'php', 'html', 'js', 'css' );
      $dir_ignore = array();
0

      $file_read = array( 'php', 'html', 'js', 'css' );
      $dir_ignore = array();
1

      $file_read = array( 'php', 'html', 'js', 'css' );
      $dir_ignore = array();
2

      $file_read = array( 'php', 'html', 'js', 'css' );
      $dir_ignore = array();
3

A key problem to hierarchical include trees is that PHP processes include paths relative to the original file, not the current including file.

A solution to that, is to prefix all include paths with:

this will generate a base path relative to the current file, which will then allow an include behavior similar to C/C++.

thus, to include a file that is 1 in the parent directory:

to include a file that is in the same directory:

to include a file that is in a subdirectory:

Notice that all paths we reference must NOT begin with a /, and must be relative to the current file, in order to concatenate correctly.

Làm cách nào để lấy tên tệp hiện tại bằng tập lệnh PHP?

Cú pháp. 'SCRIPT_NAME' cung cấp đường dẫn từ thư mục gốc bao gồm tên của thư mục. .
Để có được tên tập tin hiện tại. Chúng tôi sử dụng $currentPage= $_SERVER['SCRIPT_NAME'];
Để hiển thị tên tệp hiện tại. Chúng tôi sử dụng. tiếng vang $currentPage;

Cách lấy đường dẫn tệp hiện tại trong PHP?

getcwd() trả về đường dẫn của tập lệnh "chính" được tham chiếu trong URL. dirname(__FILE__) sẽ trả về đường dẫn của tập lệnh hiện đang thực thi.

Làm cách nào để lấy tên tệp hiện tại mà không cần phần mở rộng trong PHP?

Bạn có thể sử dụng các hàm substr() và strrpos() để truy xuất tên tệp không có phần mở rộng trong PHP. $filename = 'tên tệp. php'; . "));

Làm cách nào để lấy tên thư mục hiện tại trong PHP?

Hàm getcwd() trả về thư mục làm việc hiện tại.