0

I am writing my output to the text file and then downloading it using php but issue is that it is saving the output but it is also saving the whole structure of HTML into the textfile also. I don't know why its happening tried to solve it but did'nt figure out how.

I want to save output from fwrite($fh, $formatted_url."\n");

Below is my code:

function get_m3u8_video_segment($url,$portnum=80,$from,$to) 
        {   
           $file_name="urlscan.txt";
           $fh = fopen($file_name, 'w') or die("Unable to open file!");
           for ($x = $from; $x <= $to; $x++)
           {  
               $formatted_url="{$url}:{$portnum}/s-{$x}.m3u8";
                //echo "URL is: $formatted_url <br>";
               //$contents = file_get_contents($formatted_url);
               $contents = get_web_page( $formatted_url );
               if ((strpos($contents, 'not found') !== false)||(strpos($contents, 'EXTM3U') !== false))
               {
                   echo" $formatted_url<br>";

                   fwrite($fh, $formatted_url."\n");
               }

           }
           //header download
           header("Content-Disposition: attachment; filename=\"" . $file_name . "\"");
           header("Content-Type: application/force-download");
           header('Expires: 0');
           header('Cache-Control: must-revalidate');
           header('Pragma: public');
           header("Content-Type: text/plain");

        }
        get_m3u8_video_segment($url,$portnum,$from,$to);
     }
  • I can't see any code which would cause the contents of your text file to be downloaded. Which code do you believe is doing that? And what HTML are you talking about? There's no HTML in your sample. – ADyson Jun 15 '20 at 18:33
  • Yes you are right,,,, There is no html,,, it is downloading html from page source,,, and saving it in a textfile...last 6 lin file es starting by header() are used for downloading the text – Jahanzaib Niazi Jun 15 '20 at 18:37
  • No they just set the headers. They don't send any content by themselves. – ADyson Jun 15 '20 at 18:38
  • There is nothing here which would output the contents of urlscan.txt to the browser. And all you appear to be saving into urlscan.txt as far as I can see is some URLs. Have you checked the contents of that file on the server? – ADyson Jun 15 '20 at 18:39
  • But file is downloading dear,,, if they are just headers then how it is downloading the file..Another thing HTML text is present in my this php file it is saving that html content in text file too...i just want it to save the content which is being output the for loop. – Jahanzaib Niazi Jun 15 '20 at 18:40
  • What is `get_web_page()`? That is not a built-in PHP function. – kmoser Jun 15 '20 at 18:50
  • Does this answer your question? [Write a text file and force to download with php](https://stackoverflow.com/questions/11903436/write-a-text-file-and-force-to-download-with-php) – kmoser Jun 15 '20 at 18:51
  • @kmoser 'get_web_page()' is userdefined function which is just loading urls using curl() and yes i followed the same code which you reffered. – Jahanzaib Niazi Jun 15 '20 at 18:54
  • it is saving the whole content of my php file,,, which also contains the html content....it behaviour is insance you can see i am just writing the 'fwrite($fh, $formatted_url."\n");' but it is saving the all content from php file to textfile. – Jahanzaib Niazi Jun 15 '20 at 18:56

1 Answers1

0

If there is other HTML content elsewhere in your PHP script then this will also be outputted as it normally is, except in this case it will become part of the downloaded file. If you don't want that to happen then you have to stop your script with an exit(); command after you have output the content you actually want. In your script, it looks like you can probably do this just after the call to the function. (But if you have already output some HTML before this, you'll need to alter your script more substantially.)

N.B. I'm surprised you aren't getting a warning about headers being already sent? That normally happens if you try to set headers after you've already echoed some content. Check your log files. Normally you are supposed to output the headers first.

Also, unless you are wanting to keep it for some other purpose, there is no use in saving anything to urlscan.txt - it is not playing any part in your download process. And it would get overwritten every time this script is executed anyway. The headers will cause the browser to treat the output contents (i.e. anything which the PHP script sends to the regular output) as a text file - but this is not the same file as the text file on your server's disk, and its contents can be different.

You happen to be outputting similar content (via echo" $formatted_url<br>";) as you are adding to the urlscan file (via fwrite($fh, $formatted_url."\n");) and I think this may be confusing you into thinking that you're outputting the contents of urlscan.txt, but you aren't - your PHP headers are telling the browser to treat the output of your script (which would normally just go onto the browser window as a HTML page) as a file - but it's a) a new file, and b) actually isn't a file at all until it reaches the browser, it's just a response to a HTTP request. The browser turns it into a file on the client machine because of how it interprets the headers.

Another thing: the content you output needs to be in text format, not HTML, so you need to change the <br> in your echo to a \n.

Lastly, you're outputting the content-type header twice, which is nonsense. A HTTP request or response can only have one content type. In this case, text/plain is the valid MIME type, the other one is not real.

Taking into account all of the above, your code would probably be better written as:

function get_m3u8_video_segment($url, $portnum=80, $from, $to) 
{   
  //header download
  $file_name="urlscan.txt";
  header("Content-Disposition: attachment; filename=\"" . $file_name . "\"");
  header('Expires: 0');
  header('Cache-Control: must-revalidate');
  header('Pragma: public');
  header("Content-Type: text/plain");

  for ($x = $from; $x <= $to; $x++)
  {  
    $formatted_url="{$url}:{$portnum}/s-{$x}.m3u8";
    $contents = get_web_page( $formatted_url );

    if ((strpos($contents, 'not found') !== false)||(strpos($contents, 'EXTM3U') !== false))
    {
      echo" $formatted_url\n";
    }
  }
}
get_m3u8_video_segment($url, $portnum, $from, $to);
exit();
ADyson
  • 57,178
  • 14
  • 51
  • 63