坚持使用 xampp 在文件系统中实现图像存储
stuck in implementing image storage in file-system with xampp
关于我指定的问题的线程太多了,但我找不到我需要的线程,因为每个线程都与数据库和文件系统之间的差异或性能方面的差异有关。我需要的是下面指定的。
这是我遇到的一个场景。我正在开发一个汽车 Web 应用程序,我已经完成了其中的大部分工作。上传图片的问题越来越多,这对我来说是一项复杂的任务,因为我以前从未做过。
- 图像不应存储在数据库中,而应存储在文件系统中,图像的路径应存储在数据库中。这就是我熟悉的
但是,我需要知道什么是文件系统?因为我正在通过 xampp 使用 localhost。以下是我的担忧:
- xampp 接受什么扩展名的图像。 jpeg 或 png 或其他任何格式?
- 我们怎样才能完成这个任务?我需要一点点开始,因为我不知道从哪里开始。
- BLOB 和文件系统是不同的东西吗?
- 我们应该为文件夹的图像创建一个 table 吗?
如果有人已经做过或者有人有样品PHPcode/script,请提供给我。将不胜感激:)
What extension does xampp accepts for images. jpeg or png or anything else ?
XAMPP 接受任何文件。作为程序员,您需要验证和确认用户确实只上传了图像。有多种方法可以做到这一点。 For example something done in this question.
How can we implement this task ? i need a little start cuz i don't know where to start.
这是一个非常宽泛的问题。您希望如何执行此操作取决于您的需要以及应用程序其余部分的结构。只是简单的上传功能实际上非常非常简单。只是 Google "php file upload example" 应该会给你很多例子。例如 this.
Are the BLOB and file-system different things?
文件系统就像你机器上的文件。 Web 服务器(和 XAMPP)只是一台计算机 运行 一些软件来回答您的请求。当你上传一个文件到这个服务器,基本上就等同于在你的机器上从互联网上下载一个文件。您计算机上“我的文档”下的文件与通过 Web 表单上传的文件基本相同。 BLOB 是存储在数据库中的二进制数据的集合。这种格式对于存储巨大的价值很有用。与您无关。
Should we create a table for images of a folder?
这真的取决于你想怎么做。您可以将所有图像放在一个目录中。但是,如果此目录中有大量图像,则速度可能会变慢。常见的模式是使用文件名的第一个字母并为其创建一个目录。例如,像这样:
// Upload file target
$uploadDirectory = '/path/to/directory/';
// Create a "random" hash
$randomHash = md5(rand() . '---' . time());
// Name the file randomhash.fileending
$fileName = $ranomHash . '.' . $fileEnding;
// Find the first letter of the filename
$firstLetter = substr($fileName, 0, 1);
if (!file_exists($uploadDirectory . $firstLetter)) {
// Directory does not exists, create it
makedir($uploadDirectory . $firstLetter);
}
// Final path is now constructed
$finalPath = $uploadDirectory . $firstLetter . '/' . $fileName;
// For example, if your file is named aabbcc.png, then the $finalPath is:
// /path/to/directory/a/aabbcc.png
// When you have moved the file to this directory, you can store this string in the database
关于我指定的问题的线程太多了,但我找不到我需要的线程,因为每个线程都与数据库和文件系统之间的差异或性能方面的差异有关。我需要的是下面指定的。
这是我遇到的一个场景。我正在开发一个汽车 Web 应用程序,我已经完成了其中的大部分工作。上传图片的问题越来越多,这对我来说是一项复杂的任务,因为我以前从未做过。
- 图像不应存储在数据库中,而应存储在文件系统中,图像的路径应存储在数据库中。这就是我熟悉的
但是,我需要知道什么是文件系统?因为我正在通过 xampp 使用 localhost。以下是我的担忧:
- xampp 接受什么扩展名的图像。 jpeg 或 png 或其他任何格式?
- 我们怎样才能完成这个任务?我需要一点点开始,因为我不知道从哪里开始。
- BLOB 和文件系统是不同的东西吗?
- 我们应该为文件夹的图像创建一个 table 吗?
如果有人已经做过或者有人有样品PHPcode/script,请提供给我。将不胜感激:)
What extension does xampp accepts for images. jpeg or png or anything else ?
XAMPP 接受任何文件。作为程序员,您需要验证和确认用户确实只上传了图像。有多种方法可以做到这一点。 For example something done in this question.
How can we implement this task ? i need a little start cuz i don't know where to start.
这是一个非常宽泛的问题。您希望如何执行此操作取决于您的需要以及应用程序其余部分的结构。只是简单的上传功能实际上非常非常简单。只是 Google "php file upload example" 应该会给你很多例子。例如 this.
Are the BLOB and file-system different things?
文件系统就像你机器上的文件。 Web 服务器(和 XAMPP)只是一台计算机 运行 一些软件来回答您的请求。当你上传一个文件到这个服务器,基本上就等同于在你的机器上从互联网上下载一个文件。您计算机上“我的文档”下的文件与通过 Web 表单上传的文件基本相同。 BLOB 是存储在数据库中的二进制数据的集合。这种格式对于存储巨大的价值很有用。与您无关。
Should we create a table for images of a folder?
这真的取决于你想怎么做。您可以将所有图像放在一个目录中。但是,如果此目录中有大量图像,则速度可能会变慢。常见的模式是使用文件名的第一个字母并为其创建一个目录。例如,像这样:
// Upload file target
$uploadDirectory = '/path/to/directory/';
// Create a "random" hash
$randomHash = md5(rand() . '---' . time());
// Name the file randomhash.fileending
$fileName = $ranomHash . '.' . $fileEnding;
// Find the first letter of the filename
$firstLetter = substr($fileName, 0, 1);
if (!file_exists($uploadDirectory . $firstLetter)) {
// Directory does not exists, create it
makedir($uploadDirectory . $firstLetter);
}
// Final path is now constructed
$finalPath = $uploadDirectory . $firstLetter . '/' . $fileName;
// For example, if your file is named aabbcc.png, then the $finalPath is:
// /path/to/directory/a/aabbcc.png
// When you have moved the file to this directory, you can store this string in the database