2016-02-06 122 views
-1

我有一个问题,显示存储在我的debian服务器上的图像存储在web服务器可访问的路径之外,以保证它安全。 Here's链接到预期的结果,以及下面的相关PHP代码:用php显示图像png的问题

header("content-type: image/png"); 
if (isset($_POST['mot_de_passe']) AND $_POST['mot_de_passe'] == "superPassword") { 
    // If password is valid 
    $filename = "/home/image/test.png"; 
    $handle = fopen($filename, "rb"); 
    $contents = fread($handle, filesize($filename)); 
    fclose($handle); 
    echo $contents; 
} else { 
    // If password is invalid 
    echo '<p>Mot de passe incorrect</p>'; 
} 
+0

什么是'v'标志吗['fopen'(http://php.net/manual/en/function.fopen.php)?顺便说一句,还有['file_get_contents'](http://php.net/manual/en/function.file-get-contents.php)和['readfile'](http://php.net/manual/ EN/function.readfile.php)。 – Kenney

+0

这是一个小错误,但修复后没有改变结果,并且我尝试了file_get_content并显示相同。 – Amulus

+0

好吧,'header(“Content-Type:image/png”); readfile(“/home/image/test.php”);'应该工作正常 - 如果该文件存在! *文件路径*'/ home/image/test.png'和''之间有*区别:后者低于Web服务器的DocumentRoot。 – Kenney

回答

0

这下面的语句应该是你的脚本的第一行。意味着在任何html输出之前。这解决了问题

header("content-type: image/png"); 
+0

没有工作,我编辑我的帖子以显示您的完整代码 – Amulus

0

尝试使用r+

fopen($filename, "r+"); 

因为你说的Debian我有一个问题:PHP和httpd的访问读取目录(某处我的Debian)和文件(图片)?

chmod

+0

可以肯定,我已经在映像目录上完成了chmod 777 -R。 – Amulus

+0

和图像呢? 'cd/home/some_dir'和'chmod -R 777 *'。您控制了目录和图像文件之后的权限? – Michael

+0

目录和文件的ls -n'结果: '-rwxrwxrwx 1 1000 33 71658 Feb 6 12:42 test.png' 'drwxrwxrwx 2 0 0 4096 Feb 6 13:44 image' – Amulus