Skip to content

Commit bfc708f

Browse files
author
Fredrick Peter
committed
added hasError() and size() convertion fix
1 parent 82a7d96 commit bfc708f

File tree

5 files changed

+52
-17
lines changed

5 files changed

+52
-17
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
* [Get Mime Type](#get-mime-type)
3838
* [Not Empty](#not-empty)
3939
* [Is Empty](#is-empty)
40+
* [Has Error](#has-error)
4041
* [Mime Types](#mime-types)
4142
* [Useful links](#useful-links)
4243

@@ -434,6 +435,17 @@ File::has('avatar');
434435
File::isEmpty('avatar')
435436
```
436437

438+
### Has Error
439+
- Returns true or false. Check if there's an error in the upload
440+
441+
```
442+
$file = File::name('avatar')
443+
444+
if($file->hasError()){
445+
446+
}
447+
```
448+
437449
### Mime Types
438450
```
439451
'video' => ['.mp4', '.mpeg', '.mov', '.avi', '.wmv'],

src/File.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ public function driver($driver = null)
183183
$driver = Str::lower($driver);
184184

185185
// only change the default driver if found
186-
if(in_array($driver, array_keys($this->driverTypes)) && Tame::isInternetAvailable()){
186+
if(in_array($driver, array_keys($this->driverTypes))){
187187
$this->config['driver'] = $driver;
188188
}
189189

@@ -228,8 +228,8 @@ public function structure($structure)
228228
public function size($size = null)
229229
{
230230
$this->config['size'] = Tame::sizeToBytes(
231-
!empty($size) && $size >= 1024
232-
? Tame::byteToUnit($size)
231+
!empty($size) && (int) $size >= 1024
232+
? Tame::byteToUnit($size)
233233
: $size ?? '2mb'
234234
);
235235

@@ -319,6 +319,22 @@ public function getStatus()
319319
{
320320
return $this->data['status'];
321321
}
322+
323+
/**
324+
* Check if upload has an error
325+
*
326+
* @return int|null
327+
*/
328+
public function hasError()
329+
{
330+
$status = $this->getStatus();
331+
if(!empty($status) && !$status == 0)
332+
{
333+
return true;
334+
}
335+
336+
return false;
337+
}
322338

323339
/**
324340
* Get First Element of Uploads

src/Traits/FileTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public function globalConfig($message = [], $config = [], $filterError = [])
8181

8282
// Convert size to Bytes
8383
$config['size'] = Tame::sizeToBytes(
84-
!empty($config['size']) && $config['size'] >= 1024
84+
!empty($config['size']) && (int) $config['size'] >= 1024
8585
? Tame::byteToUnit($config['size'])
8686
: $config['size'] ?? '2mb'
8787
);
@@ -96,7 +96,7 @@ public function globalConfig($message = [], $config = [], $filterError = [])
9696

9797
// check for valid driver type
9898
// only change the default driver if found
99-
if(in_array($config['driver'], array_keys($this->driverTypes)) && Tame::isInternetAvailable()){
99+
if(in_array($config['driver'], array_keys($this->driverTypes))){
100100
$config['driver'] = $config['driver'];
101101
}
102102

src/Traits/FileValidatorTrait.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,11 @@ private function proceedToValidate()
6868
{
6969
// error tracking
7070
$errors = [];
71+
$validationAttempt = false;
7172

7273
// initilize configuration if not called
7374
$this->callConfigIfNotCalled();
7475

75-
7676
/**
7777
* Select file to upload. - error 401
7878
*/
@@ -90,7 +90,7 @@ private function proceedToValidate()
9090

9191
// begin multple validation
9292
else{
93-
93+
$validationAttempt = true;
9494
foreach($this->fileItemsData() as $key => $file){
9595

9696
// Mime types
@@ -180,11 +180,13 @@ private function proceedToValidate()
180180

181181
// if no error was found in the error array
182182
if(empty($errors)){
183-
$this->data = [
184-
"status" => 200,
185-
"message" => $this->translation('200'),
186-
];
187-
$this->success = true;
183+
if($validationAttempt){
184+
$this->data = [
185+
"status" => 200,
186+
"message" => $this->translation('200'),
187+
];
188+
$this->success = true;
189+
}
188190
}
189191

190192
return $this;

test/index1.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,19 @@
2424

2525

2626
// or more methods chaining
27-
File::name('avatar')
27+
File::name('avatar2')
2828
->limit(3)
2929
->mime('files')
30-
->driver('s3')
30+
// ->driver('s3')
3131
->folder('public/images')
3232
->validate()
3333
->save();
3434

3535

3636
// closure/callable on save() and validate()
3737
$upload = File::name('banners')
38+
->size('50kb')
39+
->filterError([401])
3840
->validate()
3941
->save(function($response){
4042

@@ -84,9 +86,12 @@
8486
Upload file
8587
</h3>
8688

87-
<div style="background: #f7b9b9; margin: 0 auto 50px; width: 100%; max-width: 600px; padding: 20px; font-size: 18px">
88-
<?= $upload->getMessage(); ?>
89-
</div>
89+
<?php if($upload->hasError()) {?>
90+
<div style="background: #f7b9b9; margin: 0 auto 50px; width: 100%; max-width: 600px; padding: 20px; font-size: 18px">
91+
<?= $upload->getMessage(); ?>
92+
</div>
93+
<?php } ?>
94+
9095

9196
<!--file upload-->
9297
<div class="col-sm-12 mt-3" style="margin: 0 0 30px;">

0 commit comments

Comments
 (0)