-
Notifications
You must be signed in to change notification settings - Fork 3
Upload
Benjamin Khalife edited this page Apr 9, 2021
·
17 revisions
<php
namespace app\controllers;
// use upload
use webrium\core\Upload;
class uploadController{
function getFile(){
$file = new Upload('file');
$save_path = storage_path('files');
$file->path( $save_path )->save();
}
}
- exsist()
Check for file availability
if($file->exsist()){
// ...
}
- path(yourPath)
You can save the file in the desired path
$file->path(__DIR__.'/')->save();
- name(new_name)
Rename the file
$file
->path($path)
->name('NewFileName')
->save();
// prj/app/storage/app/NewFileName.png
- hashName()
Names the file based on its hash (md5)
..
->hashName()
->save();
// prj/app/storage/app/jsike45o24cdi3o9edkddstsse.jpg
- status()
Restores file upload status. If the file is uploaded correctly return true else return false
if( $file->status() ){
echo 'success';
}
else{
echo 'error';
}
- name()
Get the name of the uploaded file
echo $file->name();
// output : user.png
- path()
Get saved file path
echo $file->path();
// output : prj/app/storage/app/
- size()
Get file size
echo $file->size();
// output : 5320342
- type()
Get file type
echo $file->type();
// output : image/jpeg
- extension()
Get file extension
echo $file->extension();
// output : jpg
- maxSize(int)
Limit the size of incoming files
->maxSize(1024) // 1 MB = 1024 KB
->save();
- allowExtensions(array[strings])
Upload limit by file extension
->allowExtensions(['png','jpg','jpeg'])
->save();
- allowType(array[strings])
Upload limit by file type
->allowTypes(['image/jpeg'])
->save();
You can write warning custom messages like this
$image = new Upload('image');
$path = public_path('images');
$image->path($path);
$image->maxSize(1024,'Your photo size is more than @maxSize KB'); // out:Your photo size is more than 1024 KB
$image->allowTypes(['image/jpeg'],'The file is incorrect');
$image->save();
if($image->status() == false){
echo $image->getFirstError();
}
else{
echo 'Successful';
}
You can upload several different files as arrays
To do this, we use the get() method
$files = new Upload('files');
$files->allowExtensions(['png','jpg','zip','rar']);
$path = public_path('files');
foreach( $files->get() as $file){
$status = $file->path($path)->hashName()->save();
if (! $status) {
echo "error : ".$file->getClientOriginalName()." message : ".$file->getFirstError()."<br>";
}
}
- getClientOriginalName()
Gives the original file name