lat4732's avatar
Level 12

Nothing happens when trying to ->move() a file to the public folder

Hey!

I'm trying to upload files into public folder but without success. Here's what I'm trying to do:

$request->validate([
    'formNormal' => 'mimes:jpg,jpeg,png,svg',
    'formSticky' => 'mimes:jpg,jpeg,png,svg',
]);
if($request->hasFile('formNormal')) {
    $image = file_get_contents($request->formNormal);
    $image->move(public_path('assets/img'), 'logo.' . $requets->formNormal->getClientOriginalExtension());
}
if($request->hasFile('formSticky')) {
    $image = file_get_contents($request->formSticky);
    $image->move(public_path('assets/img'), 'logo_sticky.' . $requets->formSticky->getClientOriginalExtension());
}

View:

<form action="{{ url('administrator/save/logo/changes') }}" method="POST" enctype="multipart/form-data">
        <input class="form-control" name="formNormal" type="file" id="formNormal">
        <input class="form-control" name="formSticky" type="file" id="formSticky">
</form>

What am I doing wrong? Also does ->move() override if the file exists?

0 likes
13 replies
lat4732's avatar
Level 12

@Nakov That's what I'm avoiding. Why should I create a new storage for public/assets/img instead of just put the file.

Nakov's avatar

@Laralex because you'll get the full benefit of using the Storage facade and helper functions. Does it hurt?

lat4732's avatar
Level 12

@Nakov

'public_assets_img' => [
            'driver' => 'local',
            'root'   => public_path() . '/assets/img',
            'visibility' => 'public',
],
$request->validate([
    'formNormal' => 'mimes:jpg,jpeg,png,svg',
    'formSticky' => 'mimes:jpg,jpeg,png,svg',
]);
if($request->hasFile('formNormal')) {
    Storage::disk('public_assets_img')->put('logo' . $request->formNormal->getClientOriginalExtension(), file_get_contents($request->formNormal));
}
if($request->hasFile('formSticky')) {
    Storage::disk('public_assets_img')->put('logo_sticky' . $request->formSticky->getClientOriginalExtension(), file_get_contents($request->formSticky));
}

This also doesn't work.

Nakov's avatar

@Laralex by doesn't work you mean the file is not stored in the correct directory, or not stored at all?

dd($request->hasFile('formNormal'), $request->hasFile('formSticky'));

any of these returns true?

lat4732's avatar
Level 12

@Nakov Not stored at all. They are both correctly returning true/false based on if there is something inside the inputs.

Nakov's avatar

@Laralex and what about this:

$path = $request->file('formNormal')->store(
	'logo' . $request->formNormal->getClientOriginalExtension(),
    'public_assets_img'
);
1 like
lat4732's avatar
Level 12

@Nakov I still don't see an image appear in public/assets/img but at least I'm getting a success message with your code.

$request->validate([
    'formNormal' => 'mimes:jpg,jpeg,png,svg',
    'formSticky' => 'mimes:jpg,jpeg,png,svg',
]);
if($request->hasFile('formNormal')) {
    $request->file('formNormal')->store('logo' . '.' . $request->formNormal->getClientOriginalExtension(), 'public_assets_img');
}
if($request->hasFile('formSticky')) {
    $request->file('formSticky')->store('logo_sticky' . '.' . $request->formSticky->getClientOriginalExtension(), 'public_assets_img');
}
return back()->with('success', 'Successfully updated website\'s logo');

I'm also not getting any error when I try to upload a .zip or a .pdf.... Any idea what's wrong?

Nakov's avatar
Nakov
Best Answer
Level 73

@Laralex this is my last try:

Change the filesystem path to this:

'public_assets_img' => [
            'driver' => 'local',
            'root'   => public_path() . '/assets',
            'visibility' => 'public',
],

then use it:

if($request->hasFile('formNormal')) {
    $request->file('formNormal')->storeAs('img', 'logo.' . $request->file('formNormal')->extension(), 'public_assets_img');
}

Please or to participate in this conversation.