Php вывести содержимое файла
Данная функция похожа на функцию file() с той только разницей, что file_get_contents() возвращает содержимое файла в строке, начиная с указанного смещения offset и до maxlen байт. В случае неудачи, file_get_contents() вернёт FALSE .
Использование функции file_get_contents() наиболее предпочтительно в случае необходимости получить содержимое файла целиком, поскольку для улучшения производительности функция использует технику отображения файла в память (memory mapping), если она поддерживается вашей операционной системой.
Замечание:
Если вы открываете URI содержащий спецсимволы, такие как пробел, вам нужно закодировать URI при помощи urlencode() .
Список параметров
Имя читаемого файла.
Замечание:
Начиная с версии PHP 5 можно использовать константу FILE_USE_INCLUDE_PATH для поиска файла в include path.
Корректный ресурс контекста, созданный с помощью функции stream_context_create() . Если в использовании особого контекста нет необходимости, можно пропустить этот параметр передав в него значение NULL .
Смещение, с которого начнется чтение оригинального потока.
Поиск смещения ( offset ) не поддерживается при работе с удаленными файлами. Попытка поиска смещения на нелокальных файлах может работать при небольших смещениях, но этот результат является непредсказуемым, так как он работает на буферизованном потоке.
Максимальный размер читаемых данных. По умолчанию чтение осуществляется пока не будет достигнут конец файла. Учтите, что этот параметр применяется и к потоку с фильтрами.
Возвращаемые значения
Функция возвращает прочтенные данные или FALSE в случае возникновения ошибки.
Эта функция может возвращать как boolean FALSE , так и не-boolean значение, которое приводится к FALSE . За более подробной информацией обратитесь к разделу Булев тип. Используйте оператор === для проверки значения, возвращаемого этой функцией.
Ошибки
Будет сгенерирована ошибка уровня E_WARNING , если параметр filename не удается найти, параметр maxlength меньше нуля или поиск по смещению offset в потоке завершается неудачно.
Примеры
// $file = file_get_contents ( './people.txt' , true );
// > PHP 5
$file = file_get_contents ( './people.txt' , FILE_USE_INCLUDE_PATH );
?>?php
// Читаем 14 символов, начиная с 21 символа
$section = file_get_contents ( './people.txt' , NULL , NULL , 20 , 14 );
var_dump ( $section );
?>?php
Результатом выполнения данного примера будет что-то подобное:
// Создаем поток
$opts = array(
'http' =>array(
'method' => "GET" ,
'header' => "Accept-language: en\r\n" .
"Cookie: foo=bar\r\n"
)
);
$context = stream_context_create ( $opts );
Список изменений
Версия | Описание |
---|---|
5.1.0 | Добавлены аргументы offset и maxlen . |
Примечания
Замечание: Эта функция безопасна для обработки данных в двоичной форме.
Для этой функции вы можете использовать URL в качестве имени файла, если была включена опция fopen wrappers. Смотрите более подробную информацию об определении имени файла в описании функции fopen() . Смотрите также список поддерживаемых оберток URL, их возможности, замечания по использованию и список предопределенных констант в Поддерживаемые протоколы и обработчики (wrappers).
Данная функция похожа на функцию file() с той лишь разницей, что file_get_contents() возвращает содержимое файла в строке, начиная с указанного смещения offset и до length байт. В случае неудачи, file_get_contents() вернёт false .
Использование функции file_get_contents() наиболее предпочтительно в случае необходимости получить содержимое файла целиком, поскольку для улучшения производительности функция использует технику отображения файла в память (memory mapping), если она поддерживается вашей операционной системой.
Замечание:
Если вы открываете URI, содержащий спецсимволы, такие как пробел, вам нужно закодировать URI при помощи urlencode() .
Список параметров
Имя читаемого файла.
Замечание:
Можно использовать константу FILE_USE_INCLUDE_PATH для поиска файла в include path. Только помните, что если вы используете строгую типизацию, то так сделать не получится, поскольку FILE_USE_INCLUDE_PATH имеет тип int . В таком случае используйте true .
Корректный ресурс контекста, созданный с помощью функции stream_context_create() . Если в использовании особого контекста нет необходимости, можно пропустить этот параметр передав в него значение null .
Смещение, с которого начнётся чтение оригинального потока. Отрицательное значение смещения будет отсчитываться с конца потока.
Поиск смещения ( offset ) не поддерживается при работе с удалёнными файлами. Попытка поиска смещения на нелокальных файлах может работать при небольших смещениях, но результат будет непредсказуемым, так как функция работает на буферизованном потоке.
Максимальный размер читаемых данных. По умолчанию чтение осуществляется пока не будет достигнут конец файла. Учтите, что этот параметр применяется и к потоку с фильтрами.
Возвращаемые значения
Функция возвращает прочтённые данные или false в случае возникновения ошибки.
Эта функция может возвращать как логическое значение false , так и значение не типа boolean, которое приводится к false . За более подробной информацией обратитесь к разделу Булев тип. Используйте оператор === для проверки значения, возвращаемого этой функцией.
Ошибки
Будет сгенерирована ошибка уровня E_WARNING в случаях, если не удастся найти filename , задан length меньше нуля, или поиск по смещению offset в потоке завершится неудачно.
Когда file_get_contents() вызывается в каталоге, в Windows ошибка генерируется E_WARNING , а с PHP 7.4 также в других операционных системах.
Список изменений
Версия | Описание |
---|---|
8.0.0 | Параметр length теперь допускает значение null . |
7.1.0 | Добавлена поддержка отрицательных значений offset . |
Примеры
// Если включены строгие типы, то есть объявлено (strict_types=1);
$file = file_get_contents ( './people.txt' , true );
// Иначе
$file = file_get_contents ( './people.txt' , FILE_USE_INCLUDE_PATH );
?>?php
// Читаем 14 символов, начиная с 21 символа
$section = file_get_contents ( './people.txt' , FALSE , NULL , 20 , 14 );
var_dump ( $section );
?>?php
Результатом выполнения данного примера будет что-то подобное:
// Создаём поток
$opts = array(
'http' =>array(
'method' => "GET" ,
'header' => "Accept-language: en\r\n" .
"Cookie: foo=bar\r\n"
)
);
$context = stream_context_create ( $opts );
Примечания
Замечание: Эта функция безопасна для обработки данных в двоичной форме.
Для этой функции вы можете использовать URL в качестве имени файла, если была включена опция fopen wrappers. Смотрите более подробную информацию об определении имени файла в описании функции fopen() . Смотрите также список поддерживаемых обёрток URL, их возможности, замечания по использованию и список предопределённых констант в разделе Поддерживаемые протоколы и обёртки.
(PHP 4, PHP 5, PHP 7, PHP 8)
file — Читает содержимое файла и помещает его в массив
Описание
Читает содержимое файла и помещает его в массив.
Замечание:
Можно также использовать функцию file_get_contents() для получения файла в виде строки.
Список параметров
Для этой функции вы можете использовать URL в качестве имени файла, если была включена опция fopen wrappers. Смотрите более подробную информацию об определении имени файла в описании функции fopen() . Смотрите также список поддерживаемых обёрток URL, их возможности, замечания по использованию и список предопределённых констант в разделе Поддерживаемые протоколы и обёртки.
В качестве необязательного параметра flags может можно указать одну или более следующих констант: FILE_USE_INCLUDE_PATH Ищет файл в include_path. FILE_IGNORE_NEW_LINES Пропускать новую строку в конце каждого элемента массива FILE_SKIP_EMPTY_LINES Пропускать пустые строки
Возвращаемые значения
Возвращает файл в виде массива. Каждый элемент массива соответствует строке файла, с символами новой строки включительно. В случае ошибки file() возвращает false .
Замечание:
Каждая строка в полученном массиве будет завершаться символами конца строки, если только не используется FILE_IGNORE_NEW_LINES ).
Замечание: Если у вас возникают проблемы с распознаванием PHP концов строк при чтении или создании файлов на Macintosh-совместимом компьютере, включение опции auto_detect_line_endings может помочь решить проблему.
Ошибки
Вызывает ошибку уровня E_WARNING , если файл не существует.
Примеры
// Используем необязательный параметр flags
$trimmed = file ( 'somefile.txt' , FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
?>
Примечания
Смотрите также
- file_get_contents() - Читает содержимое файла в строку
- readfile() - Выводит файл
- fopen() - Открывает файл или URL
- fsockopen() - Открывает соединение с интернет-сокетом или доменным сокетом Unix
- popen() - Открывает файловый указатель процесса
- include - include
- stream_context_create() - Создаёт контекст потока
User Contributed Notes 15 notes
If the file you are reading is in CSV format do not use file(), use fgetcsv(). file() will split the file by each newline that it finds, even newlines that appear within a field (i.e. within quotations).
To write all the lines of the file in other words to read the file line by line you can write the code like this:
$names = file ( 'name.txt' );
// To check the number of lines
echo count ( $names ). '
' ;
foreach( $names as $name )
echo $name . '
' ;
>
?>
this example is so basic to understand how it's working. I hope it will help many beginners.
this may be obvious, but it took me a while to figure out what I was doing wrong. So I wanted to share. I have a file on my "c:\" drive. How do I file() it?
Don't forget the backslash is special and you have to "escape" the backslash i.e. "\\":
$lines = file ( "C:\\Documents and Settings\\myfile.txt" );
foreach( $lines as $line )
echo( $line );
>
hope this helps.
read from CSV data (file) into an array with named keys
. with or without 1st row = header (keys)
(see 4th parameter of function call as true / false)
function csv_in_array ( $url , $delm = ";" , $encl = "\"" , $head = false )
$csvxrow = file ( $url ); // ---- csv rows to array ----
$csvxrow [ 0 ] = chop ( $csvxrow [ 0 ]);
$csvxrow [ 0 ] = str_replace ( $encl , '' , $csvxrow [ 0 ]);
$keydata = explode ( $delm , $csvxrow [ 0 ]);
$keynumb = count ( $keydata );
if ( $head === true ) <
$anzdata = count ( $csvxrow );
$z = 0 ;
for( $x = 1 ; $x < $anzdata ; $x ++) <
$csvxrow [ $x ] = chop ( $csvxrow [ $x ]);
$csvxrow [ $x ] = str_replace ( $encl , '' , $csvxrow [ $x ]);
$csv_data [ $x ] = explode ( $delm , $csvxrow [ $x ]);
$i = 0 ;
foreach( $keydata as $key ) <
$out [ $z ][ $key ] = $csv_data [ $x ][ $i ];
$i ++;
>
$z ++;
>
>
else <
$i = 0 ;
foreach( $csvxrow as $item ) <
$item = chop ( $item );
$item = str_replace ( $encl , '' , $item );
$csv_data = explode ( $delm , $item );
for ( $y = 0 ; $y < $keynumb ; $y ++) <
$out [ $i ][ $y ] = $csv_data [ $y ];
>
$i ++;
>
>
?>
fuction call with 4 parameters:
(1) = the file with CSV data (url / string)
(2) = colum delimiter (e.g: ; or | or , . )
(3) = values enclosed by (e.g: ' or " or ^ or . )
(4) = with or without 1st row = head (true/false)
As of PHP 5.6 the file(), file_get_contents(), and fopen() functions will return false if you are referencing a source URL that doesn't have a valid SSL certificate. Presumably, you will run into this a lot in your development environments this will drive you crazy.
You will need to create a stream context and provide it as an argument to the various file operations to tell it to ignore invalid SSL credentials.
$args = array("ssl"=>array("verify_peer"=>false,"verify_peer_name"=>false),"http"=>array('timeout' => 60, 'user_agent' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.9) Gecko/20071025 Firefox/3.0.0.1'));
Be aware that using file() to count lines can cause OOM on the server as it'll allocate all lines into an array.
If you're dealing with files that can have thousands of lines, SplFileObject might be a better idea and with little changes you can get the same result.
("file()'s problem with UTF-16" is wrong. This is updated.
The former may miss the last line of the string.)
file() seems to have a problem in handling
UTF-16 with or without BOM.
file() is likely to think "\n"=LF (0A) as a line-ending.
So, not only "000A" but also "010A, 020A. FE0A, FF0A. "
are regarded as line-endings.
Moreover, file() causes a serious problem in UTF-16LE.
file() loses first "0A" (the first half of "0A00")!
And the next line begins with "00" (the rest of "0A00").
So lines after the first "0A" are totally different.
To avoid this phenomena,
eg. in case (php_script : UTF-8 , file : UTF-16 with line-ending "\r\n"),
mb_regex_encoding ( 'UTF-16' ); // to help mb_ereg_..() work properly
$str = file_get_contents ( $file_path );
$to_encoding = 'UTF-16' ; // encoding of string
$from_encoding = 'UTF-8' ; // encoding of PHP_script
$pattern1 = mb_convert_encoding ( '[^\r]*\r\n' , $to_encoding , $from_encoding );
mb_ereg_search_init ( $str , $pattern1 );
while ( $res = mb_ereg_search_regs ()) $file [] = $res [ 0 ];
>
$pattern2 = mb_convert_encoding ( '\A.*\r\n(.*)\z' , $to_encoding , $from_encoding );
mb_ereg ( $pattern2 , $str , $match );
$file [] = $match [ 1 ];
?>
instead of
$file = file($file_path);
If line-ending is "\n",
$pattern1 = mb_convert_encoding('[^\n]*\n', $to_encoding, $from_encoding);
Using if ( file(name.txt) ) might not be enough for testing if the file was successfully opened for reading because the file could be empty in which case the array returned is empty, so test instead with !==. e.g.:
$file_array = file('test.txt'); // an empty file
Here's my CSV converter
supports Header and trims all fields
Note: Headers must be not empty!
function csv2array ( $file , $delim = ';' , $encl = '"' , $header = false )
Note: Now that file() is binary safe it is 'much' slower than it used to be. If you are planning to read large files it may be worth your while using fgets() instead of file() For example:
$fd = fopen ( "log_file.txt" , "r" );
while (! feof ( $fd ))
<
$buffer = fgets ( $fd , 4096 );
$lines [] = $buffer ;
>
fclose ( $fd );
?>
The resulting array is $lines.
I did a test on a 200,000 line file. It took seconds with fgets() compared to minutes with file().
This note applies to PHP 5.1.6 under Windows (although may apply to other versions).
It appears that the 'FILE_IGNORE_NEW_LINES' flag doesn't remove newlines properly when reading Windows-style text files, i.e. files whose lines end in '\r\n'.
Solution: Always use 'rtrim()' in preference to 'FILE_IGNORE_NEW_LINES'.
A user suggested using rtrim always, due to the line ending conflict with files that have an EOL that differs from the server EOL.
Using rtrim with it's default character replacement is a bad solution though, as it removes all whitespace in addition to the '\r' and '\n' characters.
A good solution using rtrim follows:
$line = rtrim ( $line , "\r\n" ) . PHP_EOL ;
?>
This removes only EOL characters, and replaces with the server's EOL character, thus making preg_* work fine when matching the EOL ($)
Using file() for reading large text files > 10 Mb gives problems, therefore you should use this instead. It is much slower but it works fine. $lines will return an array with all the lines.
$handle = @ fopen ( 'yourfile. ' , "r" );
if ( $handle ) <
while (! feof ( $handle )) <
$lines [] = fgets ( $handle , 4096 );
>
fclose ( $handle );
>
?>
Если вы хотите, чтобы использовался поиск файла в include_path, установите этот параметр в true .
Возвращаемые значения
Возвращает количество прочитанных из файла байт в случае успешного выполнения или false в случае возникновения ошибки
Ошибки
В случае неудачного завершения работы генерируется ошибка уровня E_WARNING .
Примеры
Результатом выполнения данного примера будет что-то подобное:
Примечания
Замечание:
readfile() сама по себе не приводит к каким-либо проблемам с памятью, даже при отправке больших файлов. При возникновении ошибки превышения памяти убедитесь, что буферизация вывода отключена с помощью ob_get_level() .
Для этой функции вы можете использовать URL в качестве имени файла, если была включена опция fopen wrappers. Смотрите более подробную информацию об определении имени файла в описании функции fopen() . Смотрите также список поддерживаемых обёрток URL, их возможности, замечания по использованию и список предопределённых констант в разделе Поддерживаемые протоколы и обёртки.
Смотрите также
- fpassthru() - Выводит все оставшиеся данные из файлового указателя
- file() - Читает содержимое файла и помещает его в массив
- fopen() - Открывает файл или URL
- include - include
- require - require
- virtual() - Выполняет подзапрос Apache
- file_get_contents() - Читает содержимое файла в строку
User Contributed Notes 36 notes
Just a note for those who face problems on names containing spaces (e.g. "test test.pdf").
In the examples (99% of the time) you can find
header('Content-Disposition: attachment; filename='.basename($file));
but the correct way to set the filename is quoting it (double quote):
header('Content-Disposition: attachment; filename="'.basename($file).'"' );
flobee.at.gmail.dot.com shared "readfile_chunked" function. It does work, but you may encounter memory exhaustion using "fread". Meanwhile "stream_copy_to_stream" seems to utilize the same amount of memory as "readfile". At least, when I was testing "download" function for my https://github.com/Simbiat/HTTP20 library on 1.5G file with 256M memory limitation that was the case: "fread" I got peak memory usage of ~240M, while with "stream_copy_to_stream" - ~150M.
It does not mean that you can fully escape memory exhaustion, though: if you are reading too much at a time, you can still encounter it. That is why in my library I use a helper function ("speedLimit") to calculate whether selected speed limit will fit the available memory (while allowing some headroom).
You can read comments in the code itself for more details and raise issues for the library, if you think something is incorrect there (especially since it's WIP at the moment of writing this), but so far I am able to get consistent behavior with it.
if you need to limit download rate, use this code
$local_file = 'file.zip' ;
$download_file = 'name.zip' ;
// set the download rate limit (=> 20,5 kb/s)
$download_rate = 20.5 ;
if( file_exists ( $local_file ) && is_file ( $local_file ))
header ( 'Cache-control: private' );
header ( 'Content-Type: application/octet-stream' );
header ( 'Content-Length: ' . filesize ( $local_file ));
header ( 'Content-Disposition: filename=' . $download_file );
flush ();
$file = fopen ( $local_file , "r" );
while(! feof ( $file ))
// send the current file part to the browser
print fread ( $file , round ( $download_rate * 1024 ));
// flush the content to the browser
flush ();
// sleep one second
sleep ( 1 );
>
fclose ( $file );>
else die( 'Error: The file ' . $local_file . ' does not exist!' );
>
regarding php5:
i found out that there is already a disscussion @php-dev about readfile() and fpassthru() where only exactly 2 MB will be delivered.
so you may use this on php5 to get lager files
function readfile_chunked ( $filename , $retbytes = true ) $chunksize = 1 *( 1024 * 1024 ); // how many bytes per chunk
$buffer = '' ;
$cnt = 0 ;
// $handle = fopen($filename, 'rb');
$handle = fopen ( $filename , 'rb' );
if ( $handle === false ) return false ;
>
while (! feof ( $handle )) $buffer = fread ( $handle , $chunksize );
echo $buffer ;
if ( $retbytes ) $cnt += strlen ( $buffer );
>
>
$status = fclose ( $handle );
if ( $retbytes && $status ) return $cnt ; // return num. bytes delivered like readfile() does.
>
return $status ;
My script working correctly on IE6 and Firefox 2 with any typ e of files (I hope :))
function DownloadFile($file) < // $file = include path
if(file_exists($file)) header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
>
Run on Apache 2 (WIN32) PHP5
To avoid the risk of choosing themselves which files to download by messing with the request and doing things like inserting "../" into the "filename", simply remember that URLs are not file paths, and there's no reason why the mapping between them has to be so literal as "download.php?file=thingy.mpg" resulting in the download of the file "thingy.mpg".
It's your script and you have full control over how it maps file requests to file names, and which requests retrieve which files.
But even then, as ever, never trust ANYTHING in the request. Basic first-day-at-school security principle, that.
Always using MIME-Type 'application/octet-stream' is not optimal. Most if not all browsers will simply download files with that type.
If you use proper MIME types (and inline Content-Disposition), browsers will have better default actions for some of them. Eg. in case of images, browsers will display them, which is probably what you'd want.
To deliver the file with the proper MIME type, the easiest way is to use:
header('Content-Type: ' . mime_content_type($file));
header('Content-Disposition: inline; filename="'.basename($file).'"');
A note on the smartReadFile function from gaosipov:
Change the indexes on the preg_match matches to:
$begin = intval($matches[1]);
if( !empty($matches[2]) ) $end = intval($matches[2]);
>
Otherwise the $begin would be set to the entire section matched and the $end to what should be the begin.
See preg_match for more details on this.
To anyone that's had problems with Readfile() reading large files into memory the problem is not Readfile() itself, it's because you have output buffering on. Just turn off output buffering immediately before the call to Readfile(). Use something like ob_end_flush().
To avoid errors,
just be careful whether slash "/" is allowed or not at the beginning of $file_name parameter.
In my case, trying to send PDF files thru PHP after access-logging,
the beginning "/" must be removed in PHP 7.1.
$size = filesize ( $location );
$time = date ( 'r' , filemtime ( $location ));
$begin = 0 ;
$end = $size ;
header ( "Content-Type: $mimeType " );
header ( 'Cache-Control: public, must-revalidate, max-age=0' );
header ( 'Pragma: no-cache' );
header ( 'Accept-Ranges: bytes' );
header ( 'Content-Length:' .( $end - $begin ));
header ( "Content-Range: bytes $begin - $end / $size " );
header ( "Content-Disposition: inline; filename= $filename " );
header ( "Content-Transfer-Encoding: binary\n" );
header ( "Last-Modified: $time " );
header ( 'Connection: close' );
$cur = $begin ;
fseek ( $fm , $begin , 0 );
smartReadFile ( "/tmp/filename" , "myfile.mp3" , "audio/mpeg" )
?>
It can be slow for big files to read by fread, but this is a single way to read file in strict bounds. You can modify this and add fpassthru instead of fread and while, but it sends all data from begin --- it would be not fruitful if request is bytes from 100 to 200 from 100mb file.
When using the readfile_chunked function noted here with files larger than 10MB or so I am still having memory errors. It's because the writers have left out the all important flush() after each read. So this is the proper chunked readfile (which isn't really readfile at all, and should probably be crossposted to passthru(), fopen(), and popen() just so browsers can find this information):
function readfile_chunked ( $filename , $retbytes = true ) <
$chunksize = 1 *( 1024 * 1024 ); // how many bytes per chunk
$buffer = '' ;
$cnt = 0 ;
// $handle = fopen($filename, 'rb');
$handle = fopen ( $filename , 'rb' );
if ( $handle === false ) <
return false ;
>
while (! feof ( $handle )) <
$buffer = fread ( $handle , $chunksize );
echo $buffer ;
ob_flush ();
flush ();
if ( $retbytes ) <
$cnt += strlen ( $buffer );
>
>
$status = fclose ( $handle );
if ( $retbytes && $status ) <
return $cnt ; // return num. bytes delivered like readfile() does.
>
return $status ;
>
?>
All I've added is a flush(); after the echo line. Be sure to include this!
If you are lucky enough to not be on shared hosting and have apache, look at installing mod_xsendfile.
This was the only way I found to both protect and transfer very large files with PHP (gigabytes).
It's also proved to be much faster for basically any file.
Available directives have changed since the other note on this and XSendFileAllowAbove was replaced with XSendFilePath to allow more control over access to files outside of webroot.
Download the source.
Install with: apxs -cia mod_xsendfile.c
Then to use it in your script:
$file = '/tmp/blah/foo.iso' ;
$download_name = basename ( $file );
if ( file_exists ( $file )) header ( 'Content-Type: application/octet-stream' );
header ( 'Content-Disposition: attachment; filename=' . $download_name );
header ( 'X-Sendfile: ' . $file );
exit;
>
?>
Just a note: If you're using bw_mod (current version 0.6) to limit bandwidth in Apache 2, it *will not* limit bandwidth during readfile events.
Instead of using
header ( 'Content-Type: application/force-download' );
?>
use
header ( 'Content-Type: application/octet-stream' );
?>
Some browsers have troubles with force-download.
Using pieces of the forced download script, adding in MySQL database functions, and hiding the file location for security was what we needed for downloading wmv files from our members creations without prompting Media player as well as secure the file itself and use only database queries. Something to the effect below, very customizable for private access, remote files, and keeping order of your online media.
switch ($file_extension) <
case "wmv": $ctype="video/x-ms-wmv"; break;
default: $ctype="application/force-download";
>
// required for IE, otherwise Content-disposition is ignored
if(ini_get('zlib.output_compression'))
ini_set('zlib.output_compression', 'Off');
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Type: video/x-ms-wmv");
header("Content-Type: $ctype");
header("Content-Disposition: attachment; filename=\"".basename($filename)."\";");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".@filesize($filename));
set_time_limit(0);
@readfile("$fileurl") or die("File not found.");
$donwloaded = "downloads + 1";
if ($_GET["hit"]) <
mysql_query("UPDATE ibf_movies SET downloads = $donwloaded WHERE ");
If you are looking for an algorithm that will allow you to download (force download) a big file, may this one will help you.
$filename = "file.csv";
$filepath = "/path/to/file/" . $filename;
// Close sessions to prevent user from waiting until
// download will finish (uncomment if needed)
//session_write_close();
set_time_limit(0);
ignore_user_abort(false);
ini_set('output_buffering', 0);
ini_set('zlib.output_compression', 0);
$chunk = 10 * 1024 * 1024; // bytes per chunk (10 MB)
$fh = fopen($filepath, "rb");
if ($fh === false) <
echo "Unable open file";
>
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($filepath));
// Repeat reading until EOF
while (!feof($fh)) <
echo fread($handle, $chunk);
ob_flush(); // flush output
flush();
>
A mime-type-independent forced download can also be conducted by using:
(. )
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // some day in the past
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Content-type: application/x-download");
header("Content-Disposition: attachment; filename=");
header("Content-Transfer-Encoding: binary");
?>
Remember if you make a "force download" script like mentioned below that you SANITIZE YOUR INPUT!
I have seen a lot of download scripts that does not test so you are able to download anything you want on the server.
Test especially for strings like ".." which makes directory traversal possible. If possible only permit characters a-z, A-Z and 0-9 and make it possible to only download from one "download-folder".
In the C source, this function simply opens the path in read+binary mode, without a lock, and uses fpassthru()
If you need a locked read, use fopen(), flock(), and then fpassthru() directly.
I have noticed some unusual behavior with Internet Explorer 6 that’s worth taking note of. I have a link on my site to a script that outputs an XML file to the browser with the below code:
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.$filename.'"');
@readfile($file);
When the popular IE setting “Reuse Window for Launching Shortcuts” is unchecked (access this setting in the Tools Menu > Internet Options > Advanced Tab) this script will output the file to the browser and open it in a different window if the user clicks the open button on the IE prompt. However, if this setting is checked, and browser windows are being re-used, then it will open up on top of the page where the link was clicked to access the script.
If I instead set the html link target option to be “_blank”, the script will open up in a new window as expected if the “Reuse Window for Launching Shortcuts” is checked. But, if the setting is unchecked, the output XML file will open up in a new window and there will be another blank window also open that has the address of the script, in addition to our original window.
This is far from ideal, and there is no way of knowing whether users have this option checked or not. We are stuck with the distinct possibility of half of our visitors seeing either an annoying third blank window being opened or the script writing over their original window, depending on their “Reuse Window for Launching Shortcuts” setting.
That is one way to do it, however this is avoidable. For example in Zend Framework you could do
// Action controller
public function someAction ()
$response = $this -> _response ;
// Disable view and layout rendering
$this -> _helper -> viewRenderer -> setNoRender ();
$this -> _helper -> layout ()-> disableLayout ();
// Process the file
$file = 'whatever.zip' ;
$bits = @ file_get_contents ( $file );
if( strlen ( $bits ) == 0 ) $response -> setBody ( 'Sorry, we could not find requested download file.' );
>
else $response -> setHeader ( 'Content-type' , 'application/octet-stream' , true );
$response -> setBody ( $bits );
>
>
If you guys know how to judge the return values of function "stat", in order to avoid using "is_file" or "is_readable" (or "is_dir"), please let me know or just write it here.
If you don't have to do anything special on 404, "header('HTTP/1.x xxx xxxxx');" can be inside of the function.
$stat = @ stat ( $filename );
$etag = sprintf ( '%x-%x-%x' , $stat [ 'ino' ], $stat [ 'size' ], $stat [ 'mtime' ] * 1000000 );
header ( 'Expires: ' );
header ( 'Cache-Control: ' );
header ( 'Pragma: ' );
От автора: мне всегда было интересно, как работают чужие сайты. Так и хочется подсмотреть код стороннего скрипта, чтоб и себе такой «замутить». Вот только PHP – это серверный язык, поэтому «списать» чужой код не удастся. Но все-таки, как вывести файл PHP?
О чем это я?
Согласен, заголовок статьи больше ориентирован на продвинутых юзеров. Если вы таким не являетесь (пока), я сейчас поясню, почему скрипт PHP просто так не получится «подсмотреть».
Этот программный язык является серверным. То есть, выполняется на стороне сервера (на хостинге), а не на стороне клиента (в пользовательском браузере). Чтобы все это продемонстрировать, я вставлю в HTML несколько строк кода PHP.
Бесплатный курс по PHP программированию
Освойте курс и узнайте, как создать веб-приложение на PHP с полного нуля
Теперь сохраним его в файле с расширением .php и откроем в браузере.
Пока ничего необычного. Но теперь давайте просмотрим разметку страницы в браузере:
В ней мы не увидим код скрипта, поэтому вывести файл PHP стороннего ресурса не удастся, если только у вас нет доступа к его серверу. Что мы и доказали путем эксперимента .
А если надо?
Вы, как владелец собственного ресурса, можете предоставить своим пользователям доступ к определенным файлам. Для этого нужно отобразить их содержимое в браузере. Чем мы и займемся.
Чтобы реализовать данное решение, придется выполнить несколько операций:
Считать его содержимое в буфер.
Вывести содержимое на экран.
Бесплатный курс по PHP программированию
Освойте курс и узнайте, как создать веб-приложение на PHP с полного нуля
Так можно в PHP вывести содержимое текстового файла, картинку. Но код скрипта или разметку страницы точно не получится. Проверено на себе .
Отображаем скрипт
Немного «подровняем» предыдущий пример с помощью функции htmlspecialchars(), которая преобразовывает весь программный код в «читаемый» для браузера:
А можно вообще обойтись всего несколькими строчками. Для этого нам понадобится функция file_get_contents():
Но перед тем как вывести файлы из папки PHP, хотелось бы сделать «допилинг» предыдущего примера. Чтобы код скрипта отображался на экране построчно, нужно все содержимое файла преобразовать в массив. Воспользуемся функцией file():
Получаем имена файлов
Теперь с помощью PHP выведем список файлов. Так как я по своей натуре личность ленивая, то использую самый короткий метод без всяких там функций типа opendir():
В примере для получения имен файлов мы использовали функцию glob(). Она осуществляет поиск согласно шаблону, который передается ей в качестве обязательного параметра.
В качестве необязательных аргументов данная функция принимает различные флаги. Например, с помощью флага GLOB_NOSORT можно отключить сортировку по алфавиту, применяемую функцией к элементам возвращаемого массива.
Ну, на этом и закончим. Вывести список файлов в папке PHP мы научились, отображать содержимое скриптов тоже. Думаю, пока я удовлетворил ваше любопытство .
Читайте также: