Hello

I can't quite figure out how to include a file from the file URL. Eg, the file is "article.php" and if I go to "article.php?a=article1" it will load "article1.txt" from the "articles directory".

I think php Include would be the best way to do it, but I can;t figure it out.

Can anyone post the code I should use? I know its simple, but I can't figure it out - I'm not a PHP developer (just an ASP and HTML coder 😉 )

Thanks

    unsafe but a start

    	include($_GET[a].'.txt');
    
      rfc;10900211 wrote:

      I'm not a PHP developer (just an ASP and HTML coder 😉

      I'm so sorry 🙂 (j/k... some friendly ribbing is all)..

      If I understand you correctly, you want to include a simple php file into your document? If so, it could go like this:

      include ($_SERVER['DOCUMENT_ROOT'].'/somefolder/article.php');
      

      Where $SERVER['DOCUMENT_ROOT'] is automatically assigned the root of your site, and if there is a folder where article.php resides, use that folder, then the file.. if the file is in the root, then simply use $SERVER['DOCUMENT_ROOT'].'/article.php' instead...

      Is that what you are looking for?

        lol 😉

        I'm looking for a simple file where it includes the name after the file in the browser (so if it was article.php?a=1, it would take /articles/1.txt).

          rfc;10900215 wrote:

          lol 😉

          I'm looking for a simple file where it includes the name after the file in the browser (so if it was article.php?a=1, it would take /articles/1.txt).

          Hmm. do you mean?

          include ($_SERVER['DOCUMENT_ROOT'].'/articles/1.txt');
          

          I get the sense I may be misunderstanding something here...

            GET, POST, etc. are http constructs. A local include doesn't involve http request/response, so the http query array is not involved and not set. PHP sees the include string (for local includes, without a protocol prefix) as a relative or absolute pathname. I believe the same is true for asp.

            Since included code inherits the scope of the including code, just set the appropriate variable(s) before calling the include. For example:

            $a = 'article1';
            include 'article.php';

            That relates to your first post. To do what you ask in you latest post:

            $a = 1;
            include 'article/' . $a . '.txt';

            More in the PHP manual chapter on include.

              The $GET array contains var=value pairs from the URL query string, so you could do something like:

              <?php
              $path = $_SERVER['DOCUMENT_ROOT'].'/artilces';
              $num = (isset($_GET['a'])) ? (int)$_GET['a'] : 0; // cast to int for security purposes
              if(is_readable("$path/$num.txt"))
              {
                 include "$path/$num.txt";  // use readfile() instead of include if pure text (no PHP code)
              }
              else
              {
                 // error message, or include default file, or whatever you want to do
                 // if invalid $_GET value received or no value received
              }
              ?>
              
                NogDog;10900224 wrote:

                The $GET array contains var=value pairs from the URL query string, so you could do something like:

                <?php
                $path = $_SERVER['DOCUMENT_ROOT'].'/artilces';
                $num = (isset($_GET['a'])) ? (int)$_GET['a'] : 0; // cast to int for security purposes
                if(is_readable("$path/$num.txt"))
                {
                   include "$path/$num.txt";  // use readfile() instead of include if pure text (no PHP code)
                }
                else
                {
                   // error message, or include default file, or whatever you want to do
                   // if invalid $_GET value received or no value received
                }
                ?>
                

                Thanks - that's what I meant (sorry I wasn't being very clear, English isnt my first lang).

                But, it doesnt seem to find the file (I go to ?a=1 and it says that /articles/1.txt doesn't exist).

                Here's my code:

                <?php
                $path = $_SERVER['DOCUMENT_ROOT'].'/articles';
                $num = (isset($_GET['a'])) ? (int)$_GET['a'] : 0; // cast to int for security purposes
                if(is_readable("$path/$num.txt"))
                {
                   include 'header.php';
                   echo "<br>";
                   include "$path/$num.txt";  // use readfile() instead of include if pure text (no PHP code)
                   echo "<br>";
                   include 'footer.php';
                }
                else
                {
                   // error message, or include default file, or whatever you want to do
                   // if invalid $_GET value received or no value received
                   include 'header.php';
                    echo "<br> No articles found matching your criteria. <br>";
                   include 'footer.php';
                }
                ?> 
                

                Sorry for being so newbish - it's Microsofts fault 😉

                  Write a Reply...