-1

I want to display images stored in the local filesystem in my Windows machine.

file path : D:/farewell/new/imagename.JPG

<html>
    <head>
        <title>
            Images Gallery
        </title>
    </head>
    <body>
        <?php
        $img="D:/farewell/new/IMG_0603.JPG";
        echo "<img src='$img' width=\"300\"/>";
        ?>
    </body>
</html>

The webpage does not load any image, but the Inspect Element shows following error:

Not allowed to load local resource: file:///D:/farewell/new/IMG_0603.JPG images.php:10

How can I allow PHP or Apache webserver or the Chrome browser, whatever the case may be, to allow access to images stored in other partitions?

EDIT :

I want to access all the images present in D:/farewell/new/. I tried setting up an alias for the same under httpd.conf, but that was of no use as url cannot be served to tags.

Alias /farewell/ "D:/farewell/2/"
<Directory "D:/farewell/2">
   Options Indexes FollowSymLinks
    AllowOverride All
    Order allow,deny
    Allow from all
</Directory>
kamal0808
  • 515
  • 1
  • 8
  • 21
  • 3
    you can't. it'd be a massive security vulnerability to allow web-based content to request/display "local" content. and even if it was possible, it'd be an even worse security problem to allow the server to tell the browser "trust me, it's ok". – Marc B Jul 10 '15 at 14:44
  • @MarcB b I know it will be, but i want to use it on localhost. My server never goes online, I am using it for an application that I will use offline. – kamal0808 Jul 10 '15 at 14:45
  • 2
    if it's all local, then just set up apache to use this `d:\farewell` inside your document root, e.g. an `Alias`. – Marc B Jul 10 '15 at 14:49
  • yes, but web server software isn't made for your special case. It's made for internet where security is THE main issue. You can only access folders within your own web folder tree. Put the images below the web folder and you're done. – Nelson Teixeira Jul 10 '15 at 14:51
  • @MarcB - I had set up an alias earlier, but that allows the browser to access the file. It doesn't allow PHP to access it, as it needs the absolute path to be used. – kamal0808 Jul 10 '15 at 14:53
  • @NelsonTeixeira - I have a large number of images with large sizes, all of them cannot be stored under the web folder. I will have to access them through external storage with shared access. – kamal0808 Jul 10 '15 at 14:54
  • Would `scandir()` with an absolute path not work? I mean we are talking about a Windows box here so it's not *that* well secured across the filesystem anyway : http://php.net/manual/en/function.scandir.php – CD001 Jul 10 '15 at 14:55
  • This has nothing to do with php. – VolkerK Jul 10 '15 at 14:56
  • which is fine. PHP runs at the filesystem level and has no idea (and couldn't care less) what your URL structure is. That's the price you have to pay for dealing with web space and file space. – Marc B Jul 10 '15 at 14:56
  • Have you tried creating a link inside web folder that points to the images dir ? I don't work with windows for a long, long time (thanks God) but I think this should work. It does in Linux. – Nelson Teixeira Jul 10 '15 at 14:56
  • @NelsonTeixeira - I have done that, but with no success. See my edit to the question. – kamal0808 Jul 10 '15 at 14:58
  • If you've got `+SymLinks` configured in Apache it might ... I think. – CD001 Jul 10 '15 at 14:58
  • @VolkerK - then does it have to do with Chrome being sandboxed? – kamal0808 Jul 10 '15 at 14:58
  • Yes, the exact same would happen if you servered just a plain html file with the same contents as the output of the php script. – VolkerK Jul 10 '15 at 14:59
  • @CD001 - i tried it with FollowSymLinks. See the question edit – kamal0808 Jul 10 '15 at 15:01
  • @VolkerK - is there some workaround to allow chrome or any browser to access the local file system? – kamal0808 Jul 10 '15 at 15:01
  • I was talking about symbolic links IN THE FILESYSTEM. But I guess apache alias also might work. – Nelson Teixeira Jul 10 '15 at 15:02
  • @NelsonTeixeira - no it does not work. how to setup symbolic links in the filesystem? might as well try that! – kamal0808 Jul 10 '15 at 15:09
  • @kamal0808 as I told you I don't work with windows for a long time but AFAI remeber it was SHIFT-something and drag the folder icon. In command line I really don't know. – Nelson Teixeira Jul 10 '15 at 15:11
  • @NelsonTeixeira - is there no way I can access the local filesystem? where do websites with huge amount of images store them? i store them in external drives and removable storages, because it is for offline usage. – kamal0808 Jul 10 '15 at 15:13
  • 1
    PHP can access the local file system of the server machine and serve (i.e. send) any content via http. The browser can access the local file system of the client machine, but since it's handling a document that was sent via http (i.e. not part of the client's file system) it doesn't allow simple, direct access to the client's file system from "within" the document. And that's true even if by coincidence server and client are on the same machine. Access is possible e.g. via the html5 file api, see http://www.html5rocks.com/en/tutorials/file/dndfiles/ – VolkerK Jul 10 '15 at 15:28
  • @VolkerK - the link you provided is for reading local files through javascript. Accessing a large number of files makes the system slow using javascript, and takes a lot of time. If there was some way I could directly access the shared folders through a glob() in php, it would have been better. – kamal0808 Jul 10 '15 at 15:42
  • The webserver and the html browser both run on the same system, right? In that case: You can access the directory via glob() in your php script. But you cannot simply put src="D:/farewell/new/IMG_0603.JPG" in the document that was served via http. – VolkerK Jul 10 '15 at 15:46
  • @VolkerK - Yes they both run on the same system. I can access the directory via glob(), but I am unable to display the images from that directory. It simply displays the same error message for all the images - "Not allowed to load local resource" – kamal0808 Jul 10 '15 at 16:45
  • 1
    Yes, and the circle starts over: Goto comment #1 ;-) – VolkerK Jul 10 '15 at 16:52

