Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ibrahim_bilal's avatar

file path return as .tmp when using custom request

Hello guys.. i tring to upload a profile picture and save the path into database.. but when i using custom request, the file path return as .tmp. there is no problems when i using default Request class.

the $file_path variable return correct before saving it into database.

here is my code:

/**
 * Update the specified resource in storage.
 *
 * @param  \Illuminate\Http\UpdateUserRequest $request
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function update(UpdateUserRequest $request, $id)
{
	// the request will validated by 'UpdateUserRequest' class
	try {
		// redirect if user dose not exist
		$user = User::find($id);
		if (!$user) {
			return response()->json([
				'errors' => ['The User Dose Not Exist!']
			]);
		}
		// make accoount unverified when change it's status to not_verified
		if (($user->status !== $request->get('status')) &&
			$request->get('status') == 'not_verified'
		) {
			$user->forceFill([
				'email_verified_at' => null
			])->save();
		}
		// upload profile picture
		// store file path to database
		if ($request->hasFile('profile_picture')) {
			// $request->file('profile_picture')->move(public_path('uploads/users/'), $request->file('profile_picture')->getClientOriginalName());
			// $user->profile_picture = 'uploads/users/' . $request->file('profile_picture')->getClientOriginalName();
			// $user->save();
			$file = $request->file('profile_picture');
			$file_name = $file->hashName();
			$file_path = $file->storeAs('users', $file_name, 'local');
			$user->profile_picture = $file_path;
			$user->save();
		}
		// don't update password if user didn't change it
		if (is_null($request->get('current_password')) && is_null($request->get('password'))) {
			$user->update(
				$request->safe()->except(['password', 'current_password', 'password_confirmation'])
			);
		} else {
			// except this fields from update
			// this fields doesn't exist in database
			$user->update(
				$request->safe()->except(['current_password', 'password_confirmation'])
			);
		}
		return response()->json(['success' => 'Data successfully updated!']);
	} catch (\Exception $ex) {
		return response()->json([
			'errors' => ['There Is Error!']
		]);
	}

in the 'UpdateUserRequest' file i have rules only:

/**
 * Get the validation rules that apply to the request.
 *
 * @return array<string, mixed>
 */
public function rules()
{
	return [
		'first_name' => ['required', 'string', 'max:255'],
		'last_name'	=> ['required', 'string', 'max:255'],
		'email' => [
			'required', 'string', 'email', 'max:255',
			Rule::unique('users', 'id')->ignore($this->user()->id),
		],
		'current_password' 	=> ['nullable', 'current_password:web', 'required_with:password'],
		'password' => [
			'nullable', 'string', 'confirmed', 'required_with:current_password',
			'different:current_password', Password::min(8)
		],
		'mobile' => ['nullable', 'numeric', 'digits_between:9,15'],
		'birth_date' => ['nullable', 'date', 'date_format:Y-m-d', 'before_or_equal:' . date("Y-m-d", strtotime('-18 years'))],
		'gender' => ['required', Rule::in(['male', 'female'])],
		'role_id' => ['required', 'numeric', Rule::exists(Role::class, 'id')],
		'status' => ['sometimes', 'string', Rule::in(['not_verified', 'verified', 'blocked'])],
		'language' => ['required', 'string'],
		'profile_picture' => [
			File::image()
				->types(['jpeg', 'jpg', 'png'])
				->max(1024)
				->dimensions(Rule::dimensions()->maxWidth(500)->maxHeight(500)),
		],
	];
}

any ideas to resolve this issue

0 likes
3 replies
MohamedTammam's avatar

To get the original name use getClientOriginalName

$file_name = $file->getClientOriginalName();

However, you shouldn't store the file with its original name because if the some other user upload a file with the same name, the new one is going to replace the old one. Use the unique file name that Laravel generates by default when you upload a file.

if ($request->hasFile('profile_picture')) {
    $file = $request->file('profile_picture');
    $file_path = $file->store('users', 'local');
    $user->profile_picture = $file_path;
    $user->save();
}
ibrahim_bilal's avatar

Hello @MohamedTammam thanks for your reply..

i don't have problem with naming of the file.. my problem is when i saving the file path into database, it doesn't save as correct.

$file_path = $file->storeAs('users', $file_name, 'local');
dd($file_path); // return users/moRzGuPNQa9MdXeNKRr2JeiJpCpMzzbSmqMaHyft.jpg

but when i save it into database the result have been:

E:\xampp\tmp\php1840.tmp

I tried to upload the file by all available methods, but the the result it is same

ibrahim_bilal's avatar

Problem solved guys I should have excluded the field 'profile_picture' from updating because the field value was overwritten

$request->safe()->except(['password', 'current_password', 'password_confirmation', 'profile_picture'])
$request->safe()->except(['current_password', 'password_confirmation', 'profile_picture'])

Please or to participate in this conversation.