PHP Snippets
upload_files
Information
(PHP Version >= 4.0.3)Functions used in this snippet: strlen, is_array, is_dir, is_writable, isset, sizeof, in_array, substr, strtolower, strrpos, move_uploaded_file.
Description
array upload_files( str form, str location, mixed types )
Upload any file that matches types filetypes, and send the files from the form name form to the file location of location.
form is the name of the form from the page where the uploads are coming from. This is the only required arguement to the function.
location defaults to the current working directory, but it can be set to any other directory that can be written to.
types are filetype extensions that are allowed. These must be lower case. The types can be either a array or a str. If you want to set types but leave the default location you can set location to NULL
The function returns an array of what happened with the upload. The returned values are mimetype, error code, whether the file was uploaded, file matches the types, filename, and filesize.
Note: This only checks file extensions. You may want to add additional checks of mimetypes to restrict data being loaded onto your server.
Snippet
<?php
function upload_files($form, $location='./', $types=array()){
$ret = array();
$location = $location === NULL || $location === '' ? './' : $location;
$location = $location{(strlen($location) - 1)} === '/' ? $location : $location . '/';
$types = is_array($types) ? $types : array($types);
if(!is_dir($location) || !is_writable($location) || !isset($_FILES[$form])){
return;
}
if(is_array($_FILES[$form]['error'])){
foreach($_FILES[$form]['error'] as $key=>$value){
$ret[$key] = array('uploaded'=>false, 'error'=>$value, 'typematch'=>false, 'name'=>$_FILES[$form]['name'][$key], 'mime'=>$_FILES[$form]['type'][$key], 'size'=>$_FILES[$form]['size'][$key]);
if($value == UPLOAD_ERR_OK) {
$ret[$key]['uploaded'] = true;
if(sizeof($types) === 0 || in_array(substr($_FILES[$form]['name'][$key], strtolower(strrpos($_FILES[$form]['name'][$key], '.')+1)), $types)){
move_uploaded_file( $_FILES[$form]['tmp_name'][$key], $location . $_FILES[$form]['name'][$key]);
$ret[$key]['typematch'] = true;
}
}
}
}else{
$ret = array('uploaded'=>false, 'error'=>$_FILES[$form]['error'], 'typematch'=>false, 'name'=>$_FILES[$form]['name'], 'mime'=>$_FILES[$form]['type'], 'size'=>$_FILES[$form]['size']);
if($_FILES[$form]['error'] == UPLOAD_ERR_OK) {
$ret['uploaded'] = true;
if(sizeof($types) === 0 || in_array(strtolower(substr($_FILES[$form]['name'], strrpos($_FILES[$form]['name'], '.')+1)), $types)){
move_uploaded_file( $_FILES[$form]['tmp_name'], $location . $_FILES[$form]['name']);
$ret['typematch'] = true;
}
}
}
return $ret;
}
?>
Example
upload_files Can be used in the following way:
<?php
// make form as a single file upload with formname 'file'
$files = upload_files('file');
// uploads the single file to the current directory no matter the filetype
// make form as a single upload with formname 'image'
$files = upload_files('image', './images/', array('gif', 'jpg', 'jpeg', 'png'));
// uploads the single file to the images directory with file extensions of gif, jpg, jpeg, or png
// make a form as an array upload with formname 'images'
$files = upload_files('images',NULL,'png');
// uploads multiple files to the current directory as long as the file extension is png
?>
2 Responses to upload_files
I'm a real noob with PHP and I'm trying to create a form where people can upload files on my server. I'm using this PHP but I'm not sure what the HTML form should look like. Could you help me out with that? Thanks!
Sure thing, The form would look similar to
<form enctype="multipart/form-data" action="file_to_post_to.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="30000" />
Send this file: <input name="file[]" type="file" />
Send this file as well: <input name="file[]" type="file" />
<input type="submit" value="Send File" />
</form>
Hope that helps