We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 23022
    • 8 Posts
    I want to use the content of a modx-document (defined as type = "text/x-csv" and browser handling as "download/attachment") as input to the fopen php-funtion. how I can manage that?


    <?php
    $file = fopen("[~nn~]" ’r’);
    while ($row = fgetcsv($file, $length+1, $delimiter)) {
    ..
    }
    ?>

    That way does not work. Please, who can give me help?
    • I do not believe you can as it doesn’t physically exist on your server, but I’m no coder so what do I know??? lol
        Ryan Thrash, MODX Co-Founder
        Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
      • You can use fopen() with MODx documents as it can take a URL as a file identifier wink

        You will need to use the makeURL API function to get the URL of a document though - I’ve not got access to the code where I have done it at the moment but hopefully, I can post an example later on.

        Cheers, Garry
          Garry Nutting
          Senior Developer
          MODX, LLC

          Email: [email protected]
          Twitter: @garryn
          Web: modx.com
          • 23022
          • 8 Posts
          Quote from: garryn at Mar 09, 2006, 02:55 PM

          You can use fopen() with MODx documents as it can take a URL as a file identifier wink

          You will need to use the makeURL API function to get the URL of a document though - I’ve not got access to the code where I have done it at the moment but hopefully, I can post an example later on.

          Cheers, Garry

          do you mean like that ?

          $filename = $modx->makeUrl(99);
          $file = fopen ( $filename, "r");
          


          it generates following error:
          Error: fopen(/index.php?id=99) [function.fopen]: failed to open stream: No such file or directory
          >:(
          • Hi PeppeLePew,

            I think with fopen() you’ll need to add the ’http://mywebsite.com’ to the front of the URL for it to treat the filename as a URL.

            Now, there is a way to could do this with the MODx API (will check when I get home from work) but if it’s only for your website, you could just prepend it to the start of the makeURL function:

            $filename = 'http://www.mywebsite.com'.$modx->makeUrl(99);


            Cheers, Garry
              Garry Nutting
              Senior Developer
              MODX, LLC

              Email: [email protected]
              Twitter: @garryn
              Web: modx.com
              • 11413
              • 203 Posts
              so maybe something like that : fopen($modx->config["site_url"].$modx->makeUrl(99))

              would be more efficient... no ?

              bye,

              Blaise
                Blaise Bernier

                www.medialdesign.com - Solutions for small business, hosting, design and more!
                • 23022
                • 8 Posts
                With this code it seems to work ...
                $filename = sprintf("http://%s%s", getenv("HTTP_HOST"), $modx->makeUrl(99));
                $f = @fopen($filename, "r");
                if ($f) {
                  while (!@feof($f)) {
                    $output .= sprintf ("%s", @fgets ( $f ));
                  }
                  @fclose($f);
                }
                


                ... the file can be opened laugh
                ... but the content of the file is not that I expected shocked huh

                ... the file wich is read, looks exact like the result I get with "http://www.mysite.com/index.php?id=1"
                ... when I call this address http://www.mysite.com/index.php?id=1 direct in browser I get the right content

                any ideas?
                • Well done on getting the fopen() working smiley

                  Can I ask, what did you expect as a result? fopen() will just read the HTML from the page with the code that you’ve used above, so did you want to grab something else other than the HTML?

                  Cheers, Garry
                    Garry Nutting
                    Senior Developer
                    MODX, LLC

                    Email: [email protected]
                    Twitter: @garryn
                    Web: modx.com
                    • 23022
                    • 8 Posts
                    I expect the content of the file (maybe html-code or others like csv content) that I opened and not the content of the start_url
                    For example:

                    I open the file "http://www.mysite.com/index.php?id=99" and read its content, but I get the content of the file "http://www.mysite.com/index.php?id=1".
                    • Hmm, I see what you mean now - I should learn to read better rolleyes

                      I’ve just tried the code above on my website and it works fine, and returns the correct page. Is there anything setup on the required page regarding permissions or something that would prevent fopen() getting the page?

                      I’m wondering whether you might be getting an error (eg. 404) which would default to the home page for the error page (unless you’ve set it up differently).

                      Cheers, Garry
                        Garry Nutting
                        Senior Developer
                        MODX, LLC

                        Email: [email protected]
                        Twitter: @garryn
                        Web: modx.com