Hi,

Does anyone know if it's possible to display images from a network drive?

my php runs as 'serveruser' and my network drive ( //net1/img/ )got a user called 'serveruser'

I'm able to see the images with php but when I try to display them with

<img src="file://///net1/img/image1.jpg" />

nothing happens. But if I use the above code from my desktop(file system rather than web server) in a ordinary html file it works.

Does anybody have a solution for this? I need to be able to show the images through php

thanks
G

    Hi There,

    PHP is a server side scripted language, so it may be a browser related issue, or an escaping issue.

    If you put an HTML file with the same code that works on your desktop on the server, it should yield the same result.

    With a file:/// this will tell the browser that it will look for the local file from the computer DISPLAYING the page, not from the server.

    Why are you trying to display an image from a network drive anyways?

    Is there a reason for not keeping it on the web server?

      you could write a proxy script to feed up the images. For instance, in your script that outputs html, you could have something like this:

      $img = "//net1/img/image1.jpg";
      
      echo '<img src="getImage.php?i=' . urlencode($img) . '">';
      

      then the file getImage.php might look like this:

      $img = $_GET['i'];
      $ext = substr($img, -3);
      switch ($ext) {
        case 'jpg':
          $mime = 'image/jpeg';
          break;
        case 'gif':
          $mime = 'image/gif';
          break;
        case 'png':
          $mime = 'image/png';
          break;
        default: $mime = false;
      }
      if ($mime) {
        header('Content-type: '.$mime);
      header('Content-length: '.filesize($img));
      
      $file = @fopen($img, 'rb');
      if ($file) {
        fpassthru($file);
        exit; 
      }
      

      Or something like that. Try googling 'php image proxy'

        sneakyimp;10906484 wrote:

        you could write a proxy script to feed up the images. For instance, in your script that outputs html, you could have something like this:

        $img = "//net1/img/image1.jpg";
        
        echo '<img src="getImage.php?i=' . urlencode($img) . '">';
        

        then the file getImage.php might look like this:

        $img = $_GET['i'];
        $ext = substr($img, -3);
        switch ($ext) {
          case 'jpg':
            $mime = 'image/jpeg';
            break;
          case 'gif':
            $mime = 'image/gif';
            break;
          case 'png':
            $mime = 'image/png';
            break;
          default: $mime = false;
        }
        if ($mime) {
          header('Content-type: '.$mime);
        header('Content-length: '.filesize($img));
        
        $file = @fopen($img, 'rb');
        if ($file) {
          fpassthru($file);
          exit; 
        }
        

        Or something like that. Try googling 'php image proxy'

        Thanks! 🙂 that solved the problem

          big.nerd;10906482 wrote:

          Hi There,

          PHP is a server side scripted language, so it may be a browser related issue, or an escaping issue.

          If you put an HTML file with the same code that works on your desktop on the server, it should yield the same result.

          With a file:/// this will tell the browser that it will look for the local file from the computer DISPLAYING the page, not from the server.

          Why are you trying to display an image from a network drive anyways?

          Is there a reason for not keeping it on the web server?

          The reason I keep the files on the network is that it's not a Internet site but a Intranet and the images needs to be available to other parts of the business. So keeping them on the network means that other parts of the business can update the image catalogue with images without touching the Intranet server...

            As a side note, you could use the file://// protocol to load images, but it's not recommended.

            UNC paths under all versions of NT/XP are referenced using back slashes, but because of the PHP's escape strings you'll need to double them up.

            In your case, the following would load the file from your UNC path

            file:////\net1\img\image1.jpg

            However, this would NOT be portable across a samba share, so thus would not work if you where running from a linux or unix server via a shared UNC path.

            Under windows the best way to use the file:// protocol directly is to assign a drive letter to the share, you can do this by clicking on your share name in windows network neighborhood and clicking on 'Map network drive' , you should then select the drive letter you want to assign.

            Once you've done this, you can then reference your images as follows:

            file://z//img//image1.jpg

            replacing the 'z' with whichever drive letter you selected.

            This is not just a PHP thing, it's also a problem when doing the same thing in ASP/C# .NET as well.

            The image proxy is the most sensible way (and the most OS portable) to do this as shown above, UNC paths however are quite ok once you get used to them, it's the slash differences between OS's that tend to confuse people.

            Cheers

            Shawty

              Write a Reply...