Identify folder with PHP - php

I'm making an image gallery script with PHP which is including a comment system.
The basic function of the script is to read the folder's name and to show a gallery with the folder's
name as title and it's images.
The problem is, that the comment system, which is made with mysql saves a comment like this:
ID | Folder | Image
This works great, but when I change the name of the folder, the script isn't working because there is set the wrong foldername in the Mysql table.
Also when I just save the image's name there is the possibillity of having two pictures in diffrent
folders with the same name.
Is it possible to identify a folder without using it's name?
//Note:
Thank you for your help, I think I found a solution. Now I just save the md5 hash of the image and save it in the Database instead of the foldername.

I guess it's better use another table where you'll save folders ids and their names
folder_id | folder_name
Something like that. Relying on folder names is not a good practice

No. You need to synchronize your filesystem with your database. Whenever you rename/remove/edit a folder, update its entry in the mysql database.

One possibility could be to look at using a checksum of the image contents as a unique identifier. As long as the content doesn't change then you should be OK. As far as identifying changing names of the folders I can't think of any cunning way of detecting this unless you look at using a hash that 's a function of the files contained in it.
I think to be honest that it would be good to re-think the way you're storing/identifying these items in the database.

You could always just do it this way:
Create 3 tables:
galleries -> with the columns
(id,title)
files -> with the columns (id,
gallery, name, filename)
comments -> with the columns (id,
file_id, comment)
You would then create all your galleries with their name in the "galleries" table. When you created the gallery you would put all the images that you want to display in a folder, for example uploads/. You would then insert the name of the file (Maybe generate random filenames too) in the database along with it's real name. For example "Dolphins at Beach" could point to dolphins1234142.png that's in the uploads folder. All your comments would then be in the comments table where you would setup a relationship to the files table. This will allow you to have comments for a certain image.
Just my view on a gallery system, hope it makes sense.

Related

How would I make something like puush

So I have my own webserver now and am hosting a website. I have made a basic screenshot to FTP. It copies the link once uploaded and I'm going to be adding randomized folders e.g /push/eqw8/woeqwe.jpg like puush does.
However, this is not my question. I was wondering how I would be able to grab all folders and images from those folders and create a table like this http://puu.sh/oEyfP/783483492c.png or http://puush.me/account in order from upload date. And then it shows in oderder of date uploaded.
I just wanted to do this because I feel more secure and its a fun project doing it.
How would I get about making this.
can you tell me like what I need to do in words?
I'm not good with web development and only good with languages like c# and such.
Learning php and html atm and its pretty basic but I still need time to learn.
At first it depends on your php skills about how much you can think / do in php.
The main thing you need here to order the files / folders by date or manage them at much professional level , you have to use Mysql database and store Files and Folders information inside the database.
For example , let say you have a Image file extension .jpeg then store it's information about :
Extension File size Parent Folder Uploader id / IP Upload Date Unique random key mime type Random name Original name
And there are many other things you need to consider
Files that are being stored by users should be scanned and non executable from the browser.
Store them in random folders with random names so no body can find the way to the file.
You can use javascript/ ajax for uploading multiple files at the same time.
You can make the files non executable by removing their extensions and saving them inside a folder without their original names and save their original names and extensions inside the database along with random string name and the folder name where it was stored so your system can identify the file and you can show your users the file and then on download you can replace the name with original one along with extension.
this is a one of my projects i have created , if you want you can check it out: Buckty
About any other information you need , you can ask me below in comment.

How to store user's profile picture in mysql

I am developing a chat application for iOS.
In this app, the users can set up an image as their profile picture.
So my question is, how can i be able to store images in mysql ?
I have seen that many people say, just store the link to the image(on device) in mysql, but how will the images be available on different iOS Devices, from a database right ?
I have also tried using BLOB, but when the table rows are displayed(json encoded), the value for BLOB field comes out to be NULL.
Please answer in brief.
Thanks,
This is fairly simple :) The physical image will be stored on a server and you will store in your DB only the image name, or the relative path to the image, or however you want it. So, if you like, you will have to store in your DB a "pointer" to that image.
So:
- Image in folder on the server
- In DB -> path/to/file or file_name.format OR if you know you path, and you know your format just file_name
Hope this helps! :D
May be late But I just found way to do that.
Way 1: Get image from user and rename with the id (Pref. Primary key) and in Your public_html create folder for images. Set proper permissions for that folder. and save image in that folder and the url of that image will be stored DB column . Like
https://www.example.com/images/user1.png do in that style.
Way 2: use other things to rename that image like user's email,username etc
way 3: In above 2 cases it may happen that other users,hackers may try to download images by using IDs,emails etc.
That's why , another way you can do is you can generate a hash for profile and check if the hash already exists in column if exits then generate another one. Likely I don't think there is possibility of generating similar hashes.but you may check to avoid error in future. and now rename image using that generated hash.
You may also reduce size of that link column using only storing generated hash and in your app declare some variables and achieve
https://www.example.com/images/generated_hash.png
here the url will be same in all columns excluding that hash key.
you may only store that hashes.
Hope it will help
Reference from : https://www.techupdates.live/how-to-save-profile-picture-in-php-in-5-simple-steps/

Image upload in PHP with mySQL

