Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store images to file system (on internet) and store the paths to the database?

We are trying to create a site for wallpapers so that would be somehow large files(2mb-5mb) so we'll be needing to store the images on the disk space instead in the database and only the paths to the database. So if you can give some ideas on how to do that (the method we know for now is creating a PHP script with the upload function and by manually selecting the images from the PC to be uploaded) unless you guys would have other suggestions. Tutorials will be much appreciated. Thanks a lot!

This is for the admins to add images not for the users. Note: we haven't developed any script so this is to get some ideas from you guys on what we can use with this, if none guess we will just go with the php script.

like image 244
vep temp Avatar asked Sep 30 '13 18:09

vep temp


People also ask

Where should I store image database or file system?

Generally databases are best for data and the file system is best for files. It depends what you're planning to do with the image though. If you're storing images for a web page then it's best to store them as a file on the server. The web server will very quickly find an image file and send it to a visitor.

What is the best format to store images in a database?

In order to store images in a SQL database, you first need to save each plot image in a . png format.


1 Answers

Your form,

<form action="PHP_FILE_PATH.php" method="post" enctype="multipart/form-data">
<input type="file" name="image" />
<input type="submit" name="submit" value="Upload" />
</form>

The PHP Part

<?php
if($_FILES['image']['name'])
{
  $save_path="FOLDER_PATH_TO_SAVE_UPLOADED_IMAGE"; // Folder where you wanna move the file.
  $myname = strtolower($_FILES['image']['tmp_name']); //You are renaming the file here
  move_uploaded_file($_FILES['image']['tmp_name'], $save_path.$myname); // Move the uploaded file to the desired folder
}

$inser_into_db="INSERT INTO `database`.`table` (`folder_name`, `file_name`) VALUES('$save_path', '$myname'))";

?>
like image 118
Sarvap Praharanayuthan Avatar answered Nov 14 '22 23:11

Sarvap Praharanayuthan