1

I have the following code:

use Illuminate\Support\Facades\Validator;

$rules = array(
     'attachment' => 'max:1000|file|mimes:jpeg,png,jpg,gif,svg,pdf',
);

$messages = array(
     'attachment.max' => 'max error',
     'attachment.file' => 'file error',
     'attachment.mimes' => 'mimes error',
     //'attachment' => 'all errors',
);

$validator = Validator::make($this->request->all(), $rules, $messages);

I'm trying to apply the concept of "Specifying Custom Attribute Values": https://laravel.com/docs/9.x/validation#specifying-custom-attribute-values

I'm using Laravel: "laravel/framework": "^9.19"

However, it doesn't work, the messages are not identified according to the specific error, and the following is displayed:

dd($validator->messages());

Illuminate\Support\MessageBag {#1547
   #messages: array:1 [
     "attachment" => array:1 [
       0 => "The attachment failed to upload."
     ]
   ]
   #format: ":message"
}

Where is the error? Can anyone help me and explain to me the reason for this behavior?

I want to use a specific message for each type of error, I don't want to uncomment //'attachment' and not have control of where the upload is failing.

3
  • in this case it is "attachment.uploaded" => "file upload error" Commented Feb 12, 2024 at 13:18
  • Why uploaded? @N69S . I dont want this error. Commented Feb 12, 2024 at 13:49
  • if you go to lang/en/validator.php you will find all the messages for validator error and that specific message is returned in uploaded error. 'uploaded' => 'The :attribute failed to upload.', Commented Feb 12, 2024 at 14:07

1 Answer 1

1

I managed to find the reason for my problem above.

In fact, the problem is not in this code, it is in the file that I am sending to the server and, for some reason, Laravel/PHP cannot retrieve important information to validate correctly, as follows:

In the correct scenario, where it worked as I expected, the server printout is: (notice that Laravel managed to get the size and other values)

Illuminate\Http\UploadedFile {#1523 // app/Validators/OrderFileValidator.php:58
   -test: false
   -originalName: "2023-11-12 09-17-00.mp4"
   -mimeType: "video/mp4"
   -error: 0
   #hashName: null
   path: "/tmp"
   filename: "phpsItJmi"
   basename: "phpsItJmi"
   pathname: "/tmp/phpsItJmi"
   extension: ""
   realPath: "/tmp/phpsItJmi"
   aTime: 2024-02-12 10:33:26
   mTime: 2024-02-12 10:33:26
   cTime: 2024-02-12 10:33:26
   inode: 3805560
   size: 1906989
   perms: 0100600
   owner: 33
   group: 33
   type: "file"
   writable: true
   readable: true
   executable: false
   file: true
   dir: false
   link: false
}

Illuminate\Support\MessageBag {#1547
   #messages: array:1 [
     "attachment" => array:2 [
       0 => "The maximum file size is 1 mb (megabyte)"
       1 => "The extensions allowed for the file are: jpeg,png,jpg,gif,svg,pdf"
     ]
   ]
   #format: ":message"
}

Now, for the invalid file (the reason for opening this question), Laravel/PHP read the following values: (Note that it can't get the file size and several other attributes)

Illuminate\Http\UploadedFile {#1523 // app/Validators/OrderFileValidator.php:58
   -test: false
   -originalName: "Presentation - José Victor_20240125_070758_0000.pdf"
   -mimeType: "application/octet-stream"
   -error: 1
   #hashName: null
   path: ""
   filename: ""
   basename: ""
   pathname: ""
   extension: ""
   realPath: "/var/www/html/app/public"
   aTime: 1969-12-31 21:00:00
   mTime: 1969-12-31 21:00:00
   cTime: 1969-12-31 21:00:00
   inode: false
   size: false
   perms: 00
   owner: false
   group: false
   type: false
   writable: false
   readable: false
   executable: false
   file: false
   dir: false
   link: false
}

dd($_FILES);
array:1 [
   "attachment" => array:5 [
     "name" => "Presentation - José Victor_20240125_070758_0000.pdf"
     "type" => ""
     "tmp_name" => ""
     "error" => 1
     "size" => 0
   ]
]

Here is also the form I am using to send the file:

<form action="http://localhost:8080/app/public/..." method="POST" enctype="multipart/form-data">
     <input type="file" class="form-control" name="attachment" required="">
     ...
</form>

My only question is: WHAT NOW? WHAT IS PHP'S CRITERIA FOR READING A FILE AND NOT READING ANOTHER?

Is there a PHP configuration file where I limit something like this?

Sign up to request clarification or add additional context in comments.

2 Comments

it's not PHP itself. The server take care of the upload (in system temporary folder) and then provides it to PHP or other. Your issue is that the upload is not successful. It might be a size issue (check your apache/nginx/php.ini config) or a breakage in the connexion.
The message was probably accurate, the file was not uploaded successfully meaning it didn't end up on the server. The reason for that is probably harder to determine because there are a lot of moving parts in the file upload process (e.g. browser, webserver, storage path permissions)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.