Jump to content

Recommended Posts

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.
     */

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 in1761402736_siteproblem2.thumb.JPG.75693ba974966a021265f5eadc74b38e.JPG

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?)

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).

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 by kiko12122

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
}

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.

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 by JacobSeated
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.