Как найти файл php на сервере

Для поиска файлов на сервере хорошо подходит функция glob(), которая возвращает список файлов по заданной маске, например:

$files = glob('/tmp/*.jpg');

PHP

В маске можно использовать следующие специальные символы:

* Соответствует нулю или большему количеству любых символов.
? Один любой символ.
[...] Один символ входящий в группу.
[!...] Один символ не входящий в группу.
{...,...} Вхождение подстрок, работает с флагом GLOB_BRACE.
Экранирует следующий символ, кроме случаев, когда используется флаг GLOB_NOESCAPE.

Доступные флаги:

GLOB_MARK Добавляет слеш к каждой возвращаемой директории.
GLOB_NOSORT Возвращает файлы в том виде, в котором они содержатся в директории (без сортировки). Если этот флаг не указан, то имена сортируются по алфавиту.
GLOB_NOCHECK Возвращает шаблон поиска, если с его помощью не был найден ни один файл.
GLOB_NOESCAPE Обратные слеши не экранируют метасимволы.
GLOB_BRACE Раскрывает {a,b,c} для совпадения с «a», «b» или «c».
GLOB_ONLYDIR Возвращает только директории, совпадающие с шаблоном.
GLOB_ERR Останавливается при ошибках чтения (например, директории без права чтения), по умолчанию ошибки игнорируются.

Возможно использовать несколько флагов:

$files = glob('/tmp/*.jpg', GLOB_NOSORT|GLOB_ERR);

PHP

Далее во всех примерах используется папка tmp со следующим содержимым:

Содержимое папки tmp

1

Поиск в директории

Список всех файлов и директорий

$dir = __DIR__ . '/tmp';
 
$files = array();
foreach(glob($dir . '/*') as $file) {
	$files[] = basename($file);	
} 
 
print_r($files);

PHP

Результат:

Array
(
    [0] => 1.svg
    [1] => 2.jpg
    [2] => 22-f.gif
    [3] => 22.svg
    [4] => img.png
    [5] => path
    [6] => prod.png
    [7] => style-1.txt
    [8] => style-2.css
)

Только файлы

$dir = __DIR__ . '/tmp';

$files = array();
foreach(glob($dir . '/*') as $file) {
	if (is_file($file)) {
		$files[] = basename($file);	
	}
} 

print_r($files);

PHP

Результат:

Array
(
    [0] => 1.svg
    [1] => 2.jpg
    [2] => 22-f.gif
    [3] => 22.svg
    [4] => img.png
    [5] => prod.png
    [6] => style-1.txt
    [7] => style-2.css
)

Только директории

$dir = __DIR__ . '/tmp';

$files = array();
foreach(glob($dir . '/*') as $file) {
	if (is_dir($file)) {
		$files[] = basename($file);	
	}
} 

print_r($files);

PHP

Результат:

Array
(
    [0] => path
)

Поиск по расширению

$dir = __DIR__ . '/tmp';

$files = array();
foreach(glob($dir . '/*.svg') as $file) {
	$files[] = basename($file);	
} 

print_r($files);

PHP

Результат:

Array
(
    [0] => 1.svg
    [1] => 22.svg
)

Поиск по нескольким расширениям

$dir = __DIR__ . '/tmp';

$files = array();
foreach(glob($dir . '/*.{jpg,png}', GLOB_BRACE) as $file) {
	$files[] = basename($file);	
} 

print_r($files);

PHP

Результат:

Array
(
    [0] => 2.jpg
    [1] => img.png
    [2] => prod.png
)

Поиск по имени файла

Например, в названия файлов начинаются со слова «style»:

$dir = __DIR__ . '/tmp';

$files = array();
foreach(glob($dir . '/style*.*') as $file) {
	$files[] = basename($file);	
} 

print_r($files);

PHP

Результат:

Array
(
    [0] => style-1.txt
    [1] => style-2.css
)

Или начинаются с цифр:

$dir = __DIR__ . '/tmp';

$files = array();
foreach(glob($dir . '/[0-9]*.*', GLOB_BRACE) as $obj) {
	$files[] = basename($obj);	
} 

print_r($files);

PHP

Результат:

Array
(
    [0] => 1.svg
    [1] => 2.jpg
    [2] => 22-f.gif
    [3] => 22.svg
)

2

Поиск в дереве

Поиск по всем подкатегориям более сложный т.к. требует применение рекурсии.

Список всех файлов

function glob_tree_files($path, $_base_path = null)
{
	if (is_null($_base_path)) {
		$_base_path = '';
	} else {
		$_base_path .= basename($path) . '/';
	}

	$out = array();
	foreach(glob($path . '/*') as $file) {
		if (is_dir($file)) {
			$out = array_merge($out, glob_tree_files($file, $_base_path));
		} else {
			$out[] = $_base_path . basename($file);
		}
	}
 
	return $out;
}

$dir = __DIR__ . '/tmp';
$files = glob_tree_files($dir);
print_r($files);

PHP

Результат:

Array
(
    [0] => 1.svg
    [1] => 2.jpg
    [2] => 22-f.gif
    [3] => 22.svg
    [4] => img.png
    [5] => path/icon-rew.png
    [6] => path/marker.png
    [7] => path/psd/1.psd
    [8] => path/psd/2.psd
    [9] => path/psd/index.psd
    [10] => path/sh-1.png
    [11] => path/title-1.png
    [12] => prod.png
    [13] => style-1.txt
    [14] => style-2.css
)

Список всех директорий

function glob_tree_dirs($path, $_base_path = null)
{
	if (is_null($_base_path)) {
		$_base_path = '';
	} else {
		$_base_path .= basename($path) . '/';
	}

	$out = array();
	foreach(glob($path . '/*', GLOB_ONLYDIR) as $file) {
		if (is_dir($file)) {
			$out[] = $_base_path . basename($file);
			$out = array_merge($out, glob_tree_dirs($file, $_base_path));
		}
	}
 
	return $out;
}

$dir = __DIR__ . '/tmp';
$files = glob_tree_dirs($dir);
print_r($files);

PHP

Результат:

Array
(
    [0] => path
    [1] => path/psd
)

Поиск по имени/расширению

function glob_tree_search($path, $pattern, $_base_path = null)
{
	if (is_null($_base_path)) {
		$_base_path = '';
	} else {
		$_base_path .= basename($path) . '/';
	}

	$out = array();
	foreach(glob($path . '/' . $pattern, GLOB_BRACE) as $file) {
		$out[] = $_base_path . basename($file);
	}
	
	foreach(glob($path . '/*', GLOB_ONLYDIR) as $file) {
		$out = array_merge($out, glob_tree_search($file, $pattern, $_base_path));
	}

	return $out;
}

$path = __DIR__ . '/tmp';
$files = glob_tree_search($path, '*.{jpg,png}');
print_r($files);

PHP

Результат:

Array
(
    [0] => 2.jpg
    [1] => img.png
    [2] => prod.png
    [3] => path/icon-rew.png
    [4] => path/marker.png
    [5] => path/sh-1.png
    [6] => path/title-1.png
)

