避免用户能够看到 php 中的目录

Avoiding the users to be able to see a directory in php

我目前正在开发一个商店 Web 应用程序,我正在使用 PHP 来实现它。
根据我们的业务模型,我们需要为已添加到 system.So 的每件商品提供一些图片,我已将所有图片存储在一个名为 _goods_imgs 的文件夹中。在这个文件夹中,我为每个商品创建了一个新文件夹,并将它们的图像存储在其中,当我想向 view_goods 中的用户显示图像时,我使用它们的绝对路径。我的问题是如何限制对该文件夹的访问,这样如果任何人在浏览器中键入 myshope.com/_goods_imgs 就无法看到目录。
任何帮助,将不胜感激。

在每个文件夹中,放置一个空文件或任何名为 index.html

HTML 文件

所以每当您输入图像文件夹的路径时,index.html 将 运行 而不是在文件夹中列出图像

Create a .htaccess file in your root folder and put the following code

Options -Indexes

我会使用以下代码将 index.php 文件放入其中

<?php
header("location:../");
// so every time user type that directory it will redirect to upper directory
// or you can put homepage address. 

?>