1 Answers1

1

It is possible - this is the Apache 2.4 Windows version. Note: Require all granted as mentioned here: Alias 403 Forbidden with Apache

Alias /farewell "D:/farewell/2/"
<Directory "D:/farewell/2/">
    Options Indexes FollowSymLinks MultiViews ExecCGI
    AllowOverride all 
    Require all granted
</Directory>

... and that should do it; you should be able to access images with something to the effect of:

<img src="/farewell/[YOUR_IMAGE]" ... />

To display all the images in the folder you can do something like:

$aF = scandir("D:/farewell/2");

foreach($aF as $file) {
  if(preg_match('/\.jpg|\.gif|\.png/', $file)) {
    echo "<img src=\"/farewell/{$file}\" alt=\"{$file}\" />\n";
  }
}

Technically you are displaying the images over HTTP but you're retrieving the image filenames from the filesystem with scandir() - this is, functionally, pretty much what you're after.

Simply doing <img src=\"file:///D:/farewell/2/{$file}\" ... /> won't work in any modern browser (as far as I'm aware).


glob() version (tested with my Steam screenshots folder)

foreach(glob("D:/farewell/2/*.{jpg,gif,png}", GLOB_BRACE) as $file) {
  $sFile = strrchr($file, "/");
  echo "<div><img src=\"/farewell{$sFile}\" alt=\"{$sFile}\" /></div>\n";
}
Community
  • 1
  • 1
CD001
  • 8,332
  • 3
  • 24
  • 28
  • But keep in mind that the images will be served via http, not directly read from the local file system by the browser. – VolkerK Jul 10 '15 at 16:06
  • See, that is the difference. I want to read directly from the local filesystem, as i want to display all the images present in this folder. This is possible using glob(), which required a directory path instead of a URL. – kamal0808 Jul 10 '15 at 16:43
  • http and displaying all images is not a contradication. – VolkerK Jul 10 '15 at 16:53
  • @VolkerK if that, would you be kind enough to answer http://stackoverflow.com/questions/31333171/access-all-the-contents-of-an-alias-directory-in-php here. I would gladly accept your answer as the best answer – kamal0808 Jul 10 '15 at 17:02
  • @kamal0808 - updated, this is functionally what you're after for displaying all images, in a given folder, on a web page... if it's going on a web page, even on a local server, the images **will** be retrieved via HTTP. – CD001 Jul 10 '15 at 17:23
  • @CD001 - that scandir() doesn't scan the directory! your updated code doesn't produce any result – kamal0808 Jul 10 '15 at 17:34
  • @kamal0808 - works on my machine (with different filepaths that actually exist on my machine of course) and scanning the directory is exactly what `scandir()` is for : http://php.net/manual/en/function.scandir.php – CD001 Jul 10 '15 at 17:35
  • yes it scans the directories, but only the ones present inside the webserver root. if this is not the case, then there might be some problems with my apache configuration which i am never going to resolve. :( – kamal0808 Jul 10 '15 at 17:55
  • @kamal0808 Nope - it scans *any* directory - it works the same as `glob()` but doesn't take a regular expression pattern to filter the files beforehand - I'll add a `glob()` version since you seem familiar with that. – CD001 Jul 10 '15 at 17:57
  • @kamal0808 - actually, scratch that, `glob()` is worse, it returns the full filepaths (which you don't want) **and** doesn't really do PCRE compliamt matching on filenames so filtering down to *jpg*, *gif* or *png* is actually no less effort than simply using `scandir()` ... so `scandir()` is the better solution. – CD001 Jul 10 '15 at 18:14
  • @CD001 - thanks for your efforts. However, i've got to figure why the scandir() doesn't work in my case. I'm sure I'll be able to make it work. Will get back and accept your answer – kamal0808 Jul 10 '15 at 18:19
  • everything was fine. The image files had .JPG extension instead of .jpg, although I'm not sure why that mattered. However, it worked. Thanks millions! – kamal0808 Jul 10 '15 at 18:29
  • HUZZAH! :D glad to get that sorted! – CD001 Jul 10 '15 at 18:31