php files: an introduction

37
Working with files. @jacques_thekit

Upload: jacques-woodcock

Post on 18-Jul-2015

328 views

Category:

Technology


0 download

TRANSCRIPT

Working with files.@jacques_thekit

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.

So, files . . .

• 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

3 Scenarios

3 Scenarios 1. Upload

2. Moving

3. Reading/Writing

1. Upload 2. Moving 3. Reading/Writing

1. Upload

$file = $_FILES[‘uploaded_file’];

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

if (file_exists($file[‘tmp_name’])) { !

}

never assume a file is there

2. Moving

if (is_upload_file($file[‘tmp_name’])) { !

}

just for uploads

Tip Permissions is the biggest issue you run into when working with files.

2. Moving

is_file() is_link() is_readable() is_upload_file() is_writeable()

some other check helpers

2. Moving

move_uploaded_file()

2. Moving

if (file_exits($file[‘tmp_name’])) { !

}

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

rename()

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

unlink()

2. Moving

if (file_exists( ‘/Users/jacques/file_bucket/Cool.png’ )) { unlink( ‘/Users/jacques/file_bucket/Cool.png’ ); }

3. Reading/Writing

file()

start with read

readfile()

file_get_contents()

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

fopen()

let’s do some writing

fwrite()

fclose()

3. Reading/Writing

fopen()

or more simple

fwrite()

fclose()

file_put_contents()

3. Reading/Writing

file_put_contents( ‘/Users/jacques/file.txt’, $content );

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

Thank you.@jacques_thekit