kiko12122 Posted November 17, 2019 Share Posted November 17, 2019 Hello everyone, I am not sure if I am supposed to post this here but I have a problem with a php file of my wordpress site. Let me insert the code becaus I have no idea what is the problem - $file_data = fread( $fp, 8 * 'KB_IN_BYTES' ); where the problem shows function get_file_data( $file, $default_headers, $context = '' ) { // We don't need to write to the file, so just open for reading. // Pull only the first 8 KB of the file in. $file_data = fread( $fp, 8 * 'KB_IN_BYTES' ); // PHP will close file handle, but we are good citizens. // Make sure we catch CR-only line endings. $file_data = str_replace( "\r", "\n", $file_data ); /** * Filters extra file headers by context. * * The dynamic portion of the hook name, `$context`, refers to * the context where extra headers might be loaded. * * @since 2.9.0 * * @param array $extra_context_headers Empty array by default. */ Quote Link to comment https://forums.phpfreaks.com/topic/309540-need-help-with-functionsphp-file-on-my-wordpress-site/ Share on other sites More sharing options...
requinix Posted November 17, 2019 Share Posted November 17, 2019 Thank you for including code, but how about telling us why you're doing all this? What problem are you having? Why do you think this code is responsible? Quote Link to comment https://forums.phpfreaks.com/topic/309540-need-help-with-functionsphp-file-on-my-wordpress-site/#findComment-1571646 Share on other sites More sharing options...
kiko12122 Posted November 17, 2019 Author Share Posted November 17, 2019 Hello, Well the problem is my site isnt working and many errors appear and the source of that error is always linked to line 5700 $file_data = fread( $fp, 8 * 'KB_IN_BYTES' ); ee in my wordpress files. Sorry If I sound confusing to you but here are some of the errors. One of the error is fread() expects parameter 1 to be resource, null given in Quote Link to comment https://forums.phpfreaks.com/topic/309540-need-help-with-functionsphp-file-on-my-wordpress-site/#findComment-1571648 Share on other sites More sharing options...
Barand Posted November 17, 2019 Share Posted November 17, 2019 You have this comment... 1 hour ago, kiko12122 said: // We don't need to write to the file, so just open for reading but you don't get around to actually opening the file - the comment won't do it for you. Therefore in the next line $fp has not been defined. 1 hour ago, kiko12122 said: $file_data = fread( $fp, 8 * 'KB_IN_BYTES' ); Further, you have put 'KB_IN_BYTES' inside quotes thus making it a string value (which has a numeric value of 0). So I guess the problem is in trying to read 0 bytes from a file that doesn't exist. And what is the comment about being "good citizens"? You don't close it either. (Has KB_IN_BYTES been defined as constant anywhere?) Quote Link to comment https://forums.phpfreaks.com/topic/309540-need-help-with-functionsphp-file-on-my-wordpress-site/#findComment-1571650 Share on other sites More sharing options...
kiko12122 Posted November 17, 2019 Author Share Posted November 17, 2019 So I should remove the string and remove the comments then?😀 Quote Link to comment https://forums.phpfreaks.com/topic/309540-need-help-with-functionsphp-file-on-my-wordpress-site/#findComment-1571651 Share on other sites More sharing options...
Barand Posted November 17, 2019 Share Posted November 17, 2019 Where did I say that? The comments are fine - you need to add the code that implements the comments. You also need to ensure that the constant KB_TO_BYTES has been defined and use it correctly as a constant (ie without the quotes). Quote Link to comment https://forums.phpfreaks.com/topic/309540-need-help-with-functionsphp-file-on-my-wordpress-site/#findComment-1571652 Share on other sites More sharing options...
kiko12122 Posted November 17, 2019 Author Share Posted November 17, 2019 (edited) sorry, I used the wrong term. I will remove the quotes but what code should I write for the comments? I just have no experience in coding. Or maybe these comments arent useful for me? Edited November 17, 2019 by kiko12122 Quote Link to comment https://forums.phpfreaks.com/topic/309540-need-help-with-functionsphp-file-on-my-wordpress-site/#findComment-1571653 Share on other sites More sharing options...
Barand Posted November 17, 2019 Share Posted November 17, 2019 If KB_TO_BYTES has not been defined then you need const KB_TO_BYTES = 1024; // We don't need to write to the file, so just open for reading $fp = fopen($file, 'r'); // open file for reading if ($fp) { $file_data = fread($fp, 8 * KB_TO_BYTES); fclose($fp); //close the file } Quote Link to comment https://forums.phpfreaks.com/topic/309540-need-help-with-functionsphp-file-on-my-wordpress-site/#findComment-1571654 Share on other sites More sharing options...
Barand Posted November 17, 2019 Share Posted November 17, 2019 PS there is a perfectly good function in php already which does all this for you $file_data = file_get_contents($file); Which reminds me, your function needs to return the file data. Quote Link to comment https://forums.phpfreaks.com/topic/309540-need-help-with-functionsphp-file-on-my-wordpress-site/#findComment-1571655 Share on other sites More sharing options...
JacobSeated Posted November 18, 2019 Share Posted November 18, 2019 (edited) Wordpress has its own file handler (wp_filesystem), which is the official way to deal with the file system in WP. However, WP developers will often recommend you use the database to store things – probably because they feel it is more secure. But, the file system is sometimes a better choice. Many WP developers will not like it if you use bare file- functions such as file_get_contents in PHP. I personally do not mind. In fact, I would like to warn against wp_filesystem, since it does not seem to handle concurrency and file locking, which means data can be lost if two users tries to access a file at the same time. Reading is typically not a problem, but can be if someone happen to write to a file at the same time as a file-read. wp_filesystem also introduce other problems, such as prompting users for credentials to upload files via FTP if they do not have write permissions on the server. That's not a desired behavior, so you should first check for write permissions, and then present users with an error if permissions are insufficient. Instead of using functions.php, consider making a plugin or using the Code Snippets plugin, as it will be easier to maintain. Edited November 18, 2019 by JacobSeated Quote Link to comment https://forums.phpfreaks.com/topic/309540-need-help-with-functionsphp-file-on-my-wordpress-site/#findComment-1571667 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.