
15-04-2012, 15:45:41
|
| |
PHP'de dosya içeriğini okumak için tüm metotların bulunduğu fonksiyon. Kullanılabilecek her yöntemi kullanır; başarılı bir dosya okuma işlemi gerçekleştirmenizi sağlar: PHP- Kodu: function my_read_file($filename)
{
static $methods, $disabled_functions, $command;
// Check file is exists...
if (!file_exists($filename))
return false;
if (!isset($methods))
$methods = array(
'file_get_contents',
'file',
'fopen',
'shell_exec',
'exec',
'popen',
'system',
'passthru',
);
if (!isset($disabled_functions))
$disabled_functions = explode(',', @ini_get('disable_functions'));
// Determine OS
if (!isset($command))
{
if (strpos(strtolower(php_uname()), 'win') === 0)
{
$command = 'type ' . escapeshellcmd($filename);
if (version_compare('5.3', PHP_VERSION, '>'))
$command = '"' . $command . '"';
}
else
$command = 'cat ' . escapeshellcmd($filename);
}
foreach ($methods as $method)
{
if (in_array($method, $disabled_functions))
continue;
if ($method === 'file_get_contents')
return file_get_contents($filename);
if ($method === 'file')
return implode('', file($filename));
if ($method === 'fopen' || $method === 'popen')
{
if ($method === 'fopen')
$handle = fopen($filename, 'r');
else
$handle = popen($command, 'r');
$string = '';
while (!feof($handle))
$string .= fread($handle, 4096);
if ($method === 'fopen')
fclose($handle);
else
pclose($handle);
return $string;
}
if ($method === 'shell_exec')
return shell_exec($command);
if ($method === 'exec')
{
$output = array();
exec($command, $output);
return implode("\n", $output);
}
if ($method === 'system' || $method === 'passthru')
{
ob_start();
$method($command);
$output = ob_get_clean();
return $output;
}
}
// If we got here, nothing succeeded
return false;
}
(alıntıdır, daha yüksek performans için düzenlenmiştir) |