How to protect file url with PHP?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    How to protect file url with PHP?

    can anyone help me?
    i want to protect my file's url with PHP but i don't know how to do that
    can u share php code to do that?
    i'm vietnamese,so my english not good

    #2
    can you be more specific with example? its too complicated to understand. do u want to protect ur files from direct download? or u wanna hide ur files url with another.. in first case you can use hotlink protection and in second case you can use mod_rewrite on .htaccess.

    Comment


      #3
      Originally posted by yoblooc295 View Post
      can anyone help me?

      i want to protect my file's url with PHP but i don't know how to do that
      can u share php code to do that?
      i'm vietnamese,so my english not good


      PHP Code:
      $file_url 'http://some_site.com/pathe/file.jpg';
      $contents file_get_contents($file_url);
      echo 
      $contents
      you will also need to send correct headers.

      if the file is located on your site i would recommend to use
      PHP Code:
      echo readfile($file_path); 
      or you can also use rewrite rules in you htaccess
      Advertise your mobile site for FREE with AdTwirl

      Comment


        #4
        Gumslone's question is right... the best way is to put the file out side the folder where it can be executed eg. public_html ,using the correct headers,use echo readfile($file_path); as Gumslone said....

        Comment


          #5
          gumslone right you can protect your files using fread with correct header type to make it downloadable

          Comment


            #6
            Originally posted by GumSlone View Post
            PHP Code:
            $file_url 'http://some_site.com/pathe/file.jpg';
            $contents file_get_contents($file_url);
            echo 
            $contents
            you will also need to send correct headers.

            if the file is located on your site i would recommend to use
            PHP Code:
            echo readfile($file_path); 
            or you can also use rewrite rules in you htaccess

            Something else to mention :p don't use $_GET['file']; in a
            PHP Code:
            echo readfile($file_path); 
            function because someone else can download your site files // via modified url ..

            for example if someone requests connectdb.php via your link like download.php?file=connectdb.php it will download your file .. now depends on the header you use to save the files ..


            ex:

            PHP Code:
            <?php
            $file 
            'monkey.gif';

            if (
            file_exists($file)) {
                
            header('Content-Description: File Transfer');
                
            header('Content-Type: application/octet-stream');
                
            header('Content-Disposition: attachment; filename='.basename($file));
                
            header('Content-Transfer-Encoding: binary');
                
            header('Expires: 0');
                
            header('Cache-Control: must-revalidate');
                
            header('Pragma: public');
                
            header('Content-Length: ' filesize($file));
                
            ob_clean();
                
            flush();
                
            readfile($file);
                exit;
            }
            ?>
            you could select the path from an database ..
            example:

            $file =mysql_query("SELECT * FROM table WHERE fileid='".$myid"'.");


            readfile($file['path']);
            Last edited by just_m3.; 10.01.12, 11:15.
            This is ten percent luck, twenty percent skill
            Fifteen percent concentrated power of will
            Five percent pleasure, fifty percent pain

            And a hundred percent reason to remember the name!

            Comment


              #7
              @ just_m3 its true,again if possible,U can check the ip for double request to prevent data copiers and bot copiers from wasting ur bandwith for their benefits....

              Comment

              Working...
              X