Title: php read file
Last modified: May 25, 2020

---

# php read file

 *  [xianwu](https://wordpress.org/support/users/xianwu/)
 * (@xianwu)
 * [6 years ago](https://wordpress.org/support/topic/php-read-file/)
 * 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.
    -  This topic was modified 6 years ago by [xianwu](https://wordpress.org/support/users/xianwu/).
    -  This topic was modified 6 years ago by [Steven Stern (sterndata)](https://wordpress.org/support/users/sterndata/).
    -  This topic was modified 6 years ago by [xianwu](https://wordpress.org/support/users/xianwu/).

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

 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [6 years ago](https://wordpress.org/support/topic/php-read-file/#post-12888894)
 * 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](https://wordpress.org/support/users/xianwu/)
 * (@xianwu)
 * [6 years ago](https://wordpress.org/support/topic/php-read-file/#post-12889337)
 * 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](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [6 years ago](https://wordpress.org/support/topic/php-read-file/#post-12894487)
 * It is apparently a limitation of the snippet plugin. In such a case we normally
   refer you to the plugin’s [dedicated support forum](https://wordpress.org/support/plugin/insert-php-code-snippet/).
   It looks like that forum isn’t very active though.
 * All I can suggest is manually creating your own shortcode through the [Shortcode API](https://codex.wordpress.org/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](https://wordpress.org/support/users/xianwu/)
 * (@xianwu)
 * [6 years ago](https://wordpress.org/support/topic/php-read-file/#post-12895499)
 * 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.

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 4 replies
 * 2 participants
 * Last reply from: [xianwu](https://wordpress.org/support/users/xianwu/)
 * Last activity: [6 years ago](https://wordpress.org/support/topic/php-read-file/#post-12895499)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
