Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

scdevsumit 2016 - Become a jedi with php streams

1,900 views

Published on

Improve your skills with streams in PHP, understand how to use context and what are the differences between use f* or file* functions

Published in: Internet

scdevsumit 2016 - Become a jedi with php streams

  1. 1. BECOME A JEDI WITH PHP STREAMS
  2. 2. marabesi @MatheusMarabesi
  3. 3. ZENDSC10X 10/jun
  4. 4. WHAT IS A STREAM ?
  5. 5. 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
  6. 6. ??????
  7. 7. DATA FLOW
  8. 8. INPUT OUTPUT DATA
  9. 9. PHP
  10. 10. F* FILE*
  11. 11. 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
  12. 12. 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 RESOURCE FILE PATH
  13. 13. fopen(‘file.txt’, ‘r+’); file_​get_​contents( ‘file.txt’ );
  14. 14. WRAPPERS
  15. 15. 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
  16. 16. 1) file://
  17. 17. fopen( ‘file.txt’, ‘r+’ ); fopen( ‘file://file.txt’, ‘r+’ ); != ?
  18. 18. 2) http://
  19. 19. print file_get_contents( 'http://marabesi. com' );
  20. 20. $contexto = stream_context_create([ 'http' => [ 'method' => 'POST' 'header' => 'Content-Type: application/x-www-form-urlencoded', 'content' => 'livro=php' ] ]);
  21. 21. print file_get_contents( http://marabesi.com', false, $context );
  22. 22. $fp = fopen( 'http://marabesi.com', 'r', false, $context ); print fgets($fp);
  23. 23. http://php.net/manual/en/context.php
  24. 24. 3) ftp://
  25. 25. $fp = fopen( 'ftp://usuario:senha' . '@meu_servidor.com' . '/home/matheusmarabesi/index.php', 'r' );
  26. 26. 4) php://
  27. 27. $put = fopen( 'php://stdin, 'r' ); print fgets($put);
  28. 28. 5) zlib://
  29. 29. $arquivoComprimido = fopen('compress.zlib: //arquivo.tar.gz', 'r'); while(feof($arquivoComprimido) !== true) { print fgets($compressedFile); }
  30. 30. file_put_contents( 'compress.zlib:///var/www/arquivo.txt.gz', 'Vou ser comprimido!' );
  31. 31. arquivo. txt000077700000000000000 000000000211257332276101 1244 0ustar rootrootmatheusmarabesi
  32. 32. file_put_contents( 'compress.zlib:///var/www/arquivo.txt.gz', 'Vou ser comprimido!' );
  33. 33. 6) data://
  34. 34. print file_get_contents( 'data://text/plain, Utilizando o wrapper data://' );
  35. 35. print file_get_contents( 'data://text/plain, Utilizando o wrapper data://' );
  36. 36. <img src="data: image/jpeg;base64, YXNodXNhdXNhdWlzYXVzYWl1" />
  37. 37. print file_get_contents( 'data: //text/plain;base64, VXRpbGl6YW5kbyBzdHJlYW1zIGVtIFBIUCAh');
  38. 38. 7) glob://
  39. 39. $diretorio = new RecursiveDirectoryIterator( '/var/www' ); 1
  40. 40. $iterator = new RecursiveIteratorIterator( $diretorio ); 2
  41. 41. $arquivos = new RegexIterator( $iterator, '/^.+.php/', RecursiveRegexIterator::GET_MATCH ); 3
  42. 42. $diretorio = new RecursiveDirectoryIterator( 'glob://var/www/*.php' ); foreach ($diretorio as $arquivos) { print $arquivos->getFilename(); }
  43. 43. 8) ogg://
  44. 44. 2) ogg://
  45. 45. 9) mandinga://
  46. 46. 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
  47. 47. 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; } }
  48. 48. public function stream_read($bytes) { return fread($this->file, $bytes); } public function stream_eof() { return feof($this->file); }
  49. 49. stream_register_wrapper stream_wrapper_register != ?
  50. 50. stream_register_wrapper( 'mandinga', 'Mandinga' );
  51. 51. stream_register_wrapper( 'mandinga', 'Mandinga' ); ALIAS CLASS NAME
  52. 52. print file_get_contents( mandinga://pessoas.txt' );
  53. 53. marabesi @MatheusMarabesi

×