Чтобы в результирующих списках выводились полные пути к файлам, достаточно удалить функцию basename().

Другие публикации

Использование API Яндекс Диска на PHP

Можно найти множество применений Яндекс Диска на своем сайте, например, хранение бекапов и отчетов, обновление прайсов,…

Работа с FTP в PHP

Протокол FTP – предназначен для передачи файлов на удаленный хост. В PHP функции для работы с FTP как правило всегда доступны и не требуется установка дополнительного расширения.

Загрузка файлов на сервер PHP

В статье приведен пример формы и php-скрипта для безопасной загрузки файлов на сервер, возможные ошибки и рекомендации при работе с данной темой.

Пример парсинга html-страницы на phpQuery

phpQuery – это удобный HTML парсер взявший за основу селекторы, фильтры и методы jQuery, которые позволяют…

Поиск похожих текстов в базе данных MySQL + PHP

Один из вариантов поиска похожих статей в базе данных основан на схождении слов в двух текстах.

Список MIME типов

Ниже приведён список MIME-заголовков и расширений файлов.

(PHP 4 >= 4.3.0, PHP 5, PHP 7, PHP 8)

globНаходит файловые пути, совпадающие с шаблоном

Описание

glob(string $pattern, int $flags = 0): array|false

Список параметров

pattern

Шаблон. Не происходит раскрытие тильды и подстановка параметров.

Специальные символы:


  • * – Соответствует нулю или большему количеству символов.

  • ? – Соответствует ровно одному символу (любому символу).

  • [...] – Соответствует одному символу из группы. Если первый символ !,
    то соответствует любому символу, не входящему в группу

  • – Экранирует следующий символ,
    кроме случаев, когда
    используется флаг GLOB_NOESCAPE.
flags

Допустимые флаги:


  • GLOB_MARK – Добавляет слеш (обратный слеш в Windows) к каждой возвращаемой директории.

  • GLOB_NOSORT – Возвращает файлы в том виде, в
    котором они содержатся в директории (без сортировки). Если этот флаг
    не указан, то имена сортируются по алфавиту.

  • GLOB_NOCHECK – Возвращает шаблон поиска, если
    с его помощью не был найден ни один файл.

  • GLOB_NOESCAPE – Обратные слеши не экранируют
    метасимволы.

  • GLOB_BRACE – Раскрывает {a,b,c} для совпадения с
    ‘a’, ‘b’ или ‘c’.

  • GLOB_ONLYDIR – Возвращает только директории,
    совпадающие с шаблоном.

  • GLOB_ERR – Останавливается при ошибках чтения
    (например, директории без права чтения), по умолчанию ошибки
    игнорируются.

Замечание:

Флаг GLOB_BRACE недоступен на некоторых
не GNU-системах, например, Solaris или Alpine Linux.

Возвращаемые значения

Возвращает массив, который содержит совпадающие файлы/директории,
пустой массив в случае отсутствия совпадения или false в
случае ошибки.

Замечание:

На некоторых системах невозможно отличить отсутствие совпадения
и ошибку.

Примеры

Пример #1
Удобный способ, как при помощи glob() можно заменить
opendir() и её аналоги.


<?php
foreach (glob("*.txt") as $filename) {
echo
"$filename размер " . filesize($filename) . "n";
}
?>

Результатом выполнения данного примера
будет что-то подобное:

funclist.txt размер 44686
funcsummary.txt размер 267625
quickref.txt размер 137820

Примечания

Замечание: Эта функция неприменима для работы с удалёнными файлами, поскольку файл должен быть доступен через файловую систему сервера.

Замечание:

Функция недоступна на некоторых системах (например, старой Sun OS).

Смотрите также

  • opendir() – Открывает дескриптор каталога
  • readdir() – Получает элемент каталога по его дескриптору
  • closedir() – Закрывает дескриптор каталога
  • fnmatch() – Проверяет совпадение имени файла с шаблоном

crayonviolent at phpfreaks dot com

14 years ago


Since I feel this is rather vague and non-helpful, I thought I'd make a post detailing the mechanics of the glob regex.

glob uses two special symbols that act like sort of a blend between a meta-character and a quantifier.  These two characters are the * and ?

The ? matches 1 of any character except a /

The * matches 0 or more of any character except a /

If it helps, think of the * as the pcre equivalent of .* and ? as the pcre equivalent of the dot (.)

Note: * and ? function independently from the previous character. For instance, if you do glob("a*.php") on the following list of files, all of the files starting with an 'a' will be returned, but * itself would match:

a.php // * matches nothing

aa.php // * matches the second 'a'

ab.php // * matches 'b'

abc.php // * matches 'bc'

b.php // * matches nothing, because the starting 'a' fails

bc.php // * matches nothing, because the starting 'a' fails

bcd.php // * matches nothing, because the starting 'a' fails

It does not match just a.php and aa.php as a 'normal' regex would, because it matches 0 or more of any character, not the character/class/group before it.

Executing glob("a?.php") on the same list of files will only return aa.php and ab.php because as mentioned, the ? is the equivalent of pcre's dot, and is NOT the same as pcre's ?, which would match 0 or 1 of the previous character.

glob's regex also supports character classes and negative character classes, using the syntax [] and [^]. It will match any one character inside [] or match any one character that is not in [^].

With the same list above, executing

glob("[ab]*.php) will return (all of them):

a.php  // [ab] matches 'a', * matches nothing

aa.php // [ab] matches 'a', * matches 2nd 'a'

ab.php // [ab] matches 'a', * matches 'b'

abc.php // [ab] matches 'a', * matches 'bc'

b.php // [ab] matches 'b', * matches nothing

bc.php // [ab] matches 'b', * matches 'c'

bcd.php // [ab] matches 'b', * matches 'cd'

glob("[ab].php") will return a.php and b.php

glob("[^a]*.php") will return:

b.php // [^a] matches 'b', * matches nothing

bc.php // [^a] matches 'b', * matches 'c'

bcd.php // [^a] matches 'b', * matches 'cd'

glob("[^ab]*.php") will return nothing because the character class will fail to match on the first character.

You can also use ranges of characters inside the character class by having a starting and ending character with a hyphen in between.  For example, [a-z] will match any letter between a and z, [0-9] will match any (one) number, etc..

glob also supports limited alternation with {n1, n2, etc..}.  You have to specify GLOB_BRACE as the 2nd argument for glob in order for it to work.  So for example, if you executed glob("{a,b,c}.php", GLOB_BRACE) on the following list of files:

a.php

b.php

c.php

all 3 of them would return.  Note: using alternation with single characters like that is the same thing as just doing glob("[abc].php").  A more interesting example would be glob("te{xt,nse}.php", GLOB_BRACE) on:

tent.php

text.php

test.php

tense.php

text.php and tense.php would be returned from that glob.

glob's regex does not offer any kind of quantification of a specified character or character class or alternation.  For instance, if you have the following files:

a.php

aa.php

aaa.php

ab.php

abc.php

b.php

bc.php

with pcre regex you can do ~^a+.php$~ to return

a.php

aa.php

aaa.php

This is not possible with glob.  If you are trying to do something like this, you can first narrow it down with glob, and then get exact matches with a full flavored regex engine.  For example, if you wanted all of the php files in the previous list that only have one or more 'a' in it, you can do this:

<?php

   $list
= glob("a*.php");

   foreach (
$list as $l) {

      if (
preg_match("~^a+.php$~",$file))

        
$files[] = $l;

   }

?>



glob also does not support lookbehinds, lookaheads, atomic groupings, capturing, or any of the 'higher level' regex functions.

glob does not support 'shortkey' meta-characters like w or d.


uramihsayibok, gmail, com

13 years ago


Those of you with PHP 5 don't have to come up with these wild functions to scan a directory recursively: the SPL can do it.

<?php

$dir_iterator

= new RecursiveDirectoryIterator("/path");
$iterator = new RecursiveIteratorIterator($dir_iterator, RecursiveIteratorIterator::SELF_FIRST);
// could use CHILD_FIRST if you so wishforeach ($iterator as $file) {
    echo
$file, "n";
}
?>

Not to mention the fact that $file will be an SplFileInfo class, so you can do powerful stuff really easily:

<?php

$size

= 0;
foreach (
$iterator as $file) {
    if (
$file->isFile()) {
        echo
substr($file->getPathname(), 27) . ": " . $file->getSize() . " B; modified " . date("Y-m-d", $file->getMTime()) . "n";
       
$size += $file->getSize();
    }
}

echo

"nTotal file size: ", $size, " bytesn";?>

Lunaluna.msstyles: 4190352 B; modified 2008-04-13
LunaShellHomesteadshellstyle.dll: 362496 B; modified 2006-02-28
LunaShellMetallicshellstyle.dll: 362496 B; modified 2006-02-28
LunaShellNormalColorshellstyle.dll: 361472 B; modified 2006-02-28
Luna.theme: 1222 B; modified 2006-02-28
Windows Classic.theme: 3025 B; modified 2006-02-28

Total file size: 5281063 bytes


redcube at gmx dot de

16 years ago


Please note that glob('*') ignores all 'hidden' files by default. This means it does not return files that start with a dot (e.g. ".file").

If you want to match those files too, you can use "{,.}*" as the pattern with the GLOB_BRACE flag.

<?php

// Search for all files that match .* or *

$files = glob('{,.}*', GLOB_BRACE);

?>



Note: This also returns the directory special entries . and ..


ni dot pineau at gmail dot com

10 years ago


Note that in case you are using braces with glob you might retrieve duplicated entries for files that matche more than one item :

<?php

$a

= glob('/path/*{foo,bar}.dat',GLOB_BRACE);
print_r($a);?>

Result :
Array
(
    [0] => /path/file_foo.dat
    [1] => /path/file_foobar.dat
    [2] => /path/file_foobar.dat
)


Ultimater at gmail dot com

12 years ago


glob() isn't limited to one directory:

<?php
$results
=glob("{includes/*.php,core/*.php}",GLOB_BRACE);
echo
'<pre>',print_r($results,true),'</pre>';
?>

