File system save complication

Over the past few weeks I have been working towards a plugin which saves  some things for me.

$file = new ElggFile();
$file->owner_guid = 5001;
$file->setFilename("All Reports " . $date . ".txt");
$file->open('write');
$file->write('| ' . $Content . ' |');
$file->close();
//if you want a database record
$file->save();
//also save the file to us
$file = new ElggFile();
$file->owner_guid = 5001;
$file->setFilename("All Reports " . $date . ".txt");

 

if ($file->exists()) {
system_message("Report Created (" . basename("All Reports " . $date . ".txt") . ")");
$bytes = $file->grabFile();
//FOR TESTING PURPOSES, REMOVE BEFORE FINAL DEPLOYMENT
//system_message($bytes);
forward("Automated_view_script/all");
echo $bytes;
//try to send past memebers back to the report page and turn that into the next member difference



 

}
else {
register_error("Report (" . basename("All Reports " . $date . ".txt") . ") was not saved!");
forward(REFERER);
}

 

The code above will take the $content variable, save it to the database, and then proceed to save it locally for the user. I can confirm this should work, as I always get the successful system message and if I echo out Bytes, I can see the files contents.

However,

it seems that nothing actually saves, not in the default data folder, not the database, and not locally. I have been trying for many days to figure out what may be wrong, but I can not for the life of me figure it out.

My Elgg version is 1.8, does anyone see anything that might be wrong?

  • Have you already looked into mod/file/actions/file/upload.php to see an example of a file upload+saving? I would suggest to use the code there as starting point. Of course, you can leave out some of the code (e.g. dealing with thumbnail creation on image file uploads). But the rest shouldn't be that different from what you need.

  • Ive been fooling around with this since yesterday, and I am most likely doing something wrong, but I am having no real luck just yet. My Item that is saved will be just text from that page sent to variables, does the majority of said code still function? My errors seem to relate for it wanting another file to be uploaded instead of saving something.

  • Because I am unable to edit that post for some reason, I will give a quick rundown in case it may help more.

    Using the filesystem code I provided in the OP, I wish to take the input information and transfer it into a text file, this file will then be saved locally by echoing the variable attached to the grab bytes. I have consulted with some other elgg developers to get to this point, and I am under the impression that this is the correct way to do something like this.

    However, it seems that my work does everything except actually save anything anywhere. The file is created and written to, but it is not saved to the data folder or the local folder from the call. The code in the File plugin unfortunately doesn't seem applicable to this, as it saves the file to the database using a form information rather than back end code grabbing information automatically like mine does.

    Still I appreciate the help, and I am still wondering if this issue could be something I have done or if I am just experiencing bad luck.

  • The code of the files plugin action does both: it creates a file entity in the database AND saves the uploaded file in the data directory. Just study the code more thoroughly and learn what it does. It's a working example and I would suggest to stick to the way it's done there instead of trying to do it with "echo" which obviously doesn't work.

  • Well, turns out what I have been doing has worked all along. My files have been saving as blank files in the personal folder on the elgg site, to which I can open them download them and turn them into text files. While this is strange, do you happen to know if this code is correct?

    $FileName = $title . ' ' . $date;

     

    $folderName = 'reports/' . $FileName . '.txt'
    $file->setFilename($folderName);

     

    I should technically get a text file saved, no? Instead my file is just a number (173251) and has no extension itself. Thank you for the help on the other things, it was very useful. 

Beginning Developers

Beginning Developers

This space is for newcomers, who wish to build a new plugin or to customize an existing one to their liking