简体   繁体   中英

Echo out php code from file without actually executing it

We are trying to create a webpage in laravel where people are going to be able upload their codefiles to our server, so that other users can watch the code and download it in codefiles if they like it. We however can't figure out the best way to make this happen.

I tried to just let php get a file and echo out the content. this worked well fot html and css, but with php nothing got displayed what so ever. someone mentioned using eval(), however i've read that it is a really bad idea to do so. Another idea would be to stash the code in a database and fetch it from there, which we have tried before, but it sort of over complicated, and avoiding to do so would be prefereable, and instead go directly to i file.

So my question is, do anybody have an idea that might work safely, both for us and our server and for the users.

Something like this:

<?php
   // read Codefile
   $TheCode = file_get_contents($codefile);

   // Print it...
   echo htmlentities($TheCode);
?>
  1. Save the php code in a flat file like one with a .dat extension.

  2. then read the file.

     $toechp = file(static.dat); echo $toecho; 
  3. You can allow .dat files to be downloaded on browser using headers.

     <?php $file = "http://example.com/static.dat"; header("Content-Description: File Transfer"); header("Content-Type: application/octet-stream"); header("Content-Disposition: attachment; filename=\\"$file\\""); readfile ($file); ?> 

and you are done.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM