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.

TDC São Paulo 2016 - Become a jedi with php streams

750 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: Software
  • Be the first to comment

  • Be the first to like this

TDC São Paulo 2016 - Become a jedi with php streams

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

×