Filetype php consolidating
The first 4 Chapters from this book are also available on
If you’d rather read them offline, you can download the chapters in PDF format.
An common challenge faced by PHP programmers is how to accept files uploaded by visitors to your site.
In this bonus excerpt from Chapter 12 of the recently published Site Point book: Build Your Own Database Driven Web Site Using PHP & My SQL (4th Edition) by Kevin Yank, you’ll learn how to accept file uploads from your web site visitors securely and store them.
tag that submitted the file, so the actual array index will depend on that attribute.
jpeg$/i', $_FILES['upload']['type'])) else if (preg_match('/^image/gif$/i', $_FILES['upload']['type'])) else if (preg_match('/^image/(x-)? $ext; // Copy the file (if it is deemed safe) if (!
png$/i', $_FILES['upload']['type'])) else // The complete path/filename $filename = 'C:/uploads/' . is_uploaded_file($_FILES['upload']['tmp_name']) or ! copy($_FILES['upload']['tmp_name'], $filename)) really referred to an uploaded file, your script might be used to copy sensitive files on your server into a directory from which they would become publicly accessible over the Web!
If you try to reject files because you have limited disk space and/or bandwidth, the fact that large files can still be uploaded, even though they’re deleted almost immediately, may be a problem for you. The default value is 2MB, so if you want to accept uploads larger than that, you’ll immediately need to change that value.
Instead, you can tell PHP in advance the maximum file size you wish to accept. A second restriction, affecting the total size of form submissions, is enforced by the tags in the form, so that PHP is apprised of this restriction before it receives any submitted files.