Old data on Frontend

I have a template and a controller file. (simplified)

Template:

<?php
$dateien = $page->files();
foreach($dateien as $d):
  if($d->open_file()->toBool()):
   $pos              = strpos($d->filename(), '_');
   $originalSafename = substr($d->filename(), $pos + 1);

   if($d->altname() != ""){
    $anzeigename = $d->altname().'.'.$d->extension();
   }
   else{
    $anzeigename = $originalSafename;
   }
  ?>
  <a href="<?=$d->url()?>"><?=$anzeigename?></a>
  <?php endif ?>
<?php endforeach ?>

<form method='post'>
  <label for="rename">neuer Dateiname</label>
  <input type='text' name='newname' id='newname'>
  <input type='hidden' name='name' id='name'>
 <button type='submit' class='btn' name='rename' id='rename' value='rename'>umbenennen</button>
</form>


Controller

<?php
return function($kirby, $page){

  $files = $page->files();
  if($kirby->request()->is('post') === true){
   if(get('rename')){
    $newname = get('newname');
    try{
     $files = $page->files()->find(get('name'))->update(['altname' => $newname]);
     $success = 'Datei umbenannt.';
    }
    catch(Exception $e) {
     $error = $e->getMessage();
    }
   }
  }
return compact('user', 'error', 'success');
};?>

The problem is, this code renamed the “file”(the alternative name which comes from the blueprint.) but still shows the old name. When I update the website, the new one will be displayed.

Hm, your code looks a bit weird and doesn’t work for me at all as it is. Maybe it is uncomplete? Where does the hidden input field get its value from and what file is this supposed to work on if the form itself is not within the loop?

This works for me and instantly refreshes the filename:

Controller:

<?php
return function ($kirby, $page) {


    if ($kirby->request()->is('post') === true) {
        if (get('rename')) {
            $newname = get('newname');
            // always check if your object exists first!
            if ($file = $page->files()->find(get('name'))) {
                try {
                    $newFile = $page->files()->find(get('name'))->update(['altname' => $newname]);

                } catch (Exception $e) {
                    $error = $e->getMessage();
                }
            }
        }
    }

    $files = $page->files();

    return compact('user', 'error', 'success', 'files');
};

(Note: Never end a PHP file with a closing PHP tag).

Template:

<?php
foreach($files as $d): // we don't need a new variable here but use the $files variable we defined in the controller. Always use the variables you set in your controller, otherwise it's pretty useless to define them there in the first place.
  if ($d->open_file()->toBool()):
   $pos              = strpos($d->filename(), '_');
   $originalSafename = substr($d->filename(), $pos + 1);

   if($d->altname() != ""){
    $anzeigename = $d->altname().'.'.$d->extension();
   }
   else{
    $anzeigename = $originalSafename;
   }
  ?>
  <a href="<?=$d->url()?>"><?=$anzeigename?></a>
 


<form method='post'>
  <label for="rename">neuer Dateiname</label>
  <input type='text' name='newname' id='newname'>
  <input type='hidden' name='name' id='name' value="<?= $d->filename() ?>">
 <button type='submit' class='btn' name='rename' id='rename' value='rename'>umbenennen</button>
</form>
<?php endif ?>

<?php endforeach ?>