PotatoREX
PotatoREX

Reputation: 37

PHP file handling program on laravel 4.2

I want to open a php file with a file handling code on it using laravel 4.2

Here is the php code that I have

<html> 
  <head> 
      <title>Gyoza</title>
      <link rel = "stylesheet" href = "{{URL::asset('styleCenter.css') }}">     
  </head> 
  <body>  

    <img src="gyoza.jpg" width =250px height =250px>

    <?php 
        $filename = "gyoza.txt"; 

        $file = fopen( $filename, "r" ); 
        if( $file == false ) 
        { 
            echo ( "Error in opening file" ); 
            exit(); 
        } 

        $filesize = filesize( $filename ); 
        $filetext = fread( $file, $filesize ); 

        fclose( $file );  

        echo ( "<pre>$filetext</pre>" ); 

    ?>  
  </body> 
</html> 

When I run this program it shows this error fopen(gyoza.txt): failed to open stream: No such file or directory

Upvotes: 0

Views: 242

Answers (1)

manix
manix

Reputation: 14747

Two comments:

  1. It is not needed to use exit(); cause this will stop rendering the rest html inside the view.

  2. error fopen(gyoza.txt): failed to open stream: No such file or directory means that the file could not be found in the path where the view reside. Just as example, take a note about path using laravel's native functions:

    // file at your-app/app/storage/gyonza.txt
    fopen(base_path('app/storage/gyoza.txt');
    
    // file at your-app/public/gyonza.txt
    fopen(public_path('gyoza.txt');
    

Edit: About paths

When you make a laravel installation, by default the structure of its directories looks like:

laravel-app/
    /app
    /app/storage
    /app/storage/logs/laravel.log
    /app/views
    /app/views/home.blade.php
    /test.txt
    /public/robots.txt

So, if you want to access some contained in this structure, you have some alternatives/shortcuts to access a particular file. For example:

public_path() is the shortcut to /laravel-app/public path:

public_path('robots.txt')

base_path() is the shortcut to /laravel-app/ path:

base_path('test.txt')

app_path() is the shortcut to /laravel-app/app path:

app_path('storage/logs/laravel.log')

Upvotes: 1

Related Questions