Unable to save H5P export file -- error message

I am getting this error message on a newly installed instance of H5P on my new WordPress 4.6.1 site. 

"Unable to save H5P export file"

Along with this, and possibly related, some of my content is not saving correctly. eg. in an Accordion, the Text for the latest item is not saved. It will only be saved when I create a subsequent item after it. 

H5P library is pretty recent. I only just installed H5P yesterday and I don't have much content on this instance yet. Accordion library version is 1.0.7.

Are these related? Is there an easy fix? I have so little content on there, should I just uninstall and reinstall H5P again? 

If it is helpful, I do have direct SQL access to the underlying wp_h5p-* tables so I can go poking around in there for diagnostics. 

tim's picture

Hi, thanks for reporting this bug.

If you could poke around your table for addtional error logs and perhaps see if there are any msql and php logs. It would speed up the debugging process :)

Had a quick look. Within the wp_h5p_* tables, nothing that looks like an error log. There is a second set of tables, wp_2_h5p_*, that are empty - I believe that these are there for the secondary subdomain site that I have not yet set up. 

Just to clarify this error message, if it is not one that is well known... I was not trying to export from H5P. This message comes up each time I simply edit one of the two widgets I have created so far. 

tim's picture

Hi, it may have been an issue with your installation. Could you try uninstalling and reinstalling H5P? 

Well the uninstall/reinstall did partially fix the problem. I can now add new items to the Accordion without the widget complaining, which was the main concern. So am happy about that. 

I still get the error message 'Unable to save H5P export file'. It is there, without any attempt to export. To be clear, this error message may be completely unrelated to the main saving error that has now been fixed... and now it appears more likely. So apologies for the confusion. Anyway if I come across any other oddities or new error messages, I will let you know. 

Gaah. Back to where we started. Accordion widget is acting up again. Frequently does not save newly added text details. There is a consistent pattern, which I can describe if it is helpful. 

Other widgets so far seem to be functioning ok when creating/editing. 

Given the error message about export, I tried downloading a couple of widgets. I get this error message instead so this might provide useful diagnostic info:

The requested URL /wp-content/uploads/h5p/exports/doc-tool-test-3.h5p was not found on this server.

When I use CyberDuck to explore the contents of the 'exports' folder, it is indeed empty. Any other folders you want me to take a look at in my installation? 

tim's picture

Ok great. Thanks for the very thorough explanation. I've made an issue on the taskboard of the Core Team and we'll have a deeper look at it in the coming weeks. You can track it here: 

https://h5ptechnology.atlassian.net/browse/HFP-203

One of us will update this thread if when we figure out how to resolve this bug. 

Same issue here on Drupal with the latest updates. 

icc's picture

Note that this message is probably due to the improved error handling in the latest update. So, the export may have been broken on your site for a while. Disabling export trough the H5P Settings page should make this message go away, but also your download button.

The problem is probably caused by file permissions in the H5P folder(sites/default/files/h5p for Drupal OR wp-content/uploads/h5p for WordPress). You must ensure that the user running the web server/PHP has write access to all of the sub-folders.

Thanks muchly for those suggestions. However, they did not help (unless I did things wrong). 

I made the h5p folder and all its subfolders chmod 777 (something I am not entirely comfortable with but for the sake of testing...). However, it did not get rid of the error message and I was still unable to download a H5P widget - this generated the same error message. 

None of this has helped the problem where text details for accordion elements are frequently not saved. The last element is consistently not saved, no matter how many times you do it. You can only force a save by adding another element on below it. Missing text details from earlier elements in the accordion stack are saved when you edit them a second time. (I mention again that these two problems may not be related.)

Anyway, hope this helps. Any other suggestions? 

icc's picture

It could be related to the default permission set when PHP creates the export file. To verify this we can add a line of code that stops the process before it copies the file, and then you can manually check the owner and permissions of the export file. To do this you'll have to insert the following line right before line 144 of wp-content/plugins/h5p/h5p-php-library/h5p-default-storage.class.php:

print 'source: ' . $source . '<br/>target: ' . "{$this->path}/exports/{$filename}"; exit;

Now, you can try to save some H5P content and it should print the paths it's trying to use when copying. Use a file browser or shell to investigate the owner and permissions of the source. Also, double check that the target folder exists and has the appropriate permissions.

If the source is missing something else goes wrong before the copy. If it is indeed the file permissions, you could try adding umask(0777); at the top of the getTmpPath() function in the same file.

Let me know how it goes.

I am now overseas for two weeks so may not be able to do much in the way of such tweaking. If I get a change, I'll let you know how it goes. 

Ok, I'm back now. 

I just tried some of your suggested mods. I have not programmed in PHP so please forgive any ID10T errors I have made.

I first added the lines …

print 'source: ' . $source . '<br/>target: ' .

"{$this->path}/exports/{$filename}";

exit; 

…just before line 144 as directed. This did print a source and target. First thing I noticed was that the source file does not exist. I thought at first that this might be because of the exit; clause but it still is not created when I remove the lines again. There was another temporary folder within the uploads/h5p/temp folder called h5p-5829d5b399d81, which I presume is left over from a previous operation. File date is 14-Nov-2016, not today. I checked that temp folder and its owner is listed as Unknown, with permissions 755. The temp folder has the same Unknown owner and 755 permissions. 

When I looked at wp-content/uploads folder, I noted that on this server, it is also owner Unknown and permissions 755. When I compared this with another WordPress instance that I have running on another server, it has 777 permissions for the wp-content/uploads folder. I changed the wp-content/uploads folder on this troublesome server to the same permissions but has not fixed the problem. Maybe I need to propagate the permissions down the folder trees. 

I also tried adding the umask(0777) to the getTempPath function. However that simply prevented me from doing anything with H5P at all so I probably did this wrong. I have attached my modified h5p-default-storage.class.php so you can see how I screwed up. Sorry about that. 

I just tried to update H5P plugin and got an error about file permissions so this apparently goes beyond the export problem above. I am happy to change the folder permissions - I just need some guidance about which folders need what, and also do I need to change file/folder ownership? 

icc's picture

You did a good job debugging. I think the issue is that the user running PHP/Apache might be creating directories that it doesn't have permission to write to or modify. Could you try adding the following before line 144 instead of the previous code? 

    $processUser = posix_getpwuid(posix_geteuid());
    print 'User running PHP: ' . $processUser['name'] . '<br/>';
    print 'Source "'.$source.'": ' . (is_writable($source) ? 'Exists and is writable' : (file_exists($source) ? 'Exists but isn\'t writable' : 'Does not exist'));
    if (file_exists($source)) {
      $fileUser = posix_getpwuid(fileowner($source));
      print ' – Owner: ' . $fileUser['name'] . '<br/>';
    }
    $target = "{$this->path}/exports";
    print 'Target folder "' . $target . '": ' . (is_writable($source) ? 'Exists and is writable' : (file_exists($source) ? 'Exists but isn\'t writable' : 'Does not exist'));
    if (file_exists($target)) {
      $fileUser = posix_getpwuid(fileowner($target));
      print ' – Owner: ' . $fileUser['name'] . '<br/>';
    }
    exit;

It should print which user is running PHP and which user owns the files that are created. Perhaps we get a better idea of what's going on.

If you need to reset the file you should be able to grab the original here: https://raw.githubusercontent.com/h5p/h5p-php-library/wp-1.7.7/h5p-default-storage.class.php

Hi there,

I'm wondering if there has been any more update on this issue?

We are using WordPress and the same error started to pop up after we upgraded the plug-in and the libraries to the latest release in December.

We are no longer able to export any .h5p files.

I should add however that we did at one point disable the option in H5P settings to show the download button in action bar. Although it was re-enabled now things remain broken.

What can we do? Would a reinstallation be helpful?

icc's picture

Are you getting the "Unable to save H5P export file" error message as well or is it just the download button that is gone? Are you getting any other error messages in your web server's error_log?

I think this is most likely related to the host setup and not the plugin itself as I'm not able to reproduce this for other WordPress sites.

We are getting the error message but the Download button is acaully present even though clicking on it will produce a "not found" error page stating the .h5p didn't exist.

I'm uploading a screenshot of the error message we saw. Also attached is our error log file.

Thank you for responding to this.

icc's picture

I see… that is strange. Could you verify the permissions for the folder inside the wp-content/uploads/h5p/ folder?

It would appear that the user running PHP no longer have write access to one or more of the folders there.