• xianwu

    (@xianwu)


    I have worked press in store on my local PC. And installed elementor, insert php code snippet plugin.
    create 2 php snippet

    print_deno
    <?php
    print '<h3>'.'Good Morning.'.'</h3>'.'<br>';
    print 'What is your Roll No.?'.'<br>';
    ?>

    read_file_demo

    <?php
    $myfile = fopen("abc.txt", "r") or die("Unable to open file!");
    echo fread($myfile,filesize("abc.txt"));
    fclose($myfile);
    ?>

    and insert the snippet into a page.
    the print_demo works fine. but read_file_demo failed. got this:
    Warning: fopen(abc.txt): failed to open stream: No such file or directory in C:\xampp\htdocs\wp6\wp-content\plugins\insert-php-code-snippet\widget.php(64) : eval()’d code on line 2
    Unable to open file!

    I do have abc.txt at C:\xampp\htdocs\wp6\wp-content\plugins\insert-php-code-snippet\

    please tell me what I did wrong.
    thank you for reading.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    One problem is storing custom files in plugin folders is a bad location. It’ll disappear when the plugin is updated. You should store custom files in /wp-content/ somewhere.

    The other problem is using a relative file reference. What code sees as the current directory in WP is variable. You need to specify the full path for any file references. If you use the uploads folder, get the path with wp_upload_dir(). Otherwise the path to the WP installation is defined as ABSPATH. Concatenate the rest of the path from there if you do not use /uploads/.

    Thread Starter xianwu

    (@xianwu)

    thank you so much.
    now I puth abc.txt in ABSPATH. and snippet read it correctly

    <?php
    	printf( 'Path: %s', get_home_path().'<br>' );
            printf( 'ABSPATH: %s', ABSPATH.'<br>' );
            $abcfile = ABSPATH.'abc.txt';
            print( $abcfile.'<br>' );
            
            $myfile = fopen($abcfile, "r") or die("Unable to open file!");
            echo fread($myfile,filesize($abcfile));
            fclose($myfile);
    ?>

    in elementor edit section, it shows content of the file.
    but we I visit the page. I get fatal error

    Fatal error: Uncaught Error: Call to undefined function get_home_path() in C:\xampp\htdocs\wp6\wp-content\plugins\insert-php-code-snippet\widget.php(64) : eval()’d code:2 Stack

    Moderator bcworkz

    (@bcworkz)

    It is apparently a limitation of the snippet plugin. In such a case we normally refer you to the plugin’s dedicated support forum. It looks like that forum isn’t very active though.

    All I can suggest is manually creating your own shortcode through the Shortcode API. When you create your own shortcode you cannot echo or print out content. Content must be collected in to a variable that is returned by the handler function for WP to echo out at the right time.

    Thread Starter xianwu

    (@xianwu)

    thank you. you are big helper!

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘php read file’ is closed to new replies.