0

There are so many threads about the issue i'm specifying, but i couldn't find one which i need, as each one is about the difference between db & file-system or on the performance perspective. what i need is specified bellow.

Here is a scenario i'm stuck in. I am developing a web application for cars, in which i have done most of the things. the problem is rising with uploading images which is a complex task for me because i never done it before.

  • images should not be stored in database instead they should be stored on file-system and the path to the image should be stored in db. this is what i'm familiar with

But, i need to know what actually is a file-system ? as i'm working with localhost through xampp. following are my concerns:

  • what extension does xampp accepts for images. jpeg or png or anything else ?
  • how can we implement this task ? i need a little start cuz i don't know where to start.
  • Are the BLOB and file-system different things ?
  • should we create a table for images of a folder ?

If someone has already did it or someone has a sample PHP code/script, please provide me. it would be greatly appreciated :)

  • File system = files in a directory. Presumably you're already familiar with that if you've ever used files and directories in any fashion. Nothing special there. – deceze Sep 20 '17 at 07:39
  • That's a well-written question, but still a bit broad. "XAMPP" is a distro, not a concrete server type. Files are files, and webservers don't constrain anything. If you need a database for associating file names entirely depends on how you want to display/structure or uncover images. That's impossible to generalize. – mario Sep 20 '17 at 07:40
  • If you search for "php file upload" you should be able to find a minimal reusable example that presents the web user with a file form, and which uploads a file (or just image) to the server. This should answer most of your questions. – OptimusCrime Sep 20 '17 at 07:41
  • Appreciating all the answers. i have seen a lot of videos in which developers storing images in a folder and not db's table/row. but i am unable to differentiate either they are implementing filesystem or the images are getting stored in db. – Mehran Khan Sep 20 '17 at 07:44
  • @mario yet, i am assuming the xampp as my server, so i can store and retrieve images from xampp's filesystem. this would help me verify that i'm going on right path. of course things would be a bit different when the application is going to be live. i am doing it because i don't want my application's performance to be down. can we have some personal conversation on the topic ? it would be appreciated. – Mehran Khan Sep 20 '17 at 07:50
  • Your server is called "Apache", not XAMPP (forget about it). And the underlying filesystem is rarely the bottleneck. Just don't handle/pipe any files through PHP and you'll be good. – mario Sep 20 '17 at 07:54

1 Answers1

0

What extension does xampp accepts for images. jpeg or png or anything else ?

XAMPP accepts any files. It is up to you as the programmer to validate and verify that the user is indeed uploading only images. There are a number of ways to do this. 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.

This is a very broad question. How you want to do this depends on your needs and how the rest of the application is structured. Just the bare bone uploading functionality is actually very, very simple. Just Google "php file upload example" should give you many examples. For example something like this.

Are the BLOB and file-system different things?

A file system is just like the files on you machine. A web server (and XAMPP) is just a computer running some software to answer your requests. When you upload a file to this server, it is basically the same thing as downloading a file from the Internet on your machine. The files on your machine under My Documents is basically the same as files uploaded via a web form. BLOB is a collection of binary data stored in a database. This format is useful for storing huge values. It is not relevant for you.

Should we create a table for images of a folder?

That is really up to how you want to do it. You could place all the images in a single directory. However, if there are going to be a lot of images in this directory, it may become slow. What is a common pattern is to use the first letter of the file name and create a directory for that. For example, something like this:

// 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
OptimusCrime
  • 14,662
  • 13
  • 58
  • 96
  • perfectly explained. i'm going to try this. I am very new to this and don't know much idea about that. how could i reach to you if i have more concerns ? would you be willing to help me ? – Mehran Khan Sep 20 '17 at 08:06
  • @MehranKhan The best way (for everyone) is if you follow the StackOverflow pattern. If you have a problem you are unable to solve, make it as detailed and concrete as possible. Open a new question here and include the most minimal example needed to understand/recreate your problem. This way it is easier for both you and us to understand what is going on. Simply debugging/helping/guiding someone in an open task like this is not very easy. – OptimusCrime Sep 20 '17 at 08:18