Why is the file saved in the owner's folder but not in its own folder?

Hello.

I'm trying to create a file not from the local storage:

$temp = new \ElggTempFile();
$temp->open('write');
$temp->write($content);
$temp->close();

$file = new \ElggFile();
$file->owner_guid = $user->owner_guid;
$file->container_guid = $user->guid;
$file->access_id = $user->access_id;
$filename = elgg_strtolower('file/' . time() . 'file.jpg');
$file->setFilename($filename);
$file->filestore_prefix = 'file';

$file->open('write');
$file->write($content);
$file->close();

$file->originalfilename = 'file.jpg';
$file->title = 'file.jpg';

$mime_type = _elgg_services()->mimetype->getMimeType($file->getFilenameOnFilestore());
$file->setMimeType($mime_type);
$file->simpletype = _elgg_services()->mimetype->getSimpleType($mime_type);


if (!$file->save()) {
    $file->delete();
    $temp->delete();
} else {
   $file->saveIconFromElggFile($temp);
   $temp->delete();
}
This works, but the files are not saved in the folder under its GUID, but in the folder under the owner's GUID:
 
/data/1/40/file/file.jpg
 
However, the icons are served in the normal directory:
 
/data/1/1090/icons/icon/master.jpg
 
I'm trying with elgg_call(ELGG_IGNORE_ACCESS | ELGG_SHOW_DISABLED_ENTITIES, function ()  also but not success.
 
What is wrong?
 
Thank you in advance.
  • Nothing is wrong this is completely expected behavior.

    You set $file->owner_guid to $user->owner_guid thus the file contents gets saved there. When generating icons we try to keep them with the entity the icons relate to. In this case the $file.

    You can use the \ElggFile to store information on disk and while doing that the \ElggFile doesn't need to have a guid yet, same as in your example. You created the \ElggFile wrote some data to disk and than saved it. Before the save there was no GUID for the \ElggFile. You also can't make the \ElggFile the owner of itself this can cause lots of problems.

    ps: always try to mention which Elgg version you're using, this can help others give the correct advice/solution

  • Thanks for your reply.

    Unfortunately, nothing helped.

    I tried to do this:

    $file = new \ElggFile();
    $file->owner_guid = $user->guid;
    $filename = elgg_strtolower('file/' . time() . 'file.jpg');
    $file->setFilename($filename);
    $file->filestore_prefix = 'file';
    
    $file->open('write');
    $file->write($content);
    $file->close();
    
    

    There was a typo in the previous example not $user->owner_guid but $user->guid.

    But that didn't help either.

    I'm using Elgg 5.1.9

  • As Jerome said, setting

    $file->owner_guid = $user->guid;

    is the reason.

    Have you tried NOT setting owner_guid at all?

    It also seems you're not setting container_guid in the code of your latest post. I guess you could still set

    $file->container_guid = $user->guid;

    to preserve the owner information after omitting to set owner_guid.

  • Thanks, iionly.

    I tried doing this with different variations of the code.
    For example, this one:

    $file = new \ElggFile();
    $filename = time() . 'file.txt';
    $file->setFilename($filename);
    $file->filestore_prefix = 'file';
    
    $file->open('write');
    $file->write('Test');
    $file->close();

    But the file is still saved in the logged in user's folder, but not under its own GUID