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.

PHP Files: An Introduction

Working with files in PHP can be a fun and a frustrating task; one you never know when you'll be asked to do. In our first beginner PHP talk, we'll go over how to work with files and some of the most common built in functions to help accomplish your tasks.

  • Be the first to comment

PHP Files: An Introduction

  1. 1. Working with files. @jacques_thekit
  2. 2. Who am I? • Graphic designer turned programmer • PHP/JS Developer 10 years experience • Have worked in Python, ActionScript, ColdFusion, others… • Big community supporter • Co-author of virtPHP Misses his beard.
  3. 3. So, files . . .
  4. 4. • fpassthru • fputcsv • fputs • fread • fscanf • fseek • fstat • ftell • ftruncate • fwrite • glob • is_dir • is_executable • is_file • is_link • is_readable • is_uploaded_file • is_writeable • lchgrp • lchown • link • linkinfo • lstat • mkdir • move_uploa • parse_ini_fil • parse_ini_str • pathinfo • pclose • popen • readfile • readlink • realpath_cac • realpath_cac • basename • chgrp • chmod • chown • clearstatcache • copy • delete • dirname • disk_free_space • disk_total_space • diskfreespace • fclose • feof • fflush • fgetc • fgetcsv • fgets • fgetss • file_exists • file_get_contents • file_put_contents • file • fileatime • filectime • filegroup • fileinode • filemtime • fileowner • fileperms • filesize • filetype • flock • fnmatch • fopen THERE’S A LOT
  5. 5. 3 Scenarios
  6. 6. 3 Scenarios 1. Upload 2. Moving 3. Reading/Writing
  7. 7. 1. Upload 2. Moving 3. Reading/Writing
  8. 8. 1. Upload $file = $_FILES[‘uploaded_file’];
  9. 9. 1. Upload $file[ ‘name’, ‘tmp_name’, ‘size’, ‘error’, ‘type’ ]; http://php.net/manual/en/features.file-upload.post-method.php php 5.4 short array syntax
  10. 10. 1. Upload http://php.net/manual/en/features.file-upload.post-method.php $file[ ‘name’, ‘tmp_name’, ‘size’, ‘error’, ‘type’ ]; File name Tmp name/path Size in bytes 0 if nothing File type
  11. 11. 1. Upload http://php.net/manual/en/features.file-upload.post-method.php $file[ ‘name’ = ‘file.png’, ‘tmp_name’ = ‘/tmp/ExOY.png’, ‘size’ = ‘1230’, ‘error’ = ‘UPLOAD_ERR_NO_FILE’, ‘type’ = ‘image/png’ ];
  12. 12. 1. Upload http://php.net/manual/en/features.file-upload.post-method.php http://php.net/manual/en/features.file-upload.errors.php List of File Errors
  13. 13. 2. Moving if (file_exists($file[‘tmp_name’])) { ! } never assume a file is there
  14. 14. 2. Moving if (is_upload_file($file[‘tmp_name’])) { ! } just for uploads
  15. 15. Tip Permissions is the biggest issue you run into when working with files.
  16. 16. 2. Moving is_file() is_link() is_readable() is_upload_file() is_writeable() some other check helpers
  17. 17. 2. Moving move_uploaded_file()
  18. 18. 2. Moving if (file_exits($file[‘tmp_name’])) { ! }
  19. 19. 2. Moving if (file_exists($file[‘tmp_name’])) { move_uploaded_file( $file[‘tmp_name’], ‘/Users/jacques/file_bucket/’ . $file[‘name’] ); }
  20. 20. 2. Moving if (file_exists($file[‘tmp_name’])) { move_uploaded_file( $file[‘tmp_name’], ‘/Users/jacques/file_bucket/’ . $file[‘name’] ); } always make sure a filename is provided
  21. 21. 2. Moving rename()
  22. 22. 2. Moving if (file_exists( ‘/Users/jacques/file_bucket/’ . $file[‘name’] )) { rename( ‘/Users/jacques/file_bucket/’ . $file[‘name’] ‘/Users/jacques/file_bucket/Cool.png’ ); }
  23. 23. 2. Moving if (file_exists( ‘/Users/jacques/file_bucket/’ . $file[‘name’] )) { rename( ‘/Users/jacques/file_bucket/’ . $file[‘name’] ‘/Users/Angela/Pictures/heart.png’ ); }
  24. 24. 2. Moving unlink()
  25. 25. 2. Moving if (file_exists( ‘/Users/jacques/file_bucket/Cool.png’ )) { unlink( ‘/Users/jacques/file_bucket/Cool.png’ ); }
  26. 26. 3. Reading/Writing file() start with read readfile() file_get_contents()
  27. 27. 3. Reading/Writing file() readfile() file_get_contents() reads to array reads to output buffer reads to string
  28. 28. 3. Reading/Writing file() readfile() file_get_contents() for manipulation for outputting contents elsewhere for manipulation
  29. 29. 3. Reading/Writing file() readfile() file_get_contents() WATCH MEMORY no worries on memory WATCH MEMORY
  30. 30. 3. Reading/Writing fopen() let’s do some writing fwrite() fclose()
  31. 31. 3. Reading/Writing fopen() or more simple fwrite() fclose() file_put_contents()
  32. 32. 3. Reading/Writing file_put_contents( ‘/Users/jacques/file.txt’, $content );
  33. 33. 3. Reading/Writing file_put_contents( ‘/Users/jacques/file.txt’, $content ); will create file if not already there
  34. 34. 3. Reading/Writing file_put_contents( ‘/Users/jacques/file.txt’, $content, FILE_APPEND ); flags; FILE_USE_INCLUDE_PATH FILE_APPEND LOCK_EX
  35. 35. 3. Reading/Writing http://php.net/manual/en/function.file-put-contents.php
  36. 36. Thank you. @jacques_thekit

×