Jump to content

File Visibility problems


spelltwister

Recommended Posts

Hey all!  I have a page that looks at a users posted data and fetches a .txt file for them, I want to prevent users from navigating to the .txt file directly, but if i change security off of 755, I get an error saying permission to read file denied.

 

I'm reading the file and echoing it back because the requesting application is a C# app using HttpWebRequest.  If there's a better way to do that, I'm all ears.

 

Here's the php code up:

 

<?php

$game = $_POST['gameNum'];
$player = $_POST['player'];
$file = $_POST['file'];

include('db.php');

if($file=="p"){
    $path = "/pathToFiles/$player.txt";
    getFileByPath($path);
}elseif($file=="setup"){//get setup file
  $path = "pathToFiles/setup.txt";
  getFileByPath($path);
}

function getFileByPath($path){
  $fp = fopen($path, 'r');// <------------ERROR: permission denied unless 755 :/
  if($fp){
    while(!feof($fp)){
      $buffer = fgets($fp, 4096);
      echo $buffer;
    }
    fclose($fp);
  }else{
    echo "Could not open file";
  }
}
?>

 

If I leave the file as 755, then anyone on the internet can view it by going to mydomain.com/pathToFiles/setup.txt and that'd be terrible ;D

 

Thanks for any help!

 

Mike

Link to comment
Share on other sites

You could always try putting the file in a folder with .htaccess that only allows your server to access the files, then have a php file that gets the file contents if and only if you enter in a security code or something

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.