PHP Classes

PHP Files Class: Manage uploaded files and serve them for download

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 120 All time: 9,510 This week: 488Up
Version License PHP version Categories
files-utilities 1.0.0BSD License5PHP 5, Files and Folders
Description 

Author

This package can manage uploaded files and serve them for download.

It can validate uploaded files to check if they contain the accepted MIME types, the size are within the limit, and handle multiple uploaded files. The valid files can be moved to a separate directory.

The class can also serve given server side files for download with the appropriate HTTP headers.

Picture of alismith
  Performance   Level  
Name: alismith <contact>
Classes: 2 packages by
Country: Iran Iran

Example

Class usages :
define("DEFAULT_LANGUAGE","EN");
define("BASE_UPLOAD_DIR","files/");


include('files.php');


/* Save Image and resize to 120*120 and convert to jpg */
if(isset($_FILES['file']) && !empty($_FILES['file'])){

$validation = ["file"=>"mime=png|max-size=300000|multiple=true"];
$options = ["convert" => "jpg", "resize" => "120*120", "overwrite" => true];

$file = new \Utilities\File\Files('file',$validation);
$result = $file->store('/',false,$options);

echo '<pre>';
print_r($result);

}



/* Move Destination Image (not include BASE_UPLOAD_DIR) */

if(array_key_exists('file',$result['success'])){
$localfile = "files/".$result['success']['file'][0];
$file = new \Utilities\File\Files();
if($file->load($localfile)->moveTo('moved_file/')){
echo "<br/>\n File successfully moved!";
}else{
echo "<br/>\n File transfer failed!";
}
}


/* More Example : */
$file = new \Utilities\File\Files('profile');
$profile_image = $file->get(); // get() OR first()
if($profile_image->ext() == 'jpg'){
$file = new \Utilities\File\Files($profile_image);
$save = $file->store('images/profile',$profile_image->originalName());
}
Example 2 :
function files($file=false,$validation=false){

return new \Utilities\File\Files($file,$validation);
}

$save = files('profile')->store('/images/profile','myimg');



/* Validation Rules */

multiple
type
mime
size
min-size
max-size
width
height

Example : ['slider'=>'multiple=true|mime=jpg,jpeg|max-size=1000000']



/* Download Local File */

$file = new \Utilities\File\Files();
$file->load('path/to/file/document.docx')->download();

Example 2 : (stream download)
$file = new \Utilities\File\Files();
$file->load('path/to/file/document.docx')->download(true); //send file as stream to browser


  Files folder image Files (4)  
File Role Description
Accessible without login Plain text file document.php Example Example
Accessible without login Plain text file example1.php Example example
Accessible without login Plain text file example2.php Example example usages
Plain text file files.php Class Class file

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 0%
Total:120
This week:0
All time:9,510
This week:488Up