Jay D Posted June 3, 2021 Share Posted June 3, 2021 Hi all, I am trying to pull in some data, and along with it one image. I would like to rename the image to something a little more human friendly, and seem to be running into a issue with the the actual rename part. The code below does not throw an error, but also does not rename the image file. $p->singleImage->add($remoteUrl); //grab the image. $p->save(); $image = $p->singleImage->first(); //load up image that was just saved. $newFilename = $parentName . '-' . $childName . '.' . strtolower($image->ext()); $p->singleImage->rename($image, $newFilename); $p->save(); //save changes to page. Any help on what is going wrong would be amazing! Thank you in advance, Jay Link to comment Share on other sites More sharing options...
rick Posted June 4, 2021 Share Posted June 4, 2021 Do you have output formatting off prior to working with the page data? It's not shown in your snippet, so I'm asking if it was included above the code segment you posted. Link to comment Share on other sites More sharing options...
wbmnfktr Posted June 4, 2021 Share Posted June 4, 2021 Just in case - another possible way to handle things like this: https://processwire.com/modules/process-custom-upload-names/ Maybe not right now in your case but within larger projects this module comes in handy. Link to comment Share on other sites More sharing options...
Jay D Posted June 4, 2021 Author Share Posted June 4, 2021 12 hours ago, rick said: Do you have output formatting off prior to working with the page data? It's not shown in your snippet, so I'm asking if it was included above the code segment you posted. I'm not sure what you mean by formatting off? How does that apply? Link to comment Share on other sites More sharing options...
rick Posted June 4, 2021 Share Posted June 4, 2021 From your segment... $p->of(false); // output formatting should be off prior to editing page data. $p->singleImage->add($remoteUrl); //grab the image. $p->save(); $p->of(false); // page save resets output formatting, so turn it off again. $image = $p->singleImage->first(); //load up image that was just saved. $newFilename = $parentName . '-' . $childName . '.' . strtolower($image->ext()); $p->singleImage->rename($image, $newFilename); $p->save(); //save changes to page. Link to comment Share on other sites More sharing options...
BillH Posted June 4, 2021 Share Posted June 4, 2021 A couple of good discussions about understanding output formatting: Link to comment Share on other sites More sharing options...
Jay D Posted June 4, 2021 Author Share Posted June 4, 2021 Thanks all for the output formatting, makes a lot of sense, and will now include in updates to page. I actually was using it when creating the page, just not when saving again. I tested the following code and there was no difference: $p->of(false); $p->singleImage->add($remoteUrl); //grab the image. $p->save(); $image = $p->singleImage->first(); //load up image that was just saved. $newFilename = $parentName . '-' . $childName . '.' . strtolower($image->ext()); $p->of(false); $p->singleImage->rename($image, $newFilename); $p->save(); //save changes to page. File name was not changed, in either the file system or on the page's record. Then I changed the target of the renaming from the field, to the actual file, this did rename in the file system, but did not update the image field in the db: $p->of(false); $p->singleImage->add($remoteUrl); //grab the image. $p->save(); $image = $p->singleImage->first(); //load up image that was just saved. $newFilename = $parentName . '-' . $childName . '.' . strtolower($image->ext()); $p->of(false); //$p->singleImage->rename($image, $newFilename); $image->rename($newFilename); $p->save(); //save changes to page. When I use the `$p->singleImage->rename($image, $newFilename);` method it seems to expect the image object not just the file name, which is not what the docs say. Any ideas on this? Link to comment Share on other sites More sharing options...
Jay D Posted June 4, 2021 Author Share Posted June 4, 2021 Ok, got it. There needs to be two actions to rename a image in the file system, and in the page's record: $p->of(false); $p->singleImage->add($remoteUrl); //grab the image. $p->save(); $image = $p->singleImage->first(); //load up image that was just saved. $newFilename = $parentName . '-' . $childName . '.' . strtolower($image->ext()); $p->of(false); $image->rename($newFilename); $p->singleImage->rename($image, $newFilename); $p->save(); //save changes to page. First, rename the image in the file system: $image->rename($newFilename); Then make the rename to the page/image record: $p->singleImage->rename($image, $newFilename); That works, and now my images have the names I am looking for. Thank you all for your help! 1 Link to comment Share on other sites More sharing options...
rick Posted June 4, 2021 Share Posted June 4, 2021 Can you edit the topic as solved so others know this is the place for answers to similar issues. Link to comment Share on other sites More sharing options...
matjazp Posted June 4, 2021 Share Posted June 4, 2021 Note that rename() will not rename variations... you should track changes on basename: $p->singleImage->trackChange("basename"); $image->removeVariations(); $image->rename($newFilename); $p->save("singleImage"); https://processwire.com/talk/topic/4299-how-to-rename-a-file-properly/?do=findComment&comment=42213 1 Link to comment Share on other sites More sharing options...
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