Chapter 23
How to work with files, uploads, and images
Murach's PHP and MySQL, C23
© 2014, Mike Murach & Associates, Inc.
Slide 1
Three functions to test if a file or directory exists is_file($path)
Returns true if $path exists and is a file
is_dir($path)
Returns true if $path exists and is a directory
file_exists($path)
Returns TRUE if $path exists and is either a file or a directory
A function to get the current working directory getcwd()
A constant that contains the path separator DIRECTORY_SEPARATOR
A function to get a directory listing as an array scandir($path)
Murach's PHP and MySQL, C23
© 2014, Mike Murach & Associates, Inc.
Slide 2
Display a directory listing of all files and directories
$path = getcwd(); $items = scandir($path); echo "Contents of $path"; echo ''; foreach ($items as $item) { echo '' . $item . ''; } echo '';
Murach's PHP and MySQL, C23
© 2014, Mike Murach & Associates, Inc.
Slide 3
Display only the files from a directory listing $path = getcwd(); $items = scandir($path); $files = array(); foreach ($items as $item) { $item_path = $path . DIRECTORY_SEPARATOR . $item; if (is_file($item_path)) { $files[] = $item; } } echo "Files in $path"; echo ''; foreach ($files as $file) { echo '' . $file . ''; } echo '';
Murach's PHP and MySQL, C23
© 2014, Mike Murach & Associates, Inc.
Slide 4
Uploading a file: the basic process 1. The HTML form displays the control to locate and upload a file 2. Upon form submission, the server first stores the uploaded file in a temporary directory for validation 3. The php script then needs to copy the uploaded file to its intended directory
The global PHP $_FILES array • The first parameter is the variable name from the form's input file element • The second index can be any of the following: • $_FILES['file']['name'] - the name of the uploaded file
• $_FILES['file']['type'] - the MIME (content)type of the uploaded file as provided by the browser • $_FILES['file']['size'] - the size in bytes of the uploaded file
• $_FILES['file']['tmp_name'] - the name of the temporary copy of the file stored on the server • $_FILES['file']['error'] - the error code resulting from the file upload
MIME types
An HTML form for uploading a file
The browser display of the HTML
Murach's PHP and MySQL, C23
© 2014, Mike Murach & Associates, Inc.
Slide 8
PHP for working with an uploaded file $tmp_name = $_FILES['file1']['tmp_name']; $path = getcwd() . DIRECTORY_SEPARATOR . 'images'; $name = $path . DIRECTORY_SEPARATOR . $_FILES['file1']['name']; $success = move_uploaded_file($tmp_name, $name); if ($success) { $upload_message = $name . ' has been uploaded.'; }
Murach's PHP and MySQL, C23
© 2014, Mike Murach & Associates, Inc.
Slide 9
A function that gets information about an image The getimagesize()function returns an array:
Murach's PHP and MySQL, C23
© 2014, Mike Murach & Associates, Inc.
Slide 10
Code that gets information about an image // Set the path to the image $image_path = getcwd() . DIRECTORY_SEPARATOR . 'gibson_sg.png'; // Get the image width, height, and type $image_info = getimagesize($image_path); $image_width = $image_info[0]; $image_height = $image_info[1]; $image_type = $image_info[2];
Murach's PHP and MySQL, C23
© 2014, Mike Murach & Associates, Inc.
Slide 11
Code that gets image information (continued) // Display the image type switch($image_type) { case IMAGETYPE_JPEG: echo 'This is a JPEG image.
'; break; case IMAGETYPE_GIF: echo 'This is a GIF image.
'; break; case IMAGETYPE_PNG: echo 'This is a PNG image.
'; break; default: echo 'File must be a JPEG, GIF, or PNG.
'; exit; }
Murach's PHP and MySQL, C23
© 2014, Mike Murach & Associates, Inc.
Slide 12
Code that reads and writes an image // Set the paths for the images $image_path = getcwd() . DIRECTORY_SEPARATOR . 'gibson_sg.png'; $image_path_2 = getcwd() . DIRECTORY_SEPARATOR . 'gibson_sg_2.png'; // Get the image width, height, and type $image_info = getimagesize($image_path); $image_type = $image_info[2];
Murach's PHP and MySQL, C23
© 2014, Mike Murach & Associates, Inc.
Slide 13
The user interface
Murach's PHP and MySQL, C23
© 2014, Mike Murach & Associates, Inc.
Slide 14
An image displayed in the browser
Murach's PHP and MySQL, C23
© 2014, Mike Murach & Associates, Inc.
Slide 15
The file_util.php file function get_file_list($path) { $files = array(); if (!is_dir($path)) return $files; $items = scandir($path); foreach ($items as $item) { $item_path = $path . DIRECTORY_SEPARATOR . $item; if (is_file($item_path)) { $files[] = $item; } } return $files; }
Murach's PHP and MySQL, C23
© 2014, Mike Murach & Associates, Inc.
Slide 16
The controller (index.php)
Murach's PHP and MySQL, C23
© 2014, Mike Murach & Associates, Inc.
Slide 19
The view (uploadform.php) Upload Image Upload Image Image to be uploaded
Murach's PHP and MySQL, C23
© 2014, Mike Murach & Associates, Inc.
Slide 20
The view (uploadform.php) Images in the directory No images uploaded.