Nobody seems to have tied together all of the example code in various StackOverflow questions into a good, well-put-together sample of how to handle a photo upload. Here's my start... please help improve it.
Here's our setup:
- Our file upload control is named
$file
, e.g.<input type="file" name="<?= $file ?>" />
. - We want to save the photo to
$photosPath
, e.g.$photosPath = "/photos/"
. - We want the filename to be
$targetFilename . ".jpg"
, where e.g.$targetFilename
could be from a username text field in our upload form. - We want to store the resulting file path in
$filePath
, e.g. for insertion into a database. - We want to only accept .jpgs.
- We want to only accept files of at most
$maxSize
bytes.