通过本地路径显示图像 - file:\\\c:\ using PHP

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3671447/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:38:48  来源:igfitidea点击:

Display image via local path - file:\\\c:\ using PHP

php

提问by X10nD

I want to load image via c:/user_name/image/image_name.jpgusing http://intranet/user/index.php.

我想通过c:/user_name/image/image_name.jpg使用加载图像http://intranet/user/index.php

<img src="file:///c:/user_name/image/image_name.jpg">

How do I display them?

我如何显示它们?

Thanks Jean

谢谢让

回答by Marc B

You realize this would only work if that image is in the exact same location on every machine which will be viewing this page? Is there any reason you can't serve up the image normally, via the web server itself?

您是否意识到这仅在该图像位于将查看此页面的每台机器上的完全相同位置时才有效?是否有任何原因无法通过 Web 服务器本身正常提供图像?

Since you're using an absolute Windows path, this would only work on Windows machines, which actually have a C drive, the same directories, etc... It won't work at all on a Mac or Linux or whatever else box, since they don't bother with drive letters.

由于您使用的是绝对 Windows 路径,因此这只适用于 Windows 计算机,这些计算机实际上具有 C 驱动器、相同的目录等……它在 Mac 或 Linux 或其他任何机器上根本无法使用,因为他们不打扰驱动器号。

followup:

跟进:

after pondering your question a bit, it looks like you want to serve up a specific image that's not stored in your document root and serve it from a specific page. If you put something like this at the start of your index.php:

稍微思考一下您的问题后,您似乎想要提供未存储在文档根目录中的特定图像并从特定页面提供它。如果你在 index.php 的开头放了这样的东西:

<?php
    if (isset($_GET['username']) && isset($_GET['image'])) {
       $user = $_GET['username'];
       $image = $_GET['image'];

       $path = "C:\{$user}\image\{$image}";
       if (is_readable($path)) {
           $info = getimagesize($path);
           if ($info !== FALSE) {
               header("Content-type: {$info['mime']}");
               readfile($path);
               exit();
           }
       }
    }
?>

and within the HTML:

并在 HTML 中:

<img src="index.php?user=user_name&image=image_name" />

Of course, this is very basic, and serving up files this way is highly insecure, but most likely this is the basics of what you wanted.

当然,这是非常基础的,以这种方式提供文件是非常不安全的,但很可能这是您想要的基础知识。

回答by Manikandan Namasivayam

<?php
   $path = "C:\xampp\htdocs\Create.jpg";       
   if (is_readable($path)) 
   {
       $info = getimagesize($path);
       if ($info !== FALSE) 
       {
           header("Content-type: {$info['mime']}");
           readfile($path);
           echo '<img src="data:image/jpeg;base64,'. base64_encode($path) .'" height="100" width="100"/>';    
       }       
  }     

?>

?>