Results 1 to 4 of 4

Thread: Display a PHP file in browser

  1. #1

    Thread Starter
    Hyperactive Member Daskalos's Avatar
    Join Date
    Jun 2002
    Location
    Just behind YOU...
    Posts
    364

    Display a PHP file in browser

    Hi..
    that's what I need:
    I have one file func.php
    this file have some functions that are used by the site..
    i need to show the content of this file by callin another file..
    like taking every row of the func.php and add echo in front of it but in another file
    I can't copy and paste the content of func.php

    nay way to do that?

    thanks in advance
    [vbcode]Dim Daskalos As NewBie
    If My.english = Wrong Then
    Forgive My.Poor.English
    End If[/vbcode]
    Ða§kalø§
    ICQ#: 36146307
    Current ICQ status:
    More ways to contact me

  2. #2
    Hyperactive Member flame_211's Avatar
    Join Date
    Jun 2000
    Location
    I dont really think I know where I am???
    Posts
    393

    Re: Display a PHP file in browser

    You will have to open the file, similar to the open function in VB. I'd look it up on php.net. The function you'll want to look at is called open.
    Reach me at:
    AIM: FlameWide
    AIM: Itz deep6
    MSN/Email: [email protected]

    Check out these sites:
    My Blog

  3. #3
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099

    Re: Display a PHP file in browser

    I don't know if its exactly what you want but look at highlight_file() it will print out a php and color the PHP syntax.

    http://uk2.php.net/manual/en/function.highlight-file.php

    Alternatively you could open the file, using fopen, and use preg_match to find the function that you are looking for.

  4. #4
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Display a PHP file in browser

    source.php
    PHP Code:
    <?
      /*+-------------------------+
        | PHP Source              +
        +-------------------------+
        | Author: David Miles     |
        | Version: 1.0            |
        | Date: MDY - 7/20/03     |
        +-------------------------+*/
      //| Configuration           |
        $self = "source.php"; //this scripts name
      //+-------------------------+
    ?>
    <?
      $_GET['file'] = (isset($_GET['file']) && $_GET['file'] != "") ? safe_text($_GET['file']) : "";
    ?>
    <html>
      <head>
        <title>PHP Source</title>
      </head>
      <style>
        body { background: #ffffff; font: 12px courier; color: #000000; margin: 0px; }
        a { color: #0000ff; }
        a:hover { color: #ff0000; }
        .top { width: 100%; background: #ffffff; padding: 10px 0px 15px 15px; }
        .php { width: 100%; background: #e6e6e6; border: 1px solid #000000; border-left-style: none; border-right-style: none; }
        .ind { padding: 5px 15px 5px 15px; }
        .stats { border: 1px solid #000000; border-left-style: none; border-right-style: none; border-top-style: none; }
      </style>
      <body>
        <div class="top">
          <h1><?=$_GET['file'];?></h1>
        </div>
        <div class="php">
          <a name="source"></a>
    <?
      if(isset($_GET['file']) && $_GET['file'] != ""){
        if(file_exists($_GET['file'])){
          $type = explode(".", $_GET['file']);
          if(isset($type[1]) && $type[1] == "php"){
            if($_GET['file'] != $self){
              $fopen = fopen($_GET['file'], "r");
              $fread = fread($fopen, filesize($_GET['file']));
              fclose($fopen);
              $lines = explode("\n", $fread);
              $linecount = count($lines);
              echo "      <div class=\"stats\">\n";
              echo "        <div class=\"ind\">\n";
              echo "          <b>Script Statistics</b><br>\n";
              echo "          Filename: " . $_GET['file'] . "<br>\n";
              echo "          Filesize: " . filesize($_GET['file']) . " bytes<br>\n";
              echo "          Lines: " . $linecount . "<br>\n";
              echo "        </div>\n";
              echo "      </div>\n";
              echo "      <div class=\"ind\">\n";
              //get rid of mysql login names and passwords!
              $fread = preg_replace("/\mysql_pconnect(.*?)\((.*?), (.*?), (.*?)\)/i", "mysql_pconnect$1(\"host\", \"user\", \"pass\")", $fread);
              $fread = preg_replace("/\mysql_connect(.*?)\((.*?), (.*?), (.*?)\)/i", "mysql_connect$1(\"host\", \"user\", \"pass\")", $fread);
              $code = "      [source]$fread [/source]\n";
              $code = source($code);
              echo $code;
              echo "      </div>\n";
            }else{
              echo "      <div class=\"ind\">\n";
              echo "        The source for file <b>$_GET[file]</b> is not allowed to be viewed.\n";
              echo "      </div>\n";
            }
          }else{
            echo "      <div class=\"ind\">\n";
            echo "        File <b>$_GET[file]</b> isn't a correct PHP file.\n";
            echo "      </div>\n";
          }
        }else{
          echo "      <div class=\"ind\">\n";
          echo "       File <b>$_GET[file]</b> doesn't exist.\n";
          echo "      </div>\n";
        }
      }else{
        echo "      <div class=\"ind\">\n";
        echo "        No file loaded.\n";
        echo "      </div>\n";
      }
    ?>
        </div>
      </body>
    </html><?
      function safe_text($txt){
        $bad = array("`", "~", "!", "@", "#",  "$", "%", "^", "*", "(",  ")",  "+", "=",
                     "[", "]", "{", "}", "\\", "|", ";", ":", "'", "\"", "\,", "<", ">",
                     "/", "?");
        $new = "";
        $n = count($bad);
        for($i = 0; $i < $n; $i++){
          $txt = str_replace($bad[$i], $new, $txt);
        }
        return $txt;
      }
      function source($string){
        $array_contenido = explode("[source]", $string);
        $final = $array_contenido[0];
        for($i = 1; $i < count($array_contenido); $i++){
          $array_contents = explode("[/source]", $array_contenido[$i]);
          ob_start();
          highlight_string($array_contents[0]);
          $array_contents[0] = ob_get_contents();
          ob_end_clean();
    //      $final .= "<div class=\"php\"><a name=\"source\"></a><nobr>" . $array_contents[0] . "</nobr></div>" . $array_contents[1] . "\n";
          $final .= "<nobr>" . $array_contents[0] . "</nobr>" . $array_contents[1] . "\n";
        }
        return $final;
      }
    ?>
    Open files using $_GET['file']: source.php?file=func.php

    Demo:
    http://david.gamersepitome.net/files...e=imgCount.php
    Like Archer? Check out some Sterling Archer quotes.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width