限制文件大小和文件类型

发布于 2025-01-16 01:36:09 字数 1233 浏览 3 评论 0原文

如何限制控制器上的文件大小和文件类型?我尝试了不同的方法,但不应该起作用。

这是我的控制器:

public function add_presentation_detail(Request $request){
$start_date     = $request->start_date;
$input_file     = $request->file;

$nik    = Sentinel::getUser()->nik;

$data   = AccelerateMentee::select(
    'accelerate_mentee.*',
    'accelerate_rotation.start_date',
    'accelerate_rotation.id as id_rotation'
)
->leftJoin('accelerate_rotation', 'accelerate_rotation.id_mentee', '=', 'accelerate_mentee.id')
->where([
    ['accelerate_mentee.nik', '=', $nik],
    ['accelerate_rotation.status', '<>', 'approved']
])
->first();

if (!empty($request->file) && $request->hasFile('file')) {
    
    $filename       = $input_file->getClientOriginalName();
    $new_filename   = "presentation_" . "-" . $filename;
    $upload_file    = $input_file->storeAs('public/accelerate/',$new_filename);

} else {

    $new_filename = null;

}
$update = AccelerateRotation::where('id', '=', $data->id_rotation)
->update([
    'status'            => 'submitted',
    'panel_time_start'  => $start_date,
    'file'              => $new_filename,
]);

return redirect()->back();

谢谢你,我现在很困惑。

how can I limit the file size and file type on my Controller? I'm tried different ways but shouldn't work.

This is my Controller :

public function add_presentation_detail(Request $request){
$start_date     = $request->start_date;
$input_file     = $request->file;

$nik    = Sentinel::getUser()->nik;

$data   = AccelerateMentee::select(
    'accelerate_mentee.*',
    'accelerate_rotation.start_date',
    'accelerate_rotation.id as id_rotation'
)
->leftJoin('accelerate_rotation', 'accelerate_rotation.id_mentee', '=', 'accelerate_mentee.id')
->where([
    ['accelerate_mentee.nik', '=', $nik],
    ['accelerate_rotation.status', '<>', 'approved']
])
->first();

if (!empty($request->file) && $request->hasFile('file')) {
    
    $filename       = $input_file->getClientOriginalName();
    $new_filename   = "presentation_" . "-" . $filename;
    $upload_file    = $input_file->storeAs('public/accelerate/',$new_filename);

} else {

    $new_filename = null;

}
$update = AccelerateRotation::where('id', '=', $data->id_rotation)
->update([
    'status'            => 'submitted',
    'panel_time_start'  => $start_date,
    'file'              => $new_filename,
]);

return redirect()->back();

Thank you, I'm so confused right now.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

┊风居住的梦幻卍 2025-01-23 01:36:09

您永远不应该使用来自用户输入的输入数据。你不能信任用户。这就是为什么你应该使用 Laravel 验证。 在这里您可以阅读有关它的更多信息。在使用文件存储之前,您应该验证用户输入。如果一切顺利,那么您应该继续并使用经过验证的输入。

$validatedData = $request->validate([
    'file' => 'required|size:1024' // Validate that an uploaded file is exactly 1024 kilobytes...

]);

然后你使用这个经过验证的数据

You should never use the input data from the user's input. You can not trust the user. That's why you should use Laravel Validation. Here you can read more about it. Before you use your file store, you should validate user input. If everything goes well then you should proceed and use this validated inputs.

$validatedData = $request->validate([
    'file' => 'required|size:1024' // Validate that an uploaded file is exactly 1024 kilobytes...

]);

Then you use this validated data

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文