file_get_contents(), ancak PHP dosyalarını okumak için en optimum yoldur - you're always limited to the amount of memory available bellekteki dosyaları okuyorsanız beri.
Eğer doğru izinlere sahip ancak yine sisteminizde kullanılabilir bellek miktarı ile sınırlı olacak eğer bir ini_set('memory_limit', -1) verebilir, bu tüm programlama dilleri yaygındır.
Belirtilen - tek çözüm read the file in chunks, bunun için dördüncü ve beşinci argümanları ($offset ile file_get_contents() kullanabilir ve $maxlen için bytes):
string file_get_contents(string $filename[, bool $use_include_path = false[, resource $context[, int $offset = -1[, int $maxlen = -1]]]])
İşte büyük dosya indirme hizmet için bu tekniği kullanmak bir örnek:
public function Download($path, $speed = null)
{
if (is_file($path) === true)
{
set_time_limit(0);
while (ob_get_level() > 0)
{
ob_end_clean();
}
$size = sprintf('%u', filesize($path));
$speed = (is_int($speed) === true) ? $size : intval($speed) * 1024;
header('Expires: 0');
header('Pragma: public');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Content-Type: application/octet-stream');
header('Content-Length: ' . $size);
header('Content-Disposition: attachment; filename="' . basename($path) . '"');
header('Content-Transfer-Encoding: binary');
for ($i = 0; $i <= $size; $i = $i + $speed)
{
ph()->HTTP->Flush(file_get_contents($path, false, null, $i, $speed));
ph()->HTTP->Sleep(1);
}
exit();
}
return false;
}
Başka bir seçenek kullanılmasıdır az burada, fopen(), feof(), fgets() ve fclose() fonksiyonları, specially if you care about getting whole lines at once optimize olduğu another example I provided in another StackOverflow question for importing large SQL queries into the database,
function SplitSQL($file, $delimiter = ';')
{
set_time_limit(0);
if (is_file($file) === true)
{
$file = fopen($file, 'r');
if (is_resource($file) === true)
{
$query = array();
while (feof($file) === false)
{
$query[] = fgets($file);
if (preg_match('~' . preg_quote($delimiter, '~') . '\s*$~iS', end($query)) === 1)
{
$query = trim(implode('', $query));
if (mysql_query($query) === false)
{
echo '<h3>ERROR: ' . $query . '</h3>' . "\n";
}
else
{
echo '<h3>SUCCESS: ' . $query . '</h3>' . "\n";
}
while (ob_get_level() > 0)
{
ob_end_flush();
}
flush();
}
if (is_string($query) === true)
{
$query = array();
}
}
return fclose($file);
}
}
return false;
}
Hangi gerçekten (SQL ithalat fonksiyonu ve yükleme fonksiyonu ile görebileceğiniz gibi) yapmak için çalışıyoruz ne bağlıdır kullanmak, ancak you'll always have to read the data in chunks tekniği.