Just be careful when using GLOB_BRACE regarding spaces around the comma:
{includes/*.php,core/*.php} works as expected, but
{includes/*.php, core/*.php} with a leading space, will only match the former as expected but not the latter
unless you have a directory named " core" on your machine with a leading space.
PHP can create such directories quite easily like so:
mkdir(" core");


Sam Bryan

11 years ago


glob is case sensitive, even on Windows systems.

It does support character classes though, so a case insensitive version of
<?php glob('my/dir/*.csv') ?>

could be written as
<?php glob('my/dir/*.[cC][sS][vV]') ?>


heavyraptor at gmail dot com

15 years ago


glob() (array_sum() and array_map() in fact too) can be very useful if you want to calculate the sum of all the files' sizes located in a directory:

<?php
$bytes
= array_sum(array_map('filesize',glob('*')));
?>

Unfortunately there's no way to do this recursively, using glob() (as far as I know).


david dot schueler at tel-billig dot de

12 years ago


Don't use glob() if you try to list files in a directory where very much files are stored (>100.000). You get an "Allowed memory size of XYZ bytes exhausted ..." error.
You may try to increase the memory_limit variable in php.ini. Mine has 128MB set and the script will still reach this limit while glob()ing over 500.000 files.

The more stable way is to use readdir() on very large numbers of files:
<?php
// code snippet
if ($handle = opendir($path)) {
    while (
false !== ($file = readdir($handle))) {
       
// do something with the file
        // note that '.' and '..' is returned even
   
}
   
closedir($handle);
}
?>


simon at paragi dot dk

6 years ago


This is a simple and versatile function that returns an array tree of files, matching wildcards:

<?php
// List files in tree, matching wildcards * and ?
function tree($path){
  static
$match;// Find the real directory part of the path, and set the match parameter
 
$last=strrpos($path,"/");
  if(!
is_dir($path)){
   
$match=substr($path,$last);
    while(!
is_dir($path=substr($path,0,$last)) && $last!==false)
     
$last=strrpos($path,"/",-1);
  }
  if(empty(
$match)) $match="/*";
  if(!
$path=realpath($path)) return;// List files
 
foreach(glob($path.$match) as $file){
   
$list[]=substr($file,strrpos($file,"/")+1);
  } 
// Process sub directories
 
foreach(glob("$path/*", GLOB_ONLYDIR) as $dir){
   
$list[substr($dir,strrpos($dir,"/",-1)+1)]=tree($dir);
  }

    return @

$list;
}
?>


nuntius

13 years ago


First off, it's nice to see all of the different takes on this. Thanks for all of the great examples.

Fascinated by the foreach usage I was curious how it might work with a for loop. I found that glob was well suited for this, especially compared to opendir.  The for loop is always efficient when you want to protect against a potential endless loop.

$dir=$_SERVER['DOCUMENT_ROOT']."/test/directory_listing/test";
    echo $dir;
    $filesArray=glob($dir."/*.*");   

        $line.="<pre>";
    $line.=print_r($filesArray, true);
    $line.="</pre>";
    $line.="<hr>";

        for($i=0;$i<count($filesArray);$i++) {
        $line.=key($filesArray)." - ";   
        $line.=$filesArray[$i]."<br/>";
         next($filesArray);
    }

    echo $line;

Note that I pulled the glob array keys if you should need them.

Also you can tweak it for searches with something like this... (case sensitive)

$search_names="Somedocname";
$filesArray=glob($dir."/".$search_names."*.*");   

    Enjoy!


Anonymous

2 years ago


Include dotfiles excluding . and .. special dirs with .[!.]*

<?php
$all_files
= array_merge(glob('.[!.]*'), glob('*'));
// or
$all_files = glob('{.[!.],}*', GLOB_BRACE);
?>


r dot hartung at roberthartung dot de

14 years ago


You can use multiple asterisks with the glob() - function.

Example:

<?php
  $paths
= glob('my/*/dir/*.php');
?>

$paths will contains paths as following examples:

- my/1/dir/xyz.php
- my/bar/dir/bar.php
- my/bar/dir/foo.php


carlos dot lage at nospam at foo dot bar at gmail dot com

14 years ago


I lost hours looking for the solution for this problem.
glob() wasn't eating up my directory names (stuff like "foobar[]"), and I searched online for some hours, I tried preg_quote to no avail.

I finally found the proper way to escape stuff in glob() in an obscure Python mailing list:

<?php
preg_replace
('/(*|?|[)/', '[$1]', $dir_path);
?>

If you want to add a directory path before your pattern, you should do it like this:

<?php
glob
(preg_replace('/(*|?|[)/', '[$1]', $dir_path).'*.txt');
?>
preg_quote WILL NOT work in all cases (if any).


eric at muyser dot com

15 years ago


As a follow up to recursively determining all paths (by viajy at yoyo dot org) and opendir being faster than glob (by Sam Yong - hellclanner at live [dot] com).

The list all dirs code didn't seem to work, at least on my server (provided by parazuce [at] gmail [dot] com).

I needed a function to create an unlimited multidimensional array, with the names of the folders/files intact (no realpath's, although that is easily possible). This is so I can simply loop through the array, create an expandable link on the folder name, with all the files inside it.

This is the correct way to recurse I believe (no static, return small arrays to build up the multidimensional array), and includes a check for files/folders beginning with dots.

// may need modifications

function list_files($path)
{
    $files = array();

        if(is_dir($path))
    {
        if($handle = opendir($path))
        {
            while(($name = readdir($handle)) !== false)
            {
                if(!preg_match("#^.#", $name))
                if(is_dir($path . "/" . $name))
                {
                    $files[$name] = list_files($path . "/" . $name);
                }
                else
                {
                    $files[] = $name;
                }
            }

                        closedir($handle);
        }
    }

    return $files;
}

print_r(list_files("/path/to/folder"));

// example usage

function list_html($list)
{
    $html = "";

        foreach($list as $folder => $file)
    {
        if(is_array($list[$folder]))
        {
            $html .= "> (folder) " . $folder . "<br>";
            $html .= list_html($list[$folder]);
        }
        else
        {
            $html .= " (file) " . $file . "<br>";
        }
    }

        return $html;
}

echo list_html(list_files("/path/to/folder"));


sven at NOSPAM dot sven dot globalunix dot org

13 years ago


If you want to use wildcard expressions (like they are used by glob() ) to search on strings, you can use this function:

<?php   
function match_wildcard( $wildcard_pattern, $haystack ) {
  
$regex = str_replace(
     array(
"*", "?"), // wildcard chars
    
array('.*','.'),   // regexp chars
    
preg_quote($wildcard_pattern)
   );

   return

preg_match('/^'.$regex.'$/is', $haystack);
}
$test = "foobar and blobnetc.";
var_dump(
   
match_wildcard('foo*', $test),      // TRUE
   
match_wildcard('bar*', $test),      // FALSE
   
match_wildcard('*bar*', $test),     // TRUE
   
match_wildcard('**blob**', $test),  // TRUE
   
match_wildcard('*a?d*', $test),     // TRUE
   
match_wildcard('*etc**', $test)     // TRUE
);
?>


guilleva at gmail dot com

16 years ago


In some systems (AIX for example) GLOB_BRACE isn't defined and you get the error:
glob() expects parameter 2 to be long, string given in /XX/XX.php

<?

foreach (glob("{*.pdf,*.PDF}",GLOB_BRACE ) as $filename) {
      echo "$filename n";
}

?>

The method may help you in this case.

<?

foreach (array_merge(glob("*.pdf"),glob("*.PDF")) as $filename) {
      echo "$filename n";
}

?>


peter dot adrianov at gmail dot com

13 years ago


Non-recursive search files, proceeding down directory tree.

<?php
function scandir_through($dir)
{
   
$items = glob($dir . '/*');

    for (

$i = 0; $i < count($items); $i++) {
        if (
is_dir($items[$i])) {
           
$add = glob($items[$i] . '/*');
           
$items = array_merge($items, $add);
        }
    }

    return

$items;
}
?>


lesion at autistici dot org

16 years ago


Maybe all of you still know this, but it seems that if the directory contains some unresolved symlink, glob() simply ignore those files.

Mike

11 years ago


<?phpif ( ! function_exists('glob_recursive'))
{
   
// Does not support flag GLOB_BRACEfunction glob_recursive($pattern, $flags = 0)
    {
       
$files = glob($pattern, $flags);

                foreach (

glob(dirname($pattern).'/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir)
        {
           
$files = array_merge($files, glob_recursive($dir.'/'.basename($pattern), $flags));
        }

                return

$files;
    }
}
?>

bimal at sanjaal dot com

10 years ago


To pick one of the file randomly from your directory with full physical path, you can write something like this:

<?php
function one_random_image_file()
{
   
$files = glob('../images/*.jpg');
   
shuffle($files);
    return
realpath($files[0]);
}
$image_to_read = one_random_image_file();
?>


parazuce [at] gmail [dot] com

15 years ago


As a response to a post by viajy at yoyo dot org on 10-Feb-2007 04:50, he posted the following code:
<?php
function listdirs($dir) {
    static
$alldirs = array();
   
$dirs = glob($dir . '/*', GLOB_ONLYDIR);
    if (
count($dirs) > 0) {
        foreach (
$dirs as $d) $alldirs[] = $d;
    }
    foreach (
$dirs as $dir) listdirs($dir);
    return
$alldirs;
}
?>

It is not best practice to use recursive functions like that, so I wrote a safe (non-recursive) function below:

<?php
function listdirs_safe($start)
{
   
$dir  = $start;
   
$dirs = array();
   
$next = 0;

    while (

true)
    {
       
$_dirs = glob($dir.'/*', GLOB_ONLYDIR);

        if (

count($_dirs) > 0)
        {
            foreach (
$_dirs as $key => $_dir)
               
$dirs[] = $_dir;
        }
        else
            break;
$dir = $dirs[$next++];
    }

        return

$dirs;
}
?>

Example:

print_r(listdirs_safe('*'));

Output:
Array
(
    [0] => Dummy folder/Dummy folder 2
    [1] => Dummy folder/Dummy folder 2/Dummy Folder 3
    [2] => Dummy folder/Dummy folder 2/Dummy Folder 3/4
    [3] => Dummy folder/Dummy folder 2/Dummy Folder 3/Dummy folder 4
    [4] => Dummy folder/Dummy folder 2/Dummy Folder 3/4/5
)


nataxia at gmail dot com

16 years ago


Something I used to sort dir & subdir into array (multidimensional) reflecting dir structure.

    function getRecursiveFolderList($curDir,$currentA=false)
      {                   
        $dirs = glob($curDir . '/*', GLOB_ONLYDIR);    

                $cur = 0;
        foreach($dirs as $dir)
          {
            $currentA[$cur]['path'] = $dir;
            $currentA[$cur] = $this->getRecursiveFolderList($dir,$currentA[$cur]);

                            ++$cur;
          }

        return $currentA;
      }


alan at synergymx dot com

15 years ago


Here is a function that returns specific files in an array, with all of the details. Includes some basic garbage checking.

Variables

$source_folder // the location of your files
$ext // file extension you want to limit to (i.e.: *.txt)
$sec // if you only want files that are at least so old.

The function

function glob_files($source_folder, $ext, $sec, $limit){
    if( !is_dir( $source_folder ) ) {
        die ( "Invalid directory.nn" );
    }

        $FILES = glob($source_folder."*.".$ext);
    $set_limit    = 0;

        foreach($FILES as $key => $file) {

            if( $set_limit == $limit )    break;

                if( filemtime( $file ) > $sec ){

                    $FILE_LIST[$key]['path']    = substr( $file, 0, ( strrpos( $file, "\" ) +1 ) );
            $FILE_LIST[$key]['name']    = substr( $file, ( strrpos( $file, "\" ) +1 ) );   
            $FILE_LIST[$key]['size']    = filesize( $file );
            $FILE_LIST[$key]['date']    = date('Y-m-d G:i:s', filemtime( $file ) );
            $set_limit++;

                }

            }
    if(!empty($FILE_LIST)){
        return $FILE_LIST;
    } else {
        die( "No files found!nn" );
    }
}

So....

$source_folder = "c:tempmy_videos";
$ext = "flv"; // flash video files
$sec = "7200"; // files older than 2 hours
$limit = 2;

print_r(glob_files($source_folder, $ext, $sec, $limit));

Would return:

Array
(
    [0] => Array
        (
            [path] => c:tempmy_videos
            [name] => fluffy_bunnies.flv
            [size] => 21160480
            [date] => 2007-10-30 16:48:05
        )

    [1] => Array
        (
            [path] => c:tempmy_videos
            [name] => synergymx.com.flv
            [size] => 14522744
            [date] => 2007-10-25 15:34:45
        )


agd243 at gmail dot com

10 years ago


A simple function that find all files by extension an return it by an array.
<?php
function findFiles($directory, $extensions = array()) {
    function
glob_recursive($directory, &$directories = array()) {
        foreach(
glob($directory, GLOB_ONLYDIR | GLOB_NOSORT) as $folder) {
           
$directories[] = $folder;
           
glob_recursive("{$folder}/*", $directories);
        }
    }
   
glob_recursive($directory, $directories);
   
$files = array ();
    foreach(
$directories as $directory) {
        foreach(
$extensions as $extension) {
            foreach(
glob("{$directory}/*.{$extension}") as $file) {
               
$files[$extension][] = $file;
            }
        }
    }
    return
$files;
}
var_dump(findFiles("C:", array ("jpg",
   
"pdf",
   
"png",
   
"html"
)));
?>

wang yun

13 years ago


A function to quickly remove a directory (works in seconds for a hundred thousand files)

<?php
/**
* Remove the directory and its content (all files and subdirectories).
* @param string $dir the directory name
*/
function rmrf($dir) {
    foreach (
glob($dir) as $file) {
        if (
is_dir($file)) {
           
rmrf("$file/*");
           
rmdir($file);
        } else {
           
unlink($file);
        }
    }
}
?>


Anonymous

18 years ago


The example on this page will generate a warning if the glob function does not find any filenames that match the pattern.

The glob function result will only be an array if it finds some files and the foreach statement requires its argument to be an array.

By checking for the possibility that the result of the glob function may not be an array you can eliminate the warning.

Here's a better example:

<?php

$matches
= glob("*.txt");

if (
is_array ( $matches ) ) {

   foreach (
$matches as $filename) {

      echo
"$filename size " . filesize($filename) . "n";

   }

}

?>


alan at ridersite dot org

16 years ago


A couple of notes:
glob() handles symbolic filesystem links and resolves references to './', '../'  nicely and handles an extra '/' character , at least on X-systems. e.g., glob("../*") will do next higher dir.

This is good to use so warnings or errors show as "../foo" and not your system's full path. 

Several of the examples use a notation "*.*" when just plain "*" does the same thing.  The "*.*" notation is misleading as it implies foo.ext will not be found with "*" because the "." is not present.

Watch the flags must not be strings. They are defined constants. Thus,
glob("../*", GLOB_ONLYDIR)  works;
glob("../*", "GLOB_ONLYDIR")  does not.


rene dot rathmann at gmx dot de

9 years ago


You can do a recursive file search with yield.

<?php
function findAllDirs($start) {
   
$dirStack=[$start];
    while(
$dir=array_shift($dirStack)) {
       
$ar=glob($dir.'/*',GLOB_ONLYDIR|GLOB_NOSORT);
        if(!
$ar) continue;$dirStack=array_merge($dirStack,$ar);
        foreach(
$ar as $DIR)
            yield
$DIR;
    }
}
$fname='*.php';
$result=[];
foreach(
findAllDirs('/var/www/localhost') as $dir) {
   
$match=glob($dir.'/'.$fname,GLOB_NOSORT);
    if(!
$match) continue;
   
$result=array_merge($result,$match);
}
print_r($result);
?>


soywiz at gmail dot com

10 years ago


Recursive glob for PHP>=5.5

function globRecursive($path, $find) {
    $dh = opendir($path);
    while (($file = readdir($dh)) !== false) {
        if (substr($file, 0, 1) == '.') continue;
        $rfile = "{$path}/{$file}";
        if (is_dir($rfile)) {
            foreach (globRecursive($rfile, $find) as $ret) {
                yield $ret;
            }
        } else {
            if (fnmatch($find, $file)) yield $rfile;
        }
    }
    closedir($dh);
}


troy at troyonline dot com

12 years ago


If you have open_basedir set in php.ini to limit which files php can execute, glob(...) will return false when there are no matching files.  If open_basedir is not set, the very same code will return an empty array in the same situation.

This is unfortunate as a seemingly innocuous change causes different functionality that breaks code like:

foreach(glob("*.ext") as $file)
...

See this link if you have any questions as to why this is so:
http://bugs.php.net/bug.php?id=47358


endiku

13 years ago


For those who need to recursively search a directory tree and cannot or wish not to call a function within itself here is another suggestion.

I tried the previously suggested listdirs_safe() and it didn't seem to find all subfolders in a directory tree.

There are two variations on the same theme.

Single Array.
<?php
$dir
='/dir';
while(
$dirs = glob($dir . '/*', GLOB_ONLYDIR)) {
 
$dir .= '/*';
  if(!
$d) {
    
$d=$dirs;
   } else {
     
$d=array_merge($d,$dirs);
   }
}
?>

Multiple arrays.
<?php
$n
=0;
$dir ='/dir';
while(
$dirs = glob($dir . '/*', GLOB_ONLYDIR)) {
 
$dir .= '/*';
  if(!
$d) {
    
$d=$dirs;
   } else {
    
$d[$n]=$dirs;
   }
 
$n++;
}
?>

This will result in the glob looping via "dir/*/*/*/*.." until it is no longer finding a directory structure.


pope at q dot com

15 years ago


alan at ridersite dot org 18-Mar-2007 03:26 -- Stated '*.*' is the same as '*' -- This is not true as * alone will return directories too and *.* will only return files with an extension such as .pdf or .doc or .php.

viajy at yoyo dot org

16 years ago


this is a recursive function i wrote to return an array of all subdirectories of a dir

function listdirs($dir) {
    static $alldirs = array();
    $dirs = glob($dir . '/*', GLOB_ONLYDIR);
    if (count($dirs) > 0) {
        foreach ($dirs as $d) $alldirs[] = $d;
    }
    foreach ($dirs as $dir) listdirs($dir);
    return $alldirs;
}

example:
print_r(listdirs('/some/path'));


mkas at destiny dot lt

11 years ago


recursive file search

<?php
$path
[] = 'starting_place/*';

while(

count($path) != 0)
{
   
$v = array_shift($path);
    foreach(
glob($v) as $item)
    {
        if (
is_dir($item))
           
$path[] = $item . '/*';
        elseif (
is_file($item))
        {
            
//do something
       
}
    }
}
?>


Gabriel S. Luraschi

10 years ago


Use this to exclude hidden files on MS Windows.

<?php
exec
("dir "{$path}" /A:-H /B", $result);
?>


BigueNique at yahoo dot ca

13 years ago


Here is the function array_prepend() used by my latest post of safe_glob().

<?php
/**
* Prepends $string to each element of $array
* If $deep is true, will indeed also apply to sub-arrays
* @author BigueNique AT yahoo DOT ca
* @since 080324
*/
function array_prepend($array, $string, $deep=false) {
    if(empty(
$array)||empty($string)) return $array;
    foreach(
$array as $key => $element)
        if(
is_array($element))
            if(
$deep)
               
$array[$key] = array_prepend($element,$string,$deep);
            else
               
trigger_error('array_prepend: array element',E_USER_WARNING);
        else
           
$array[$key] = $string.$element;
    return
$array;

    }

?>


NOSPAM sketch at infinite dot net dot au

18 years ago


in the example below, i found i got an error if the directory was empty.

<?php

foreach (glob("*.txt") as $filename) {

   echo
"$filename size " . filesize($filename) . "n";

}

?>



I think its because glob()'ing an empty directory returns false, and so calling foreach (false as $value) will obviously break.

to fix this, i did the following:

<?php

$files
= glob("*.txt) or array(); // give it an empty array if the directory is empty or glob fails otherwise

   echo "
$filename size " . filesize($filename) . "n";

}

?>

Hope this helps someone


info at urbits dot com

17 years ago


I have been working towards a CMS-type design that is both modular and quite flat. For example, included files are all one level below the installation folder.

glob() just help me get rid of a lot of opendir() hassle. I wasn't sure if the double asterix would work - but it's fine:

foreach (glob(SERVER_PATH."/*/includes/*.php") as $inc) {
   require($inc);
}


DMan

17 years ago


Whilst on Windows, a path starting with a slash resolves OK for most file functions - but NOT glob.
If the server is LAUNCHED (or chdir()ed) to W:, then
file_exists("/temp/test.txt")
returns true for the file "W:/temp/test.txt".
But glob("/temp/*.txt") FAILS to find it!

A solution (if you want to avoid getting drive letters into your code) is to chdir() first, then just look for the file.
<?php
$glob
="/temp/*.txt";
chdir(dirname($glob));
// getcwd() is now actually "W:temp" or whateverforeach (glob(basename($glob)) as $filename) {
  
$filepath = dirname($glob)."/".$filename; // must re-attach full path
  
echo "$filepath size " . filesize($filepath) . "n";
}
?>

