rick Posted July 25 Posted July 25 (edited) Two things first: 1) My searches returned no results for this issue. 2) I don't use github so I do not know how to submit a possible issue. So I would like to ask the community if I have either made a mistake (which is most likely) or have an actual issue; In which case I hope one of you more familiar with github can submit this as an issue. I've create a user profile page whereby the user can upload an avatar image. It works to the point of actually saving the image file in the correct location with the correct filename, but does not assign that to the image field due to the issues noted later. This is the code: ... $upload_path = wire()->config->paths->assets . "files/avatar_uploads/"; if(!is_dir($upload_path)) { if(!wireMkdir($upload_path)) throw new WireException("No upload path!"); } try { $user->first_name = $first_name; $user->last_name = $last_name; $user->user_bio = $user_bio; $user->share_bio = $share_bio; $files = new WireUpload('avatar'); $files->setDestinationPath($upload_path); $files->setTargetFilename($user->name."-avatar"); $files->setValidExtensions(['jpg','jpeg','png']); $files->setOverwrite(true); $file = $files->execute(); //returns array of uploaded filenames if($files->getErrors()){ foreach($file as $filename) @unlink($upload_path.$filename); ... } $user->avatar->removeAll(); $user->avatar = $upload_path.$file[0]; ... $user->save(['quiet'=>true]); The result of which issues two messages: Quote Warning: Undefined array key "extension" in .../wire/core/WireUpload.php on line 715 Deprecated: basename(): Passing null to parameter #2 ($suffix) of type string is deprecated in .../wire/core/WireUpload.php on line 715 I 've changed the WireUpload.php (I know, but it was a fast fix) to the following: protected function getTargetFilename($filename) { if(!$this->targetFilename) return $filename; // Parse incoming filename $pathInfo = pathinfo($filename); $extension = isset($pathInfo['extension']) ? $pathInfo['extension'] : ''; // Parse the set targetFilename $targetPathInfo = pathinfo($this->targetFilename); $targetExtension = isset($targetPathInfo['extension']) ? $targetPathInfo['extension'] : ''; // Strip off the target’s extension (if any) $base = basename( $this->targetFilename, $targetExtension !== '' ? '.' . $targetExtension : '' ); // Re‑append the incoming file’s extension (if any) return $base . ($extension !== '' ? '.' . $extension : ''); } This runs on Debian 12 Apache2, ProcessWire 3.0.246 Master, php8.4 if it matters. Thanks for your help! Edited August 2 by rick closed
Jan Romero Posted July 25 Posted July 25 (edited) Just add an extension here: $files->setTargetFilename($user->name."-avatar"); Although I might agree that PW should perhaps be able to handle it regardless. But also, why not use the field? user()->of(false); user()->avatar->deleteAll(); $field_avatar = user()->getInputfield('avatar'); $field_avatar->setMaxFilesize(500_000); $field_avatar->processInput(input()->post); if (!user()->save(['quiet'=>true])) die('oh no'); user()->of(true); Edited July 25 by Jan Romero 1 1
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now