php files: an introduction
TRANSCRIPT
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.
• 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_uploaded_file • parse_ini_file • parse_ini_string • pathinfo • pclose • popen • readfile • readlink • realpath_cache_get • realpath_cache_size
• 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
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
1. Uploadhttp://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
1. Uploadhttp://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’ ];
1. Uploadhttp://php.net/manual/en/features.file-upload.post-method.php
http://php.net/manual/en/features.file-upload.errors.php
List of File Errors
2. Moving
is_file() is_link() is_readable() is_upload_file() is_writeable()
some other check helpers
2. Moving
if (file_exists($file[‘tmp_name’])) { move_uploaded_file( $file[‘tmp_name’], ‘/Users/jacques/file_bucket/’ . $file[‘name’] ); }
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
2. Moving
if (file_exists( ‘/Users/jacques/file_bucket/’ . $file[‘name’] )) { rename( ‘/Users/jacques/file_bucket/’ . $file[‘name’] ‘/Users/jacques/file_bucket/Cool.png’ ); }
2. Moving
if (file_exists( ‘/Users/jacques/file_bucket/’ . $file[‘name’] )) { rename( ‘/Users/jacques/file_bucket/’ . $file[‘name’] ‘/Users/Angela/Pictures/heart.png’ ); }
2. Moving
if (file_exists( ‘/Users/jacques/file_bucket/Cool.png’ )) { unlink( ‘/Users/jacques/file_bucket/Cool.png’ ); }
3. Reading/Writing
file()
readfile()
file_get_contents()
reads to array
reads to output buffer
reads to string
3. Reading/Writing
file()
readfile()
file_get_contents()
for manipulation
for outputting contents elsewhere
for manipulation
3. Reading/Writing
file()
readfile()
file_get_contents()
WATCH MEMORY
no worries on memory
WATCH MEMORY
3. Reading/Writing
file_put_contents( ‘/Users/jacques/file.txt’, $content );
will create file if not already there
3. Reading/Writing
file_put_contents( ‘/Users/jacques/file.txt’, $content, FILE_APPEND );
flags; FILE_USE_INCLUDE_PATH FILE_APPEND LOCK_EX
3. Reading/Writing
http://php.net/manual/en/function.file-put-contents.php