query('iddatadb'); if (empty($iddatadb)) { return response('ID riga non fornito', 400); } // Show the upload form return view('userarea.upload_photos_mobile', [ 'iddatadb' => $iddatadb ]); } public function upload(Request $request) { // Validation $request->validate([ 'photo' => 'required|file|mimes:jpeg,png,gif,heic,heif|max:5120', // 5MB 'iddatadb' => 'required|integer' ]); $iddatadb = $request->input('iddatadb'); $photo = $request->file('photo'); $iduserlogin = auth()->id(); // assuming Laravel authentication // Check if user exists $userExists = DB::table('auth_users')->where('id', $iduserlogin)->exists(); if (!$userExists) { return response()->json(['success' => false, 'message' => 'Utente non valido']); } // Upload folder $uploadDir = public_path('photostrf'); if (!is_dir($uploadDir)) { mkdir($uploadDir, 0755, true); } if (!is_writable($uploadDir)) { return response()->json(['success' => false, 'message' => 'La cartella photostrf non รจ scrivibile']); } // New filename $timestamp = now()->format('YmdHis'); $originalName = pathinfo($photo->getClientOriginalName(), PATHINFO_FILENAME); $extension = strtolower($photo->getClientOriginalExtension()); $newFileName = "{$iddatadb}-{$timestamp}-{$originalName}.{$extension}"; $destination = $uploadDir . '/' . $newFileName; // Move uploaded file $photo->move($uploadDir, $newFileName); // Save DB record DB::table('datadb_photos')->insert([ 'iddatadb' => $iddatadb, 'file_path' => $newFileName, 'file_name' => $newFileName, 'uploaded_by' => $iduserlogin ]); return response()->json(['success' => true, 'message' => 'Foto caricata con successo']); } }