I am reading a lot about uploading images with PHP. I have come to the conclusion that it's best to have a folder /images to keep the files. And have a db table that holds the path to the file and i'd also like to keep track of what it is an image of (in my case houses).
I would have 3 fields in the table:
id
unit
image
Does this sound like the proper way to handle this? I am unable to find any definitive article on how to do this.
Also, when it comes to uploads, are there any recommended articles on how to accomplish this in the manner I want?
Yes it is.
Although mysql lets you store images into records just as strings or numbers it is generally preferred to keep separated "records" from "files".
I'd suggest to avoid recording the path to the image.
Instead, as the image is associated with a record, give the image a name that links it to the record. Assuming your table have an unique, primary key, auto-increment, integer field named id each image will have filename id.jpg
After the upload you'll move the image from the php upload directory to the images directory renaming the file.
For information on how manage file uploads with php google for php upload files

storing and showing images

I still really don't understand image storing and showing.
Let me give you an example, what I did first (people say my logic is a bad idea).
I did at first, every user has a folder with an user_id, and after the form was submitted it saves the image with an unique name in to this folder and the image name too, with the extension in the database, and I've shown the image matching the file name in the folder with the saved name in the database,
Example (here let's skip the unique name just to make it more clear a bit)
an user uploads an image named butterfly.jpg
the image is stored in the database
id | name |
1 | butterfly.jpg |
and stored in a folder too with the same name
/images/user_id/butterfly.jpg
and showing the image this way
<img src="images/unser_id/<?php echo $name ?>" alt="">
So it matches the names (I hope I was clear sorry if not)
So I know my logic is a bad logic, because storing the names in the database will lead to a huge one, maybe to other problems in the later future
And what I would like to ask if its not a big request, if someone could write down a logical thread with some example codes (I'm not asking to write it for me), just to give me the idea what I'm missing
And other questions about what I read about
Some developers say, they only store the image folder name in the database, that is okay, but how I am able to show those images if only the folder name is stored? because it will maybe have more than one image like 3 one the original image and 2 other thumbnails.
So please don't hate me with this question, I only asked this because I would like to learn and get advice from a more experienced developer.
I think you're on the right path. The correct way of doing it is - to upload the file to your server and then put the image path into your database, as far as i can tell, you're doing it right. :-)

PHP file rename

I have a form where an admin will upload three pictures with different dimensions to three different designated directories. now to make sure that i don't get into the problem of duplicate file names i implemented something like the php will compare the uploaded file name and it will check if that file name exist in the designated directory if yes then it will echo an error and stop the script execution.
Now one of my friend suggested me that it is very bad asking the admin to manually rename the picture file and asking them to take care of the file duplication problem. the solution he suggested was to rename the file automatically and then store it in the database and then direct it to directory.
I am confused about what combination should i give to the renamed file and also make sure it will remain unique file name to be more precise i would like you to understand my directory structure
as i said there will be three pictures files the admin will be uploading namely
a) Title Picture b) Brief Picture c)
Detail Picture
and all the three picture files will be moved to the different respective directory, like title picture goes to title directory and so on.
i am using to script below currently just to move and store the file name with path using varchar in the database.
$ns_pic_title_loc= $_FILES["ns_pic_title"]["tmp_name"];
$ns_pic_title_name = $_FILES["ns_pic_title"]["name"];
move_uploaded_file($ns_pic_title_loc, $ns_title_target.$ns_pic_title_name) or die(mysql_error());
that is just the sample code i havent included the validation function which i am using. i was thinking like i want to rename all the files like
a) In title directory the file should be stored as.
title_1.jpg
title_2.jpg
title_3.jpg
title_4.jpg
and so on
and the same way to rest of the pictures. how do i do that? what function do i use to achieve my target. and if this is not the good way to rename the file i would appreciate any suggestion followed to rename the file.
thanks in advance
Well, here's a possible solution:
Get uploaded filename from $_FILES["ns_pic_title"]["name"] and separate extension OR if we are only talking about image files get the image type with getimagesize($_FILES["ns_pic_title"]["tmp_name"]);
Check your database for the maximum id of the image records and make the the $file_name variable 'title_'.($max_id + 1)
At this point you should have $file_name and $file_extension so do move_uploaded_file($_FILES["ns_pic_title"]["tmp_name"], $ns_title_target.$file_name.'.'.$file_extension)
Hopefully this makes sense and helps.
There are a couple of good options with various pros and cons.
Use php's tempnam when moving the file, and store the path in your mysql database. tempnam generates a unique filename.
Use mysql to store the image content in a blob. This way you will access the image content via an id instead of a pathname.
Instead of having logic to figure out what the latest picture name is and calculate the next number increment, why not just use PHP's tempnam() function? It generates an unique name with a prefix of your choice (i.e., "title", "brief", "detail"). You could also simply prepend a timestamp to the file name -- if you don't have a whole lot of admins uploading pictures at the same time, that should handle most name conflicts.
Since your pictures are going to be sorted into title, brief and detail directories already, it's not really necessary to name each picture title_*, brief_*, and detail_*, right? If it's in the title directory, then it's obviously a title picture.
Also, you're going to be putting the file names in the database. Then elsewhere in the app, when you want to display a picture, I assume you are getting the correct file name from the database. So it isn't really important what the actual file name is as long as the application knows where to find it. If that's correct, it's not necessary to have a very friendly name, thus a tempnam() file name or a timestamp plus the original file name would be acceptable.
Because you are storing references into the DB, I would prefer to just md5 the datetime and use that for the filename and store the disk filename to the DB also. It doesn't matter what name it is written to disk with as long as you can point to it with the unique name into the DB.
I use this methodology, and in none of my testing does the disk name (md5 from the datetime) ever require multiple tries.

Categories