Note also, glob() IS case sensitive although most other file funcs on Windows are not.


Philo

2 years ago


It is also possible to nest alternations like this:
<?php
foreach (glob("{$dir}/*.{[jJ][pP]{,[eE]}[gG],[tT][iI][fF]{,[fF]}}", GLOB_BRACE) as $file) {
   
// finds both .jp(e)g and .tif(f) files
    // please note that you should double-check the file type, though
}

David Spector

5 months ago


Here is basic PHP code to do a recursive scan of an entire directory tree, allowing you to do processing when reaching each directory and file:

// Do a recursive scan of an entire directory tree,
// starting with the current working directory
// using relative filepaths
// Includes files starting with '.'
// Minimally tested on Windows and Linux

Scan('.');

function Scan($dir)
    {
    // Add code for each directory $dir here

    $subdirs=glob($dir.'/{.[!.],}*',GLOB_BRACE);
    foreach ($subdirs as $file)
        {
        if (is_file($file))
            {
            // Add code for each file $file here
            }
        else if (substr($file,0,1)!='./.')
            Scan($file);
        }
    } // Scan


davewho at nexicom dot net

2 years ago


Note that if you use braces ie. /*.{gif,jpg,htm} then glob returns
gifs before jpgs before htm files rather than in filename sequence.

bitman at biitworks dot de

2 years ago


Brackets in the pattern maust be bracketed:

Filename = " [Fail2Ban] recidive *.eml" 

The pattern should be: 

    $pattern = '[[]Fail2Ban[]] recidive *.eml';

Otherwise the brackets will be treated as character class identifier.


vincewansink at outlook dot com

3 years ago


Note that when the documentation says the files will be returned in "alphabetical" order, you may not get the results you expect if your files are numbered.

For example, files will be returned in the following order:

file.1.txt
file.10.txt
file.2.txt

Not (as you might expect):

file.1.txt
file.2.txt
file.10.txt


sebastian dot wasser at gmail dot com

7 years ago


I created a rglob function to support a '/**/' wildcard. You can even set the order to post-order or pre-order traversal.

<?phpfunction rglob ($pattern, $flags = 0, $traversePostOrder = false) {
   
// Keep away the hassles of the rest if we don't use the wildcard anyway
   
if (strpos($pattern, '/**/') === false) {
        return
glob($pattern, $flags);
    }
$patternParts = explode('/**/', $pattern);// Get sub dirs
   
$dirs = glob(array_shift($patternParts) . '/*', GLOB_ONLYDIR | GLOB_NOSORT);// Get files for current dir
   
$files = glob($pattern, $flags);

    foreach (

$dirs as $dir) {
       
$subDirContent = rglob($dir . '/**/' . implode('/**/', $patternParts), $flags, $traversePostOrder);

        if (!

$traversePostOrder) {
           
$files = array_merge($files, $subDirContent);
        } else {
           
$files = array_merge($subDirContent, $files);
        }
    }

    return

$files;
};
?>


sharshun dot aliaksandr at gmail dot com

7 years ago


<?phpfunction glob_recursive($pattern, $flags = 0){
// forked from https://github.com/rodurma/PHP-Functions/
    // blob/master/glob_recursive.php
 
$files = glob($pattern, $flags);

         foreach (

glob(dirname($pattern).'/*',
     
GLOB_ONLYDIR|GLOB_NOSORT) as $dir){
   
$files = array_merge($files, glob_recursive
       
($dir.'/'.basename($pattern), $flags));
  }
  return
$files;
}
// $a=glob_recursive('c:/test_directory/'."*.*");
// print_r($a);
function dirInfoReGet($s){
   
$a=glob_recursive($s."*.*");
   
$ar=glob_recursive($s."**/**");
   
$arr=array_unique(array_merge($a, $ar));

    foreach (

$arr as $v) {
        if (
is_dir($v)) {
           
$arra[0][]=$v.'/';
        } else {
           
$arra[1][]=$v;
        }
    }
   
sort($arra);
    return
$arra;
}
$a=dirInfoReGet('c:/test_directory/');
print_r($a);?>

http://i.stack.imgur.com/H7UF3.jpg

Best regards.


Anxiety35 at gmail dot com

8 years ago


After fiddling with GLOB_BRACE a bunch, I have found the most items that can be included in the braces is about 10 before glob no longer returns any matches.

I have a scenario where there can be a thousand or more files to check for where I can't pattern match and need to check specific names. I was hoping to batch them in large groups to see if it would help performance. However, if I include more than 10 in a GLOB_BRACE the function will return FALSE.


torch at torchsdomain dot com

16 years ago


Here is simple function that will find and remove all files (except "." ones) that match the expression ($match, "*" as wildcard) under starting directory ($path) and all other directories under it.

function rfr($path,$match){

   static $deld = 0, $dsize = 0;

   $dirs = glob($path."*");

   $files = glob($path.$match);

   foreach($files as $file){

      if(is_file($file)){

         $dsize += filesize($file);

         unlink($file);

         $deld++;

      }

   }

   foreach($dirs as $dir){

      if(is_dir($dir)){

         $dir = basename($dir) . "/";

         rfr($path.$dir,$match);

      }

   }

   return "$deld files deleted with a total size of $dsize bytes";

}


joseph dot morphy at gmail dot com

16 years ago


<?php
$html_array
= glob("*.html");

function

sort_by_mtime($file1,$file2) {
   
$time1 = filemtime($file1);
   
$time2 = filemtime($file2);
    if (
$time1 == $time2) {
        return
0;
    }
    return (
$time1 < $time2) ? 1 : -1;
    }
usort($html_array,"sort_by_mtime");
//$html_array is now ordered by the time it was last modified
?>

Привет, друзья! Довелось мне поработать на одном коммерческом сайте. И, как выяснилось, с течением недолгого времени на сайте пропала часть изображений. Удалять их, конечно же, никто не удалял, и предположения были, что папку случайно перенесли в другое место.

Да, бывает и такое. Но суть сейчас не в этом. А в том, как я искал эту папку. Поскольку мне были известны имена некоторых файлов (а это уже хорошая зацепка при поиске), то я решил написать небольшой скрипт для поиска этих файлов по всему сайту. Вдобавок ко всему специально для аудитории сайта я доработал поиск и сделал несколько разных его вариантов.

Два варианта поиска файлов по названию на PHP

В этом блоке я представляю вашему вниманию два разных варианта поиска файлов на вашем хостинге по названию.

1. Точный поиск по названию:

<?php

	header("Content-Type: text/html; charset=utf-8");

	$path = $_SERVER["DOCUMENT_ROOT"];
	$directory = new RecursiveDirectoryIterator($path);
	$iterator = new RecursiveIteratorIterator($directory);

	foreach ($iterator as $info) {

		$name_file = substr($info->getfileName(), 0, strrpos($info->getfileName(), "."));

		$name_search = array("robots", "www_pandoge_com"); // Список файлов

		foreach($name_search as $key_name) {

			if($name_file == $key_name) {
		
				echo $info->getPathname()."<br>";

			}

		}

	}

?>

2. Неточный поиск (по наличию слова в названии):

<?php

	header("Content-Type: text/html; charset=utf-8");

	$path = $_SERVER["DOCUMENT_ROOT"];
	$directory = new RecursiveDirectoryIterator($path);
	$iterator = new RecursiveIteratorIterator($directory);

	foreach ($iterator as $info) {

		$name_file = substr($info->getfileName(), 0, strrpos($info->getfileName(), "."));

		$name_search = array("robots", "www_pandoge_com"); // Список файлов

		foreach($name_search as $key_name) {

			if(preg_match("/".$key_name."/", $name_file)) {
		
				echo $info->getPathname()."<br>";

			}

		}

	}

?>

Во всех вариантах – переменная $name_search содержит в себе слово (слова) для вашего поиска по файлам.

Код вы вставляете в файл PHP с любым именем, после чего грузите его на ваш хостинг.

Обращаетесь к нему на вашем сайте, и на выходе (при положительном результате) вы получите список найденных файлов с полным их местоположением.

Поиск файлов по расширению на PHP

В поисках файлов по расширению, как вы поняли, не учитывается имя, а учитывается его формат. Например, у изображений это может быть PNG, JPG или GIF, а у аудиофайлов – MP3, WAVE и другие.

Код для поиска:

<?php

	header("Content-Type: text/html; charset=utf-8");

	$path = $_SERVER["DOCUMENT_ROOT"];
	$directory = new RecursiveDirectoryIterator($path);
	$iterator = new RecursiveIteratorIterator($directory);

	foreach ($iterator as $info) {

		$file_formal = substr($info->getfileName(), strrpos($info->getfileName(), ".") + 1);

		$name_search = array("mp3", "jpg"); // Список форматов

		foreach($name_search as $key_name) {

			if($file_formal == $key_name) {
		
				echo $info->getPathname()."<br>";

			}

		}

	}

?>

Его вы также вставляете в PHP-файл, грузите на хостинг и обращаетесь для получения результатов поиска.

Обратите внимание, что регистр во всех вариантах имеет значение. И, например, если файл на хостинге имеет расширение MP3, а в переменной для поиска вы указали «mp3» – файлы найдены не будут. Но этот момент можно подправить с помощью функции «mb_strtolower».

Доброго всем времени суток, господа! Нужен совет php гуру.

Есть на сайте директория tmp, внутри файлы, у которых известны имена (какое откуда), но не написаны расширения. Алгоритм такой, знаем первую часть имени – нужно найти есть ли файлы начинающиеся на это имя, и есть ли вообще, скажем начинается на f931992748863024fda1b647080e8ffa (я так подозреваю что в прошлом это был session_id, но не суть)

Вот подскажите, как найти все файлы, которые начинаются на указанное имя? (их может быть несколько, может быть 1, а может и не быть) . file_exists(), к сожалению, требует точного совпадения. Вторая часть файлов обычно _1.txt, _5.docx … в таком формате, т.е. подчеркивание, цифра, точка, и какое то расширение. Очень хотелось бы получить ответ на php

