Reputation: 267
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.
Upvotes: 5
Views: 9111
Reputation: 4360
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'))";
?>
Upvotes: 6
Reputation: 12589
For each file uploaded, generate a UUID and use that for the filename on disk. That avoids collisions, sanitizing filenames and path traversal vulnerabilities.
You'll have a table like this: (id, description, filename)
with values like (1, "Green field", "0D729DCD-5116-4480-81CE-90A0380B557A.png")
.
Next, you want to avoid the problem of having too many files in one folder — you'll hit a filesystem limitation for many FSes.
To work around this problem, create directories based on the first few letters of the filename. For 0D729DCD-5116-4480-81CE-90A0380B557A.png
, you would store it in /0/D/7/0D729DCD-5116-4480-81CE-90A0380B557A.png
.
Upvotes: 6