readfile in directory outside of public_html

Issue

I’m trying to read a file outside of the public_html folder.

$file = '/var/www/new.txt';

$fileHandle = file_get_contents($file);
if($result)
{

    $o = mysqli_num_rows($result);
    
    if ($o)
        readfile($file);
    else
        echo 'Error authenticating please contact support';
}
else {
    echo '';
}

I think the php file doesn’t have permissions to access this file, how would I be able to make it have access to this file?

Solution

try to access your file via relational path.

for example your folder structure is:

-folder
   --new.txt
 
-public_html
   --reader.php

in your reader.php :

<?php
    $file = '../folder/new.txt';
    
    $fileHandle = file_get_contents($file);

Answered By – Milad Elyasi

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published