p.s. формировать массив о содержимом каталога, и потом его перебирать – думаю не выход, т.к. конкретно у меня в каталоге более 10тыс файлов. Это займет много времени.

Путь на сервере до файла через php, выведем на экран ! Вообще получим путь на сервере до файла разными способами!

  • Что такое путь до файла на сервере -> теория

    Иногда, очень трудно понять, что означают те или иные значения! Как можно объяснить по простому, Что такое путь до файла на сервере!?

    Давайте посмотрим в адресную строку и там вы увидите ссылку на файл и выше выделенного красным, вы не сможете подняться, это домен или по-другому называется -> корневая папка, но данный путь на корневой папке не останавливается…

    https://dwweb.ru

    /page/php/096_put_na_servere_do_fayla_php.html

    Пример пути на сервере:

    Выше есть еще несколько папок, с разной степенью доступа! И если мы весь путь, который существует выше корневой папки поставим вместо домена. то получим путь на сервере до файла

    home/domen/dwweb_ru/www

    /page/php/096_put_na_servere_do_fayla_php.html

    И если вы используете php, то вам все равно придется использовать путь на сервере до файла, потому, что многие функции работают только с файлом, к которому прописан путь на сервере

  • Получить путь до файла на сервере

    Можно ли получить путь до файла на сервере без php!? Можно, но зачем!? Предположим, что у вас нет php!? Очень странно, но возможно! Никогда такой херней не занимался, но что не сделаешь для своих любимых пользователей…

    У меня только единственный способ … кроме обращения в тех поддержку…

    Заходим в админку, поскольку я не умею гадать, то заходим в свою админку сайта, у нас это DIRECTADMIN на ruweb – е

    Ищем вкладку создать ftp аккаунт.

    После того, как вы попали на новую вкладку опять нажимаем ftp аккаунт

    Получить  путь до файла на сервере

    Далее выбираем первый чекбокс и внизу видим путь на сервер до корневой папки:

    Получить  путь до файла на сервере

  • Получить путь до файла на сервере -> способ №1

    Да! Напоминаю, что в зависимости от структуры вашего сервера, и будет зависеть путь до файла на сервере

    Для того, чтобы получить путь до файла на сервере нам понадобится, путь на сервере до корневой папки

    $_SERVER[‘DOCUMENT_ROOT’];

    Далее нам понадобится путь от корневой, до файла:

    $_SERVER[‘REQUEST_URI’];

    Теперь соберем все вместе и выведем через echo

    echo $_SERVER[‘DOCUMENT_ROOT’].$_SERVER[‘REQUEST_URI’];

    И получим путь на сервере до файла через глобальную переменную $_SERVER

    home/domen/dwweb_ru/www/page/php/096_put_na_servere_do_fayla_php.html

    Но у данной переменной есть некоторый минус, она вам покажет все, что есть после расширения, например get запрос.

    Давайте сделаем живой пример и создадим ссылку с гет запросом нажмите по ссылке и посмотрите ниже пункта 4, что прибавилось в строке…, выделено красным:

    https://dwweb.ru/page/php/096_put_na_servere_do_fayla_php.html?example=10

    Можно избавиться от гет запроса, и всякой другой шняги, которую суют пользователи после расширения, см. -> здесь

  • Как получить путь на сервере до файла с помощью -> __FILE__

    Существуют некоторые константы, которые иногда называют “Магические константы” и одну из них я в данном пункте буду использовать!

    Для того, чтобы получить путь на сервере до файла с помощью __FILE__, нам потребуется в самом файле установить код php!

    Прямо здесь, выведем вот такую конструкцию получения всего пути на сервере до файла:

    echo __FILE__ ;

    Код получения пути на сервере с помощью __FILE__

    <?

    echo __FILE__;

    ?>

    Пример вывода пути до файла на сервере:

    Теперь давайте посмотрим… что выведет выше приведенная конструкция… разметим код прямо здесь:

    home/domen/dwweb_ru/www/page/php/096_put_na_servere_do_fayla_php.html

  • Путь на сервере до файла php с использованием PHP_URL_PATH

    Когда вам нужно избавиться от всякого мусора в пути до файла на сервере, то можно использовать ниже идущий способ:

    Если вы собирайтесь делать сайт, то вам все равно придется иметь на борту несколько путей и разных, в разных переменных!

    Так не получится, что например вы написали одну переменную и этого вам хватит – точно вам говорю не хватит!

    Вчера мы рассматривали путь до корневой папки …

    Из которой вы узнали, что путь до корневой папки я помещал в переменную – $home

    $home = $_SERVER[‘DOCUMENT_ROOT’];

    В переменную $parse_url поместим функцию parse_url и разберем на части $_SERVER[‘REQUEST_URI’] из которой получим только часть ссылки где есть путь:

    $parse_url = parse_url($_SERVER[‘REQUEST_URI’], PHP_URL_PATH);

    Теперь можем соединить $home . $parse_url

    Выведем на экран путь на сервере до файла с помощью echo:

    echo $home . $parse_url ;

    Код вывода пути до файла php

    Соберем весь код вывода пути до файла в одном месте:

    <?

    $home = $_SERVER['DOCUMENT_ROOT'];

    $parse_url = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);

    echo $home . $parse_url ;

    ?>

    Результат вывода пути на сервере до файла php:

    И чтобы проверить работоспособность приведенного кода выше, разместим его пярямо здесь:

    home/domen/dwweb_ru/www/page/php/096_put_na_servere_do_fayla_php.html

  • Как сделать путь на сервере самостоятельно?

    Кроме всех выше перечисленных способов получения пути к файлу – мы констатируем, что файл уже существует, а если нам нужно создать путь к файлу, который не существует?

    Для того, чтобы сделать такой путь на сервере нам понадобится:

    Константа, которая не изменяется, (кроме если вы не выше корневой) – это путь до корневой на сервере :путь до корневой

    echo $_SERVER[‘DOCUMENT_ROOT’];

    Результат:

    home/domen/dwweb_ru/www

    Далее – нам нужно знать путь до папки(если папка будет существовать), например :

    /example/example_2/

    Соединим :

    echo $_SERVER[‘DOCUMENT_ROOT’].’/example/example_2/’;

    Выведем:

    home/domen/dwweb_ru/www/example/example_2/

    И третье… надо знать название и разрешение файла:

    example.html

    Соединим :

    echo $_SERVER[‘DOCUMENT_ROOT’].’/example/example_2/example.html’;

    Выведем:

    home/domen/dwweb_ru/www/example/example_2/example.html

    Мы вывели предполагаемый путь до не существующего файла.

    Зачем такое нужно?

    Когда вы создаете новый контент, то файла изначально не существует.

    Но путь к предполагаемому файлу вы должны знать!

    Естественно! Папки должны существовать – если нет, то предварительно их нужно создать.

    Добавить комментарий