Loading file links from database with PHP - php

As a beginner I need some information. I've been trying to piece together bits for an online user uploading and sharing site like newgrounds.com
I don't have any code so far, just questions.
My big one at the moment, is how can I display SQL entries as file links? The idea is when a user uploads a file, an entry is created with the username, file name and file location. The file location will be a folder relevant to the user's username. But how will I create a link to that file onto a most recent uploads page?

You'll need to perform an SQL query everytime you list all the files anyway, so when you do your query, make sure to include the file location field and parse it in your PHP.
For example, if you are using MySQL, you can do SELECT * FROM FILES WHERE 'user_id' = $userid ORDER BY 'date' DESC LIMIT 10 and loop through those to display all the fields in your PHP code.

Related

Add image to existing MySql record

I'm quite new to PHP and MYSQL.
I need to add an image to an existing record.
So I have a list of records and when I click the link directing to this page.
In this form I would like to ad an image.
Thanks in advance
I would highly discourge you from saving files in a database like that.
Here are the steps that I have used before:
I would create a folder on the server for each user.
Save the filename and extension in a table.
Save the file to the server folder.
If I wanted to grab a file I would go to the right directory and file name.
Examples:
Profile Picture = {userFiles/userID/filename}
Contract for Billy = {userFiles/{Billy'sID}/{filename}

grocery crud file upload field pick file from server

I'm developing a web site with codeigniter + grocery crud.
I really like the upload file feature from grocery crud.
But I'm wondering if it's possible to pick a file from a server folder instead of uploading it. I mean just put the name of the file in the field.
I don't want to write more than what is needed.
Edit: I want the user to be able to do both: upload a file, or pick it from a server folder.
I've been asking myself the same question. I was thinking in doing it like this:
1 - Create a Media Library table (Wordpress style) and store all the file uploads in a table.
2 - Add a relation field to select images form this table.
http://www.grocerycrud.com/examples/set_a_relation
The basic relation field will generate a select that allows you to select images by name, id, description, etc.
This is not very user friendly, so I guess you'll have to write one or two callback functions to show the users the image they are selecting
this is the approach I'll be using on my next codeigniter+grocerycrud project :)
Good luck!

Codeigniter - Download a upload file in the directory

I'm using the latest Codeigniter version, and I write a program about basic file uploading and download helper in http://ellislab.com/codeigniter%20/user-guide/helpers/download_helper.html.
I want to create a system that will upload a multiple files to the directory and save the file name to the database and the name of the uploader, and will have function to have download links to download every file of that specific user. If possible the system can email the encrypted link to the users to download the file. And can only download for specific time..
I don't know the logic in dynamic files to download. Can someone teach how to do this or what logic can solve this problem. Thank you very much! :)
For multiple files you have some alternatives, you can create each field as a user press a button or use the multiple propriety to <input> tag.
To manage this multiple uploads you must create your own upload library reading each $_FILES['nameoffield'] in a foreach loop for example although there are alternatives ready to be used like: https://github.com/nicdev/CodeIgniter-Multiple-File-Upload
On your database, you could have two fields that stores the original file name and path, and the encrypted one. Probably associated to a random unique number or timestamp.
To email encrypted link, and by encrypted I think you are saying a disguised link to the file, not using original name, you simple select the field which store the encrypted name to a controller, like download and keep a variable to receive a value as parameter. This value you must check on database if it really exists and then redirect to the file. By doing that you should have your file being downloaded.

How to add PDF files in a database and be able to search for them through a website using phpMyAdmin

I am working on a website and the owner wants to use PDF files to show the contents of a bill for users to see what they owe. He wants to be able to search through the website using specific search criteria. For example, the PDF number, a certain town, a certain year etc... I have a form already made to use to search for these PDF files, I just don't know how to store the PDF files in the database using phpMyAdmin. Can anyone help?
I think you should keep all the metadata and filename in the database and store the actual files in uploads/ directory. When you search for a file by its ID, city or whatever, you'll retrieve the filename and you'll be able to redirect to a particular file.
Personally I'd change all filenames to uniqid().'.pdf' so that they don't repeat and have constant length.

Access folder and select filename using PHP

I have written a code, where the user selects a profile picture and then the picture is stored in localhost/user/$username/photos/photo1.gif.
After that, I assigned the filename (photo1.gif) into a session variable so I can display it from all my php scripts. This is working just fine. I can display the picture in every php script by accessing this session variable.
The only problem I have is when I am trying to login from the login page: In the login page I connect to the database, retrieve email and password, check them and if they are OK I redirect the user to home.php. The problem is that the user's photo is not linked to the email so i cannot know the filename of the photo. The only thing I know for sure is the directory (because I can retrieve username from database as well).
Lets say that a user has uploaded 4 photos (photo1, photo2, photo3, photo4 - photo4 was uploaded last). It makes sense that he is currently using photo4 as my profile picture.
Is there a way for me to access that folder and retrieve the filename of the picture uploaded last?
Also, as a general question, what is better, store the photos(or files) in a database or server?
A few options:
It would be 'better' to create a photo table and store the user_id and the photo location in that table. Storing the actual photo in the table as a blob is not generally recommended.
Alternatively, to avoid more tables, you could rename the photos as
username_photo1.jpg
username_photo2.jpg
username_photo3.jpg
And then you can retrieve the largest of them.
Finally, another option is to get the file creation date of the photos in the directory and take the most recent photo.
see Getting the filenames of all files in a folder

Categories