Jump to content

How to rename a file properly?


Valery
 Share

Recommended Posts

Hey guys,

I was doing some file renaming and stumbled upon the following problem:

rename() renames only the physical file itself but in the Admin backend it still shows up under its old name.

Can anyone point out for me what I am doing wrong, please?

Link to comment
Share on other sites

You can't just rename them physically and then think PW magically still knows which file it is. This is because PW saves the name of the file to the db. It's like the key!

So you'd also have to rename it in the DB table field_yourfieldname also and you'll see.

  • Like 3
Link to comment
Share on other sites

Or a bit of API vodoo.

$old_name = 'lizard.jpg';
$new_name = 'wizard';

$p = $pages->find("image.data=$old_name")->first;
$p->of(false); // outputformatting off, single and multiple image fields are from now on wire array's

$path           = $p->image->path;
$page_image     = $p->image->first();
$ext            = $page_image->ext;
$orig_image     = $page_image->filename
$new_image      = $path . $new_name . "." . $ext;

echo "orig: " . $orig_image . "<br/>";
echo "path: " . $path . "<br/>";
echo "ext: " . $ext . "<br/>";
echo "new: " . $new_image . "<br/>";

// copy and rename
copy($orig_image, $new_image);

// remove old, add new image to page
$p->image->remove($page_image); // orig, will get deleted
$p->image->add($new_image); // new
$p->save(); 

Or in a shorter code: https://gist.github.com/somatonic/6282944

  • Like 5
Link to comment
Share on other sites

I think it also could be a bit shorter because of the $image->rename() method:
 

$old_name = 'lizard.jpg';
$new_name = 'wizard';

$p = $pages->find("image.data=$old_name")->first;
$p->of(false); // outputformatting off, single and multiple image fields are from now on wire array's

// get the image and the extension
$page_image     = $p->image->first();
$ext            = $page_image->ext;

// pathes and old file basename aren't needed because we cannot leave the pages assets directory!
$newFilename    = $new_name .'.'. $ext;

// only rename, copy and remove isn't needed
$page_image->rename($newFilename);
$p->save();

 
only written in browser, not tested, but should work, i think.
 
EDIT:
 
Have now tested and I'm afraid too: - it doesn't work! :(

Edited by horst
Link to comment
Share on other sites

Ahhh Mr. ImageHorstManipulation :P

No it doesn't work I'm afraid to say. Also the image variations doesn't get deleted and the image will not be changed in DB.

Oh no, - so - for what is the ->rename() method useful ?

Link to comment
Share on other sites

I looked closer and it doesn't work as simple, you have to call trackChange on image field for basename and remove variations too.

So this will work:

$old_name = 'wizard.jpg';
$new_name = 'lizard';

$p = $pages->get("image.data=$old_name");
$p->of(false); // outputformatting off, single and multiple image fields are from now on wire array's

// get the image and the extension
$image          = $p->image->find("name=$old_name")->first();
$ext            = $image->ext;

$newFilename    = $new_name . "." . $ext;

$p->image->trackChange("basename");
$image->removeVariations();
$image->rename($newFilename);

$p->save("image");
  • Like 4
Link to comment
Share on other sites

Thanks, Soma!

Now how would one go about renaming a file? Calling get("file_field.data=...) did not seem to work for me.

It's the same, as image field is just extending file fieldtype.

Link to comment
Share on other sites

Soma,

My bad. I must admit I rather blindly copied your code which was not a good thing.

I removed the removeVariations() line and the trick with trackChanges() worked.

Thanks! It's a lot more elegant way to rename files than the "add/delete" way.

Have a very nice day ;)

  • Like 1
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...