Jump to content

Files from directory


hackalive

Recommended Posts

Hi guys,

I am using a MySQL table to store if a user has access permisisons to a file.

 

The files are stored outside the webdirectory in drive F:\

 

So I have this code I have been playing around with in order to display the file I access and check permissions for:

<?php
$file = $_GET['file'];

$myfile = 'F:\files\/'.$file.'.jpg';

echo "<img src='$myfile' />";
?>

 

 

So a sample case would be say on index.php

<img src="http://somedomain.com/files?file=abcdefghijkl" />

and only if the current user had permissions to view that file would they see it.

 

Any help on how I can do this is greatly appreciated.

 

I do understand all my above code is probably totally wrong.

 

Cheers

Link to comment
Share on other sites

The F:\ drive will not be accessible because it is outside of the web servers root. You will need to use a php script to serve up the file.

 

There is an example of how to do so in our code snippet / repository board.

Link to comment
Share on other sites

This is the script I was thinking of. Not exactly what you need, as this will force a download, but hopefully you'll get the idea.

 

The idea is to create a php file called image.php or whatever, have it do the database check and all that jazz, then if the user has access set the appropriate headers to make the image.php file serve an image.

 

All you would need to do then is place it in your src attribute. eg;

 

echo "<img src='image.php?f=somefile' />";

 

I'd post an example except the in laws are on there way.

Link to comment
Share on other sites

Here is my code:

 

test.php

<?php
    echo "<img src='filehandler.php?file=1' />";
?>

 

filehandler.php

<?php
    $file = $_GET['file'];
    // You would need a way to set the extension automatically.
    $file = 'F:\files\/'.$file.'.jpg';
    
    // I use this $content_type so that the handler can become multi-purpose.
    //You will need an auto way to adjust the final $file line above.
    $content_type = mime_content_type($file);

    header("Content-Type: $content_type");

    @readfile($file);
?>    

 

All seems to work, without permissions yet.

Link to comment
Share on other sites

There is nothing stopping anyone from using your script now to look at whatever images they like though.

 

You need to put the actual permissions checks in this file itself and maybe serve an "Unauthorised" image or something if your user doesn't have sufficient permissions.

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.