scdevsumit 2016 - Become a jedi with php streams
-
Upload
matheus-marabesi -
Category
Internet
-
view
1.442 -
download
3
Transcript of scdevsumit 2016 - Become a jedi with php streams
![Page 1: scdevsumit 2016 - Become a jedi with php streams](https://reader031.fdocuments.net/reader031/viewer/2022030310/58f9b368760da3da068bd5de/html5/thumbnails/1.jpg)
BECOME A JEDI WITH PHP STREAMS
![Page 2: scdevsumit 2016 - Become a jedi with php streams](https://reader031.fdocuments.net/reader031/viewer/2022030310/58f9b368760da3da068bd5de/html5/thumbnails/2.jpg)
marabesi
@MatheusMarabesi
![Page 3: scdevsumit 2016 - Become a jedi with php streams](https://reader031.fdocuments.net/reader031/viewer/2022030310/58f9b368760da3da068bd5de/html5/thumbnails/3.jpg)
![Page 4: scdevsumit 2016 - Become a jedi with php streams](https://reader031.fdocuments.net/reader031/viewer/2022030310/58f9b368760da3da068bd5de/html5/thumbnails/4.jpg)
![Page 5: scdevsumit 2016 - Become a jedi with php streams](https://reader031.fdocuments.net/reader031/viewer/2022030310/58f9b368760da3da068bd5de/html5/thumbnails/5.jpg)
ZENDSC10X
10/jun
![Page 6: scdevsumit 2016 - Become a jedi with php streams](https://reader031.fdocuments.net/reader031/viewer/2022030310/58f9b368760da3da068bd5de/html5/thumbnails/6.jpg)
![Page 7: scdevsumit 2016 - Become a jedi with php streams](https://reader031.fdocuments.net/reader031/viewer/2022030310/58f9b368760da3da068bd5de/html5/thumbnails/7.jpg)
WHAT IS A STREAM ?
![Page 8: scdevsumit 2016 - Become a jedi with php streams](https://reader031.fdocuments.net/reader031/viewer/2022030310/58f9b368760da3da068bd5de/html5/thumbnails/8.jpg)
A stream is a body of water[1] with a current, confined within a bed and stream banks. Depending on its location or certain characteristics, a stream may be referred to as a branch, brook, beck, burn, creek, crick, gill (occasionally ghyll), kill, lick, mill race, rill, river, syke, bayou, rivulet, streamage, wash, run, or runnel.
https://en.wikipedia.org/wiki/Stream
![Page 9: scdevsumit 2016 - Become a jedi with php streams](https://reader031.fdocuments.net/reader031/viewer/2022030310/58f9b368760da3da068bd5de/html5/thumbnails/9.jpg)
![Page 10: scdevsumit 2016 - Become a jedi with php streams](https://reader031.fdocuments.net/reader031/viewer/2022030310/58f9b368760da3da068bd5de/html5/thumbnails/10.jpg)
??????
![Page 11: scdevsumit 2016 - Become a jedi with php streams](https://reader031.fdocuments.net/reader031/viewer/2022030310/58f9b368760da3da068bd5de/html5/thumbnails/11.jpg)
DATA FLOW
![Page 12: scdevsumit 2016 - Become a jedi with php streams](https://reader031.fdocuments.net/reader031/viewer/2022030310/58f9b368760da3da068bd5de/html5/thumbnails/12.jpg)
INPUT
OUTPUT
DATA
![Page 13: scdevsumit 2016 - Become a jedi with php streams](https://reader031.fdocuments.net/reader031/viewer/2022030310/58f9b368760da3da068bd5de/html5/thumbnails/13.jpg)
PHP
![Page 14: scdevsumit 2016 - Become a jedi with php streams](https://reader031.fdocuments.net/reader031/viewer/2022030310/58f9b368760da3da068bd5de/html5/thumbnails/14.jpg)
F* FILE*
![Page 15: scdevsumit 2016 - Become a jedi with php streams](https://reader031.fdocuments.net/reader031/viewer/2022030310/58f9b368760da3da068bd5de/html5/thumbnails/15.jpg)
fopen
fpassthru
fputcsv
fputs
fread
fscanf
fseek
fstat
ftell
ftruncate
fwrite
file_ exists
file_ get_ contents
file_ put_ contents
file
fileatime
filectime
filegroup
fileinode
filemtime
fileowner
fileperms
![Page 16: scdevsumit 2016 - Become a jedi with php streams](https://reader031.fdocuments.net/reader031/viewer/2022030310/58f9b368760da3da068bd5de/html5/thumbnails/16.jpg)
fopenfpassthrufputcsvfputsfreadfscanffseekfstatftellftruncatefwrite
file_ existsfile_ get_ contentsfile_ put_ contentsfilefileatimefilectimefilegroupfileinodefilemtimefileownerfileperms
RESOURCE FILE PATH
![Page 17: scdevsumit 2016 - Become a jedi with php streams](https://reader031.fdocuments.net/reader031/viewer/2022030310/58f9b368760da3da068bd5de/html5/thumbnails/17.jpg)
fopen(‘file.txt’, ‘r+’);file_ get_ contents( ‘file.txt’);
![Page 18: scdevsumit 2016 - Become a jedi with php streams](https://reader031.fdocuments.net/reader031/viewer/2022030310/58f9b368760da3da068bd5de/html5/thumbnails/18.jpg)
WRAPPERS
![Page 19: scdevsumit 2016 - Become a jedi with php streams](https://reader031.fdocuments.net/reader031/viewer/2022030310/58f9b368760da3da068bd5de/html5/thumbnails/19.jpg)
file:// — Accessing local filesystem
http:// — Accessing HTTP(s) URLs
ftp:// — Accessing FTP(s) URLs
php:// — Accessing various I/O streams
zlib:// — Compression Streams
data:// — Data (RFC 2397)
glob:// — Find pathnames matching pattern
phar:// — PHP Archive
ssh2:// — Secure Shell 2
rar:// — RAR
ogg:// — Audio streams
![Page 20: scdevsumit 2016 - Become a jedi with php streams](https://reader031.fdocuments.net/reader031/viewer/2022030310/58f9b368760da3da068bd5de/html5/thumbnails/20.jpg)
1) file://
![Page 21: scdevsumit 2016 - Become a jedi with php streams](https://reader031.fdocuments.net/reader031/viewer/2022030310/58f9b368760da3da068bd5de/html5/thumbnails/21.jpg)
fopen( ‘file.txt’, ‘r+’);
fopen( ‘file://file.txt’, ‘r+’);
!= ?
![Page 22: scdevsumit 2016 - Become a jedi with php streams](https://reader031.fdocuments.net/reader031/viewer/2022030310/58f9b368760da3da068bd5de/html5/thumbnails/22.jpg)
2) http://
![Page 23: scdevsumit 2016 - Become a jedi with php streams](https://reader031.fdocuments.net/reader031/viewer/2022030310/58f9b368760da3da068bd5de/html5/thumbnails/23.jpg)
print file_get_contents(
'http://marabesi.
com'
);
![Page 24: scdevsumit 2016 - Become a jedi with php streams](https://reader031.fdocuments.net/reader031/viewer/2022030310/58f9b368760da3da068bd5de/html5/thumbnails/24.jpg)
![Page 25: scdevsumit 2016 - Become a jedi with php streams](https://reader031.fdocuments.net/reader031/viewer/2022030310/58f9b368760da3da068bd5de/html5/thumbnails/25.jpg)
$contexto = stream_context_create([
'http' => [
'method' => 'POST'
'header' => 'Content-Type:
application/x-www-form-urlencoded',
'content' => 'livro=php'
]
]);
![Page 26: scdevsumit 2016 - Become a jedi with php streams](https://reader031.fdocuments.net/reader031/viewer/2022030310/58f9b368760da3da068bd5de/html5/thumbnails/26.jpg)
print file_get_contents(
http://marabesi.com',
false,
$context
);
![Page 27: scdevsumit 2016 - Become a jedi with php streams](https://reader031.fdocuments.net/reader031/viewer/2022030310/58f9b368760da3da068bd5de/html5/thumbnails/27.jpg)
$fp = fopen(
'http://marabesi.com',
'r',
false,
$context
);
print fgets($fp);
![Page 28: scdevsumit 2016 - Become a jedi with php streams](https://reader031.fdocuments.net/reader031/viewer/2022030310/58f9b368760da3da068bd5de/html5/thumbnails/28.jpg)
http://php.net/manual/en/context.php
![Page 29: scdevsumit 2016 - Become a jedi with php streams](https://reader031.fdocuments.net/reader031/viewer/2022030310/58f9b368760da3da068bd5de/html5/thumbnails/29.jpg)
3) ftp://
![Page 30: scdevsumit 2016 - Become a jedi with php streams](https://reader031.fdocuments.net/reader031/viewer/2022030310/58f9b368760da3da068bd5de/html5/thumbnails/30.jpg)
$fp = fopen(
'ftp://usuario:senha' .
'@meu_servidor.com' .
'/home/matheusmarabesi/index.php',
'r'
);
![Page 31: scdevsumit 2016 - Become a jedi with php streams](https://reader031.fdocuments.net/reader031/viewer/2022030310/58f9b368760da3da068bd5de/html5/thumbnails/31.jpg)
![Page 32: scdevsumit 2016 - Become a jedi with php streams](https://reader031.fdocuments.net/reader031/viewer/2022030310/58f9b368760da3da068bd5de/html5/thumbnails/32.jpg)
4) php://
![Page 33: scdevsumit 2016 - Become a jedi with php streams](https://reader031.fdocuments.net/reader031/viewer/2022030310/58f9b368760da3da068bd5de/html5/thumbnails/33.jpg)
$put = fopen(
'php://stdin, 'r'
);
print fgets($put);
![Page 34: scdevsumit 2016 - Become a jedi with php streams](https://reader031.fdocuments.net/reader031/viewer/2022030310/58f9b368760da3da068bd5de/html5/thumbnails/34.jpg)
![Page 35: scdevsumit 2016 - Become a jedi with php streams](https://reader031.fdocuments.net/reader031/viewer/2022030310/58f9b368760da3da068bd5de/html5/thumbnails/35.jpg)
5) zlib://
![Page 36: scdevsumit 2016 - Become a jedi with php streams](https://reader031.fdocuments.net/reader031/viewer/2022030310/58f9b368760da3da068bd5de/html5/thumbnails/36.jpg)
$arquivoComprimido = fopen('compress.zlib:
//arquivo.tar.gz', 'r');
while(feof($arquivoComprimido) !== true) {
print fgets($compressedFile);
}
![Page 37: scdevsumit 2016 - Become a jedi with php streams](https://reader031.fdocuments.net/reader031/viewer/2022030310/58f9b368760da3da068bd5de/html5/thumbnails/37.jpg)
file_put_contents(
'compress.zlib:///var/www/arquivo.txt.gz',
'Vou ser comprimido!'
);
![Page 38: scdevsumit 2016 - Become a jedi with php streams](https://reader031.fdocuments.net/reader031/viewer/2022030310/58f9b368760da3da068bd5de/html5/thumbnails/38.jpg)
arquivo.
txt000077700000000000000
000000000211257332276101
1244 0ustar
rootrootmatheusmarabesi
![Page 39: scdevsumit 2016 - Become a jedi with php streams](https://reader031.fdocuments.net/reader031/viewer/2022030310/58f9b368760da3da068bd5de/html5/thumbnails/39.jpg)
file_put_contents(
'compress.zlib:///var/www/arquivo.txt.gz',
'Vou ser comprimido!'
);
![Page 40: scdevsumit 2016 - Become a jedi with php streams](https://reader031.fdocuments.net/reader031/viewer/2022030310/58f9b368760da3da068bd5de/html5/thumbnails/40.jpg)
6) data://
![Page 41: scdevsumit 2016 - Become a jedi with php streams](https://reader031.fdocuments.net/reader031/viewer/2022030310/58f9b368760da3da068bd5de/html5/thumbnails/41.jpg)
print file_get_contents(
'data://text/plain, Utilizando o
wrapper data://'
);
![Page 42: scdevsumit 2016 - Become a jedi with php streams](https://reader031.fdocuments.net/reader031/viewer/2022030310/58f9b368760da3da068bd5de/html5/thumbnails/42.jpg)
print file_get_contents(
'data://text/plain, Utilizando o
wrapper data://'
);
![Page 43: scdevsumit 2016 - Become a jedi with php streams](https://reader031.fdocuments.net/reader031/viewer/2022030310/58f9b368760da3da068bd5de/html5/thumbnails/43.jpg)
<img src="data:
image/jpeg;base64,
YXNodXNhdXNhdWlzYXVzYWl1"
/>
![Page 44: scdevsumit 2016 - Become a jedi with php streams](https://reader031.fdocuments.net/reader031/viewer/2022030310/58f9b368760da3da068bd5de/html5/thumbnails/44.jpg)
print file_get_contents( 'data:
//text/plain;base64,
VXRpbGl6YW5kbyBzdHJlYW1zIGVtIFBIUCAh');
![Page 45: scdevsumit 2016 - Become a jedi with php streams](https://reader031.fdocuments.net/reader031/viewer/2022030310/58f9b368760da3da068bd5de/html5/thumbnails/45.jpg)
7) glob://
![Page 46: scdevsumit 2016 - Become a jedi with php streams](https://reader031.fdocuments.net/reader031/viewer/2022030310/58f9b368760da3da068bd5de/html5/thumbnails/46.jpg)
![Page 47: scdevsumit 2016 - Become a jedi with php streams](https://reader031.fdocuments.net/reader031/viewer/2022030310/58f9b368760da3da068bd5de/html5/thumbnails/47.jpg)
$diretorio = new
\RecursiveDirectoryIterator(
'/var/www'
);
1
![Page 48: scdevsumit 2016 - Become a jedi with php streams](https://reader031.fdocuments.net/reader031/viewer/2022030310/58f9b368760da3da068bd5de/html5/thumbnails/48.jpg)
$iterator = new
\RecursiveIteratorIterator(
$diretorio
);
2
![Page 49: scdevsumit 2016 - Become a jedi with php streams](https://reader031.fdocuments.net/reader031/viewer/2022030310/58f9b368760da3da068bd5de/html5/thumbnails/49.jpg)
$arquivos = new \RegexIterator(
$iterator, '/^.+\.php/',
\RecursiveRegexIterator::GET_MATCH
);
3
![Page 50: scdevsumit 2016 - Become a jedi with php streams](https://reader031.fdocuments.net/reader031/viewer/2022030310/58f9b368760da3da068bd5de/html5/thumbnails/50.jpg)
![Page 51: scdevsumit 2016 - Become a jedi with php streams](https://reader031.fdocuments.net/reader031/viewer/2022030310/58f9b368760da3da068bd5de/html5/thumbnails/51.jpg)
$diretorio = new
\RecursiveDirectoryIterator(
'glob://var/www/*.php'
);
foreach ($diretorio as $arquivos) {
print $arquivos->getFilename();
}
![Page 52: scdevsumit 2016 - Become a jedi with php streams](https://reader031.fdocuments.net/reader031/viewer/2022030310/58f9b368760da3da068bd5de/html5/thumbnails/52.jpg)
8) ogg://
![Page 53: scdevsumit 2016 - Become a jedi with php streams](https://reader031.fdocuments.net/reader031/viewer/2022030310/58f9b368760da3da068bd5de/html5/thumbnails/53.jpg)
![Page 54: scdevsumit 2016 - Become a jedi with php streams](https://reader031.fdocuments.net/reader031/viewer/2022030310/58f9b368760da3da068bd5de/html5/thumbnails/54.jpg)
![Page 55: scdevsumit 2016 - Become a jedi with php streams](https://reader031.fdocuments.net/reader031/viewer/2022030310/58f9b368760da3da068bd5de/html5/thumbnails/55.jpg)
2) ogg://
![Page 56: scdevsumit 2016 - Become a jedi with php streams](https://reader031.fdocuments.net/reader031/viewer/2022030310/58f9b368760da3da068bd5de/html5/thumbnails/56.jpg)
9) mandinga://
![Page 57: scdevsumit 2016 - Become a jedi with php streams](https://reader031.fdocuments.net/reader031/viewer/2022030310/58f9b368760da3da068bd5de/html5/thumbnails/57.jpg)
streamWrapper {
public resource $context ;
__construct ( void )
__destruct ( void )
public bool dir_closedir ( void )
public bool dir_opendir ( string $path , int $options )
public string dir_readdir ( void )
public bool dir_rewinddir ( void )
public bool mkdir ( string $path , int $mode , int $options )
public bool rename ( string $path_from , string $path_to )
public bool rmdir ( string $path , int $options )
public resource stream_cast ( int $cast_as )
public void stream_close ( void )
public bool stream_eof ( void )
public bool stream_flush ( void )
public bool stream_lock ( int $operation )
public bool stream_metadata ( string $path , int $option , mixed $value )
public bool stream_open ( string $path , string $mode , int $options , string &$opened_path
)
public string stream_read ( int $count )
public bool stream_seek ( int $offset , int $whence = SEEK_SET )
public bool stream_set_option ( int $option , int $arg1 , int $arg2 )
public array stream_stat ( void )
THERE IS MORE
![Page 58: scdevsumit 2016 - Become a jedi with php streams](https://reader031.fdocuments.net/reader031/viewer/2022030310/58f9b368760da3da068bd5de/html5/thumbnails/58.jpg)
class Mandinga {
private $arquivo;
public function stream_open($arquivo, $modo)
{
if (!file_exists($arquivo)) {
throw new \Exception(
'O arquivo informado não existe'
);
$this->arquivo = fopen($arquivo, $modo);
return true;
}
}
![Page 59: scdevsumit 2016 - Become a jedi with php streams](https://reader031.fdocuments.net/reader031/viewer/2022030310/58f9b368760da3da068bd5de/html5/thumbnails/59.jpg)
public function stream_read($bytes)
{
return fread($this->file,
$bytes);
}
public function stream_eof()
{
return feof($this->file);
}
![Page 60: scdevsumit 2016 - Become a jedi with php streams](https://reader031.fdocuments.net/reader031/viewer/2022030310/58f9b368760da3da068bd5de/html5/thumbnails/60.jpg)
stream_register_wrapper
stream_wrapper_register
!= ?
![Page 61: scdevsumit 2016 - Become a jedi with php streams](https://reader031.fdocuments.net/reader031/viewer/2022030310/58f9b368760da3da068bd5de/html5/thumbnails/61.jpg)
stream_register_wrapper(
'mandinga', 'Mandinga'
);
![Page 62: scdevsumit 2016 - Become a jedi with php streams](https://reader031.fdocuments.net/reader031/viewer/2022030310/58f9b368760da3da068bd5de/html5/thumbnails/62.jpg)
stream_register_wrapper(
'mandinga', 'Mandinga'
);
ALIAS
CLASS NAME
![Page 63: scdevsumit 2016 - Become a jedi with php streams](https://reader031.fdocuments.net/reader031/viewer/2022030310/58f9b368760da3da068bd5de/html5/thumbnails/63.jpg)
print file_get_contents(
mandinga://pessoas.txt'
);
![Page 64: scdevsumit 2016 - Become a jedi with php streams](https://reader031.fdocuments.net/reader031/viewer/2022030310/58f9b368760da3da068bd5de/html5/thumbnails/64.jpg)
marabesi
@MatheusMarabesi