Escolar Documentos
Profissional Documentos
Cultura Documentos
Para uma lista e descrição dos vários wrappers URL que podem ser utilizados como
arquivos remotos, veja em Protocolos e Wrappers suportados.
Índice ¶
+add a note
User Contributed Notes 8 notes
up
down
60
Christian ¶
15 years ago
I just learned that, to specify file names in a portable manner, you
DON'T need 'DIRECTORY_SEPARATOR' - just use '/'. This really surprised
and shocked me, as until now I typed about a zillion times
'DIRECTORY_SEPARATOR' to stay platform independent - unnecessary.
Don't make the same mistake.
up
down
-2
drapeko.com ¶
13 years ago
You have an array of directories (straightforward list of
directories):
<?php
$array = array(
'/home/drapeko/var',
'/home/drapeko/var/y',
'/home/drapeko',
'/home',
'/var/libexec'
);
);
?>
<?php
$array = array (
'home' => array (
'drapeko' => array (
'var' => array (
'y' => array()
)
)
),
'var' => array(
'libexec' => array()
)
);
?>
<?php
/**
* This function converts real filesystem path to the string array
representation.
*
* for example,
* '/home/drapeko/var/y will be converted to
$result_array['home']['drapeko']['var']['y']
* '/home/drapeko/var/y/file.txt will be converted to
$result_array['home']['drapeko']['var']['y']
*
* @param $path realpath of the directory
* @return string string array representation of the path
*/
function pathToArrayStr($path) {
// TODO constants/configs?
return $res_path;
}
?>
How can you use this function? I know it looks like a little
confusing. But it's quite simple. Consider the example below:
<?php
$result = array();
$check = array();
foreach($array as $val) {
$str = pathToArrayStr($val, 'result');
foreach($check as $ck) {
if (strpos($ck, $str) !== false) {
continue 2;
}
}
$check[] = $str;
eval('$result'.$str.' = array();');
}
print_r($result);
?>
Heh, how do you find it? This approach has helped me very much. I hope
you will find it useful. :)
up
down
-3
tunnelareaten at gmail dot com ¶
17 years ago
I made this function to search and/or display files by extension or
for a string occurance in the filename. Any comments or enhancements
are welcome offcourse. I'll update this function soon.
<?php
?>
up
down
-6
mitra at mitra dot biz ¶
19 years ago
Pollard@php.net contributed this in response to a question on setting
these variables ...
This option *IS* settable within your PHP scripts.
Example:
<?php
ini_set('auto_detect_line_endings', true);
$contents = file('unknowntype.txt');
ini_set('auto_detect_line_endings', false);
$content2 = file('unixfile.txt');
?>
Note, with PHP 4.3 anytime Mac files are read using fgets or file
you'll need to auto_detect_line_endings since \n is otherwise
assumed. However, with PHP 5.0, stream_get_line() will allow you to
specify what line ending character to read up to.
up
down
-7
Gregor Mosheh ¶
19 years ago
This function searches a directory and returns an array of all files
whose filename matches the specified regular expression. It's similar
in concept to the Unix find program.
function findfile($location='',$fileregex='') {
if (!$location or !is_dir($location) or !$fileregex) {
return false;
}
$matchedfiles = array();
$all = opendir($location);
while ($file = readdir($all)) {
if (is_dir($location.'/'.$file) and $file <> ".." and $file <>
".") {
$subdir_matches = findfile($location.'/'.$file,$fileregex);
$matchedfiles = array_merge($matchedfiles,$subdir_matches);
unset($file);
}
elseif (!is_dir($location.'/'.$file)) {
if (preg_match($fileregex,$file)) {
array_push($matchedfiles,$location.'/'.$file);
}
}
}
closedir($all);
unset($all);
return $matchedfiles;
}
$htmlfiles = findfile('/some/dir','/\.(htm|html)$/');
up
down
-9
jdhurn at uberidx dot com ¶
19 years ago
This is a function I use to determine if a file contains Binary
information. I use this for my search engine so that it doesn't try to
index files like .zip or .mp3 or any other file that doesn't contain
readable information. It makes use of the Character Type Extension if
it's loaded, if it's not then it uses Regular Expressions.
function is_binary($link)
{
$tmpStr = '';
@$fp = fopen($link, 'rb');
@$tmpStr = fread($fp, 256);
@fclose($fp);
if($tmpStr != '')
{
$tmpStr = str_replace(chr(10), '', $tmpStr);
$tmpStr = str_replace(chr(13), '', $tmpStr);
$tmpInt = 0;
if($tmpInt > 5)
return(0);
else
return(1);
}
else
return(0);
}
up
down
-8
regis at webstuff dot com dot br ¶
19 years ago
Here is a useful function if you're having trouble writing raw bytes
into a file.
function int2bytes($number){
$byte = $number;
$i=0;
do{
$dec_tmp = $byte;
$byte = bcdiv($byte,256,0);
$resto = $dec_tmp - (256 * $byte);
$return[] = $resto;
} while($byte >= 256);
if($byte) $return[] = $byte;
return array_reverse($return);
}
Example:
$arr = int2bytes(75832);
Now, to write this data to the file, just use a fputs() with chr(),
just like this:
fputs($fp,chr($arr[0]).chr($arr[1]).chr($arr[2]))
-- Regis
up
down
-11
ob at babcom dot biz ¶
9 years ago
a function based on "tunnelareaten at gmail dot com"s idea to search
for files in a given directory by a searchstring or by fileextension.
function FileSearch_r($Dir,$Search,$SearchType="search",$SavePath=1) {
$Array=array();
$D=dir($Dir);
while (false!==($Entry=$D->read()))
if ($Entry!='.' && $Entry!='..') {
$Entry=$Dir.$Entry;
if
(is_dir($Entry)) $Array=array_merge($Array,FileSearch_r($Entry.'/',
$Search,$SearchType,$SavePath));
else
if ($SearchType=="search"
?substr_count($Entry,$Search)>0
:($SearchType=="ext"
?substr($Entry,-strlen($Search))===$Search
:true))
$Array[]=$Entry;
}
$D->close();
sort($Array,SORT_STRING);
if(!(bool)
$SavePath) $Array=str_replace($Dir,"",array_values($Array));
return $Array;
}
?>
+add a note
Filesystem
Introdução
Instalação/Configuração
Constantes pré-definidas
Funções para Sistema de arquivo
Contact
Other PHP.net sites
Privacy policy