How to return a file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bryan

    #1

    How to return a file

    Hello all,

    In some html code I have the following:

    <script type='text/javascript' src='foo.js'></script>

    However, I'd like to replace 'foo.js' with something like
    "bar.php?file=f oo" and have bar.php echo foo.js back to the script
    element. Is this possible?

  • clashers5@gmail.com

    #2
    Re: How to return a file

    Yes.
    Put the foo.js text in the bar.php file or read it from a database or
    text file and have php spit it out. Whatever suits your fancy.

    Then, when bar.php receives the query string variable file with the
    value foo, have it first print the type of the output with this code:

    header('Content-type: text/javascript');

    and then have it print the contents of foo.js (whereever you may have
    stored them)

    HTH
    -Joe

    On Oct 16, 10:41 pm, "Bryan" <BTRichard...@g mail.comwrote:
    Hello all,
    >
    In some html code I have the following:
    >
    <script type='text/javascript' src='foo.js'></script>
    >
    However, I'd like to replace 'foo.js' with something like
    "bar.php?file=f oo" and have bar.php echo foo.js back to the script
    element. Is this possible?

    Comment

    • Tyrone Slothrop

      #3
      Re: How to return a file

      On 16 Oct 2006 19:41:14 -0700, "Bryan" <BTRichardson@g mail.comwrote:
      >Hello all,
      >
      >In some html code I have the following:
      >
      ><script type='text/javascript' src='foo.js'></script>
      >
      >However, I'd like to replace 'foo.js' with something like
      >"bar.php?file= foo" and have bar.php echo foo.js back to the script
      >element. Is this possible?
      First, a query string will not work with an include, like:
      <? include('bar.ph p?file=foo'); }

      However, you can create a function:

      function foo($file)
      {
      include ('$file.js');
      }

      <?=foo('foo') ?>

      Also review file_get_conten ts() and readfile().

      Comment

      • Bryan

        #4
        Re: How to return a file

        Thanks for the help guys, but I can't get it to work.

        As an example, Google does the following in GMail. How does this work?

        <script src="?view=page &amp;name=brows er&amp;ver=o9ci a6293i3x"></script>

        Comment

        • Johnny

          #5
          Re: How to return a file


          "Tyrone Slothrop" <ts@paranoids.c omwrote in message
          news:8hi8j2le32 1hqdkoouo2qnp9b 8n11itnko@4ax.c om...
          On 16 Oct 2006 19:41:14 -0700, "Bryan" <BTRichardson@g mail.comwrote:
          >
          Hello all,

          In some html code I have the following:

          <script type='text/javascript' src='foo.js'></script>

          However, I'd like to replace 'foo.js' with something like
          "bar.php?file=f oo" and have bar.php echo foo.js back to the script
          element. Is this possible?
          >
          First, a query string will not work with an include, like:
          <? include('bar.ph p?file=foo'); }
          >
          However, you can create a function:
          >
          function foo($file)
          {
          include ('$file.js');
          }
          >
          <?=foo('foo') ?>
          >
          Also review file_get_conten ts() and readfile().
          imho that makes things way more complicated than they are...
          the OP is refering to calling a PHP file from a script in an HTML file not
          from a PHP file.
          a PHP file can be called from a script in HTML in the same way as you can
          post to a PHP file from a form using action="myscrip t.php" you just refer to
          the file.

          Also re clashers5: my understanding is that headers are not needed as the
          script is embedded as part of the http being sent to the browser with header
          started for that, it's being included just as foo.js would have been. foo.js
          would not have had a header included in the file.

          so you do something like these files (text as between and not
          inlcuding the dashed lines):
          -----------
          <!-- file this.html -->
          <html>
          <head>
          <title></title>
          </head>
          <body>
          <script type='text/javascript' src='js3.php'></script>
          </body>
          </html>
          ------------


          and in file js3.php:
          -----------
          <?php
          # this is file js3.php with some js to send back to the browser
          # get the js from a db, file or wherever you like
          $js = "alert(\"hi there!\");";

          echo $js;
          ?>
          ----------

          So all you have to do is either echo or print the js to get it into the
          browser as it is embedded as part of the normal HTTP being transmitted to
          the
          browser within the existing html header.


          Here's what w3 has to say about scripts:



          Comment

          • Jerry Stuckle

            #6
            Re: How to return a file

            Bryan wrote:
            Thanks for the help guys, but I can't get it to work.
            >
            As an example, Google does the following in GMail. How does this work?
            >
            <script src="?view=page &amp;name=brows er&amp;ver=o9ci a6293i3x"></script>
            >
            Google is using javascript to pass a url to the server. This is
            something completely different - unrelated to include.

            --
            =============== ===
            Remove the "x" from my email address
            Jerry Stuckle
            JDS Computer Training Corp.
            jstucklex@attgl obal.net
            =============== ===

            Comment

            • Jerry Stuckle

              #7
              Re: How to return a file

              Johnny wrote:
              "Tyrone Slothrop" <ts@paranoids.c omwrote in message
              news:8hi8j2le32 1hqdkoouo2qnp9b 8n11itnko@4ax.c om...
              >
              >>On 16 Oct 2006 19:41:14 -0700, "Bryan" <BTRichardson@g mail.comwrote:
              >>
              >>
              >>>Hello all,
              >>>
              >>>In some html code I have the following:
              >>>
              >>><script type='text/javascript' src='foo.js'></script>
              >>>
              >>>However, I'd like to replace 'foo.js' with something like
              >>>"bar.php?fil e=foo" and have bar.php echo foo.js back to the script
              >>>element. Is this possible?
              >>
              >>First, a query string will not work with an include, like:
              >><? include('bar.ph p?file=foo'); }
              >>
              >>However, you can create a function:
              >>
              >>function foo($file)
              >{
              >include ('$file.js');
              >}
              >>
              >><?=foo('foo') ?>
              >>
              >>Also review file_get_conten ts() and readfile().
              >
              >
              imho that makes things way more complicated than they are...
              the OP is refering to calling a PHP file from a script in an HTML file not
              from a PHP file.
              a PHP file can be called from a script in HTML in the same way as you can
              post to a PHP file from a form using action="myscrip t.php" you just refer to
              the file.
              >
              Also re clashers5: my understanding is that headers are not needed as the
              script is embedded as part of the http being sent to the browser with header
              started for that, it's being included just as foo.js would have been. foo.js
              would not have had a header included in the file.
              >
              so you do something like these files (text as between and not
              inlcuding the dashed lines):
              -----------
              <!-- file this.html -->
              <html>
              <head>
              <title></title>
              </head>
              <body>
              <script type='text/javascript' src='js3.php'></script>
              </body>
              </html>
              ------------
              >
              >
              and in file js3.php:
              -----------
              <?php
              # this is file js3.php with some js to send back to the browser
              # get the js from a db, file or wherever you like
              $js = "alert(\"hi there!\");";
              >
              echo $js;
              ?>
              ----------
              >
              So all you have to do is either echo or print the js to get it into the
              browser as it is embedded as part of the normal HTTP being transmitted to
              the
              browser within the existing html header.
              >
              >
              Here's what w3 has to say about scripts:

              >
              >
              What a confusing way to do it - when Joe's method works quite well.

              --
              =============== ===
              Remove the "x" from my email address
              Jerry Stuckle
              JDS Computer Training Corp.
              jstucklex@attgl obal.net
              =============== ===

              Comment

              • Johnny

                #8
                Re: How to return a file


                "Jerry Stuckle" <jstucklex@attg lobal.netwrote in message
                news:OIadnSwb9d bnL6nYnZ2dnUVZ_ oidnZ2d@comcast .com...
                Johnny wrote:
                "Tyrone Slothrop" <ts@paranoids.c omwrote in message
                news:8hi8j2le32 1hqdkoouo2qnp9b 8n11itnko@4ax.c om...
                >On 16 Oct 2006 19:41:14 -0700, "Bryan" <BTRichardson@g mail.comwrote:
                >
                >
                >>Hello all,
                >>
                >>In some html code I have the following:
                >>
                >><script type='text/javascript' src='foo.js'></script>
                >>
                >>However, I'd like to replace 'foo.js' with something like
                >>"bar.php?file =foo" and have bar.php echo foo.js back to the script
                >>element. Is this possible?
                >
                >First, a query string will not work with an include, like:
                ><? include('bar.ph p?file=foo'); }
                >
                >However, you can create a function:
                >
                >function foo($file)
                {
                include ('$file.js');
                }
                >
                ><?=foo('foo')? >
                >
                >Also review file_get_conten ts() and readfile().

                imho that makes things way more complicated than they are...
                the OP is refering to calling a PHP file from a script in an HTML file
                not
                from a PHP file.
                a PHP file can be called from a script in HTML in the same way as you
                can
                post to a PHP file from a form using action="myscrip t.php" you just
                refer to
                the file.

                Also re clashers5: my understanding is that headers are not needed as
                the
                script is embedded as part of the http being sent to the browser with
                header
                started for that, it's being included just as foo.js would have been.
                foo.js
                would not have had a header included in the file.

                so you do something like these files (text as between and not
                inlcuding the dashed lines):
                -----------
                <!-- file this.html -->
                <html>
                <head>
                <title></title>
                </head>
                <body>
                <script type='text/javascript' src='js3.php'></script>
                </body>
                </html>
                ------------


                and in file js3.php:
                -----------
                <?php
                # this is file js3.php with some js to send back to the browser
                # get the js from a db, file or wherever you like
                $js = "alert(\"hi there!\");";

                echo $js;
                ?>
                ----------

                So all you have to do is either echo or print the js to get it into the
                browser as it is embedded as part of the normal HTTP being transmitted
                to
                the
                browser within the existing html header.


                Here's what w3 has to say about scripts:

                >
                What a confusing way to do it - when Joe's method works quite well.
                >
                what a confusing and totally uncalled for response: when argument fails just
                dismiss... oi!

                what is so confusing about replacing 'foo.js' with 'script.php?foo ' after
                all it is that simple, the rest is illustration padding...
                and BTW in case you didn't notice Joe and I are essentailly in agreement
                except for the header part, which you don't argue with, just dismiss the
                entire post. Anyone can dismiss. All I have done is expand on what Joe said
                with an example but showing that it works just fine without the header.
                And your contribution is...even more confusing but lacking any substance.



                Comment

                • Jerry Stuckle

                  #9
                  Re: How to return a file

                  Johnny wrote:
                  "Jerry Stuckle" <jstucklex@attg lobal.netwrote in message
                  news:OIadnSwb9d bnL6nYnZ2dnUVZ_ oidnZ2d@comcast .com...
                  >
                  >>Johnny wrote:
                  >>
                  >>>"Tyrone Slothrop" <ts@paranoids.c omwrote in message
                  >>>news:8hi8j2l e321hqdkoouo2qn p9b8n11itnko@4a x.com...
                  >>>
                  >>>
                  >>>>On 16 Oct 2006 19:41:14 -0700, "Bryan" <BTRichardson@g mail.comwrote:
                  >>>>
                  >>>>
                  >>>>
                  >>>>>Hello all,
                  >>>>>
                  >>>>>In some html code I have the following:
                  >>>>>
                  >>>>><script type='text/javascript' src='foo.js'></script>
                  >>>>>
                  >>>>>However, I'd like to replace 'foo.js' with something like
                  >>>>>"bar.php?f ile=foo" and have bar.php echo foo.js back to the script
                  >>>>>element. Is this possible?
                  >>>>
                  >>>>First, a query string will not work with an include, like:
                  >>>><? include('bar.ph p?file=foo'); }
                  >>>>
                  >>>>However, you can create a function:
                  >>>>
                  >>>>function foo($file)
                  >>>>{
                  >>>>include ('$file.js');
                  >>>>}
                  >>>>
                  >>>><?=foo('foo ')?>
                  >>>>
                  >>>>Also review file_get_conten ts() and readfile().
                  >>>
                  >>>
                  >>>imho that makes things way more complicated than they are...
                  >>>the OP is refering to calling a PHP file from a script in an HTML file
                  >
                  not
                  >
                  >>>from a PHP file.
                  >>>a PHP file can be called from a script in HTML in the same way as you
                  >
                  can
                  >
                  >>>post to a PHP file from a form using action="myscrip t.php" you just
                  >
                  refer to
                  >
                  >>>the file.
                  >>>
                  >>>Also re clashers5: my understanding is that headers are not needed as
                  >
                  the
                  >
                  >>>script is embedded as part of the http being sent to the browser with
                  >
                  header
                  >
                  >>>started for that, it's being included just as foo.js would have been.
                  >
                  foo.js
                  >
                  >>>would not have had a header included in the file.
                  >>>
                  >>>so you do something like these files (text as between and not
                  >>>inlcuding the dashed lines):
                  >>>-----------
                  >>><!-- file this.html -->
                  >>><html>
                  >>><head>
                  >>><title></title>
                  >>></head>
                  >>><body>
                  >>><script type='text/javascript' src='js3.php'></script>
                  >>></body>
                  >>></html>
                  >>>------------
                  >>>
                  >>>
                  >>>and in file js3.php:
                  >>>-----------
                  >>><?php
                  >>># this is file js3.php with some js to send back to the browser
                  >>># get the js from a db, file or wherever you like
                  >>>$js = "alert(\"hi there!\");";
                  >>>
                  >>>echo $js;
                  >>>?>
                  >>>----------
                  >>>
                  >>>So all you have to do is either echo or print the js to get it into the
                  >>>browser as it is embedded as part of the normal HTTP being transmitted
                  >
                  to
                  >
                  >>>the
                  >>>browser within the existing html header.
                  >>>
                  >>>
                  >>>Here's what w3 has to say about scripts:
                  >>>http://www.w3.org/TR/html401/interact/scripts.html
                  >>>
                  >>>
                  >>
                  >>What a confusing way to do it - when Joe's method works quite well.
                  >>
                  >
                  what a confusing and totally uncalled for response: when argument fails just
                  dismiss... oi!
                  >
                  what is so confusing about replacing 'foo.js' with 'script.php?foo ' after
                  all it is that simple, the rest is illustration padding...
                  and BTW in case you didn't notice Joe and I are essentailly in agreement
                  except for the header part, which you don't argue with, just dismiss the
                  entire post. Anyone can dismiss. All I have done is expand on what Joe said
                  with an example but showing that it works just fine without the header.
                  And your contribution is...even more confusing but lacking any substance.
                  >
                  >
                  >
                  No, I disagree.

                  Yes, your answer and Joe's do the same thing. But his is
                  straightforward and easy to understand. Yours is more complicated,
                  harder to understand and more prone to errors - especially when you have
                  to later modify it.

                  KISS. And Joe's is much simpler.


                  --
                  =============== ===
                  Remove the "x" from my email address
                  Jerry Stuckle
                  JDS Computer Training Corp.
                  jstucklex@attgl obal.net
                  =============== ===

                  Comment

                  • Rik

                    #10
                    Re: How to return a file

                    Jerry Stuckle wrote:
                    KISS. And Joe's is much simpler.

                    Script building rule #1: the most simple solution is often the right one.

                    :-)
                    --
                    Rik Wasmus


                    Comment

                    • Johnny

                      #11
                      Re: How to return a file


                      "Jerry Stuckle" <jstucklex@attg lobal.netwrote in message
                      news:7rydnS6W7r JT3ajYnZ2dnUVZ_ vydnZ2d@comcast .com...
                      <snip/>
                      >What a confusing way to do it - when Joe's method works quite well.
                      >
                      what a confusing and totally uncalled for response: when argument fails
                      just
                      dismiss... oi!

                      what is so confusing about replacing 'foo.js' with 'script.php?foo '
                      after
                      all it is that simple, the rest is illustration padding...
                      and BTW in case you didn't notice Joe and I are essentailly in agreement
                      except for the header part, which you don't argue with, just dismiss the
                      entire post. Anyone can dismiss. All I have done is expand on what Joe
                      said
                      with an example but showing that it works just fine without the header.
                      And your contribution is...even more confusing but lacking any
                      substance.

                      >
                      No, I disagree.
                      >
                      Yes, your answer and Joe's do the same thing.
                      OK...
                      But his is
                      straightforward and easy to understand.
                      That's clearly _not_ the case as Bryan responded "Thanks for the help guys,
                      but I can't get it to work."
                      Yours is more complicated,
                      harder to understand and more prone to errors - especially when you have
                      to later modify it.
                      err...since Joe's didn't cut it , I felt that expansion might help.
                      and I'm very surprised that you find such a simple example as the few lines
                      I supplied hard to understand.
                      LOL The "prone to errors" assertion is interesting in such a short piece of
                      script...

                      It was meant as the simplest, yet complete, _working_ example of returning
                      javascipt using a PHP script.
                      A place to start from...
                      the basic minimum HTML tags around the script tag,
                      <html>...<scrip t...></script><html>
                      don't see what's so prone to error there... I must be missing something

                      the other the bare minimum to return some javascript:
                      <?php #foo.php
                      $js = do_what_you_do_ to_get_the_java script();
                      echo $js;
                      ?>
                      prone to error? well I guess do_what_you_do_ to_get_the_java script(); is
                      prone to error but it is just as prone when done from Joe's same difference.
                      >
                      KISS. And Joe's is much simpler.
                      >
                      The point of the exercise is to help Bryan "get it to work"
                      Clearly, although simpler, Joe's post failed in that end.
                      Has my post failed to help Bryan get it to work? Only Bryan can say.

                      Just in case Bryan hasn't got it to work yet:
                      I'll simplify bits for Jerry and expand some more for Bryan, best of both
                      worlds :-)

                      in the html (simplified...j ust the script line from the file):
                      <script type='text/javascript' src='bar.php?fi le=foo'></script>

                      in the javascript file foo.js:
                      alert("hi there!");

                      in the PHP file, with Joe's header:
                      <?php #bar.php
                      header('Content-type: text/javascript');
                      echo file_get_conten ts($_GET['file'].".js");
                      ?>

                      or even simpler, which Jerry professes to like :-), without Joe's header
                      <?php #bar.php
                      echo file_get_conten ts($_GET['file'].".js");
                      ?>

                      This works with and without the header in bar.php
                      minimum 1 code line in each file. simple enough? and yet not so simple as
                      Joe's post but yet expanded to a working example.

                      Hope this helps get it to work for you Bryan.



                      Comment

                      • Johnny

                        #12
                        Re: How to return a file


                        "Rik" <luiheidsgoeroe @hotmail.comwro te in message
                        news:d0a0$45356 b2a$8259c69c$20 598@news2.tudel ft.nl...
                        Jerry Stuckle wrote:
                        KISS. And Joe's is much simpler.
                        >
                        >
                        Script building rule #1: the most simple solution is often the right one.
                        >
                        :-)
                        --
                        Rik Wasmus
                        >
                        'cept in this case there was no script in the "most simple solution" as
                        defined by Jerry and which of course for the OP wasn't a solution, so no and
                        no. :-)


                        Comment

                        • Jerry Stuckle

                          #13
                          Re: How to return a file

                          Johnny wrote:
                          "Rik" <luiheidsgoeroe @hotmail.comwro te in message
                          news:d0a0$45356 b2a$8259c69c$20 598@news2.tudel ft.nl...
                          >
                          >>Jerry Stuckle wrote:
                          >>
                          >>>KISS. And Joe's is much simpler.
                          >>
                          >>
                          >>Script building rule #1: the most simple solution is often the right one.
                          >>
                          >>:-)
                          >>--
                          >>Rik Wasmus
                          >>
                          >
                          'cept in this case there was no script in the "most simple solution" as
                          defined by Jerry and which of course for the OP wasn't a solution, so no and
                          no. :-)
                          >
                          >
                          No, he didn't type it out. Rather than give an answer, Joe did
                          something better - he have guidance on how to do it. That way the
                          operator has a chance to learn something.

                          It's exactly what I do when teaching classes. And if they continue to
                          have problems, I give them more detailed guidance.

                          But just giving them an answer on something like this doesn't give him a
                          chance to learn.

                          --
                          =============== ===
                          Remove the "x" from my email address
                          Jerry Stuckle
                          JDS Computer Training Corp.
                          jstucklex@attgl obal.net
                          =============== ===

                          Comment

                          • Rik

                            #14
                            Re: How to return a file

                            Jerry Stuckle wrote:
                            Johnny wrote:
                            >"Rik" <luiheidsgoeroe @hotmail.comwro te in message
                            >news:d0a0$4535 6b2a$8259c69c$2 0598@news2.tude lft.nl...
                            >>
                            >>Jerry Stuckle wrote:
                            >>>
                            >>>KISS. And Joe's is much simpler.
                            >>>
                            >>>
                            >>Script building rule #1: the most simple solution is often the
                            >>right one.
                            >>>
                            >>
                            >'cept in this case there was no script in the "most simple solution"
                            >as defined by Jerry and which of course for the OP wasn't a
                            >solution, so no and no. :-)
                            >>
                            >>
                            >
                            No, he didn't type it out. Rather than give an answer, Joe did
                            something better - he have guidance on how to do it. That way the
                            operator has a chance to learn something.
                            >
                            It's exactly what I do when teaching classes. And if they continue to
                            have problems, I give them more detailed guidance.
                            >
                            But just giving them an answer on something like this doesn't give
                            him a chance to learn.
                            It is indeeed what people should do. Even after all this time (and my
                            little hiatus) I haven't learned that yet.

                            The problem is that it's often simpler and faster to type out a solution
                            then explain it, and I'm lazy offcourse :-)

                            (Oh: and I haven't repaired my sig-seperator for nothing, my sig does not
                            have to be quoted...)

                            --
                            Grtz,

                            Rik Wasmus


                            Comment

                            • Jerry Stuckle

                              #15
                              Re: How to return a file

                              Rik wrote:
                              Jerry Stuckle wrote:
                              >
                              >>Johnny wrote:
                              >>
                              >>>"Rik" <luiheidsgoeroe @hotmail.comwro te in message
                              >>>news:d0a0$45 356b2a$8259c69c $20598@news2.tu delft.nl...
                              >>>
                              >>>
                              >>>>Jerry Stuckle wrote:
                              >>>>
                              >>>>
                              >>>>>KISS. And Joe's is much simpler.
                              >>>>
                              >>>>
                              >>>>Script building rule #1: the most simple solution is often the
                              >>>>right one.
                              >>>>
                              >>>
                              >>>'cept in this case there was no script in the "most simple solution"
                              >>>as defined by Jerry and which of course for the OP wasn't a
                              >>>solution, so no and no. :-)
                              >>>
                              >>>
                              >>
                              >>No, he didn't type it out. Rather than give an answer, Joe did
                              >>something better - he have guidance on how to do it. That way the
                              >>operator has a chance to learn something.
                              >>
                              >>It's exactly what I do when teaching classes. And if they continue to
                              >>have problems, I give them more detailed guidance.
                              >>
                              >>But just giving them an answer on something like this doesn't give
                              >>him a chance to learn.
                              >
                              >
                              It is indeeed what people should do. Even after all this time (and my
                              little hiatus) I haven't learned that yet.
                              >
                              The problem is that it's often simpler and faster to type out a solution
                              then explain it, and I'm lazy offcourse :-)
                              >
                              (Oh: and I haven't repaired my sig-seperator for nothing, my sig does not
                              have to be quoted...)
                              >
                              Actually, I think you do a pretty good job at it, Rik.

                              I should also add - when it comes to more complicated problems, often I
                              will supply an answer, also. Mainly because there's a point where an
                              example is necessary to explain what you mean.


                              --
                              =============== ===
                              Remove the "x" from my email address
                              Jerry Stuckle
                              JDS Computer Training Corp.
                              jstucklex@attgl obal.net
                              =============== ===

                              Comment

                              Working...