File upload insert into database - php

I am struggling to come up with a process to upload a file with a form i am working on an application that needs the user to upload a file then insert it into a database.
i have setup a database with.
TABLE - tracks
track_name
track_bpm
track_rate
track_ref
track_special
track_file // This is where i need the uploaded file to go
I am stuck on process here is where i have got so far.
http://html5up.users35.interdns.co.uk/
I am looking at doing a you tube style filling out the form while your file uploads, the file could be up to 2gig, then insert file when completed into sessions id here is a snippet of my file-upload.php
mysql_query("UPDATE tracks SET track_file = '".$file_name."'
WHERE session_id = '".$_SESSION['id']."'");
Can someone please help me with the correct process or at least a decent tutorial or way off doing this correctly been Googling for ages.
Is passing a session id the correct way to do this?
Any help please everyone

Advices:
mysql_* functions will be deprecated soon, use PDO or mysqli
Don't store files in the database, try to store only paths to the files.
Learn, how to sanitize SQL-queries.

You can store your files into the database (See Why store binary data in MySQL?), in MySQL that's done in a BLOB field.
You insert the data into the table by reading it from the uploaded file and then send that data over to the database with a standard SQL query.
There are plenty of tutorials available on the web, one is Uploading Files To MySQL Database.
If you store the file on disk, then just store the file on disk. Just take care that the insertion/updating/deleting of the data in the database is atomic with the operation on the file (creation of the file/updating;renaming/deletion).

Related

How can download files from longblob mysql without (extension,type,size) fields

I searched a lot about how to download files from longblob mysql but all the code requests (extension,type,size) and i do not make These fields and i do not want make it . SO, Is there any way to download any file type (jpg,gif,pdf,png,txt,ect..) from the database without (extension,type,size) fields ?
It's not a good idea to save a file in your database. It's an old tradition. Try to come outside. Save the file in the directory and after that save the file name or path in the database. This will decrease the load on the database and also decrease unusual use of the db storage
A couple things here:
Do NOT store files in the database. The database is for data, the
file system is for files. The smart way to do something like this is
to have a varchar storing /path/to/your/file.foo
Unless you plan to examine each file (costly and slow), you benefit GREATLY from just storing this data for re-use in the future

Displaying uploaded files using PHO

I'm creating an interactive website where the user can upload a file.All I want to do is how can I let the other users see this file? For example if some datas are saved into database I can make this using SELECT and then print these data. But if I have a file how can I make this file to be seen only (not downloaded) from users?
So I just want to know what can I use to make this and don't want you to make this for me! So please if anyone has an ide please tell what should I learn to make this? Is it possible to save the file in a database in phpMyAdmin and to select then? If yes which is the type of the field I should use to save the file so varchar?
It depends on how you want the file to be saved. If it's a text file I would go with text. If it's an image or some other kind of media I would pick blob. You can take a look at What are the differences between the BLOB and TEXT datatypes in MySQL? for more information on MySQL data types.
Another approach would be to save the file somewhere outside the database (in a directory tree somewhere) and save a filepath to that location in a varchar column. Many DBMS do not support group by on blob or clob columns so that is something to consider if portability is an issue, and it adds a layer of separation between your data and file storage.

creating a text file is good or saving it to database is good?

I am creating an application where users will post texts. Currently I am creating the text files of that texts post but i am not sure if storing it to MySql table is good or creating a txt file is good. .
Note:
1) There is no limit of the size of text post. Users may post a very big text or user me post containing only a letter.
2) There is no limit of post a user can upload.
Please Help
Thank You. .
Saving data into a file is more cost operation rather than in database.So its better to go for database.Because saving in file will do IO operation and this file I/O operation is really very costly in terms of computation and from OS perspectives.
You should store it in the database instead of text file. There are many reasons for it. Go through this question for more descriptive answer. In short, you should choose database over files for security reason, speed, code maintainability and concurrency.
You can use LOAD DATA INFILE to read the contents of a file and store it in a table in the database in a structured format.
LOAD DATA INFILE 'data.txt' INTO TABLE db2.my_table;
You can also use largetext in mysql.But it would be slower and non-resilient.
Please check here
See there are some major things you should know before taking decision on where to save things
If the file is an image/Video/PDF
For this you should save the FILE NAME into the database and the image on server
If the file is a large text
For this you should save the file to the server as the database will not be able to take loads of text and many requests for long text at the same time and this will also save you space for more things in database
For any other thing, if you find it space consuming just save it on the server as this will give you space for database and more advantages

Upload Image into SQL database and then retrieve it from the database and display it

I'm currently facing a problem. I do not know how to upload image into mysql database.
And then after that i will need to retrieve from the database and display it.
It's actually because i need to create an online catalogue that includes an add to cart button. But for the images to be displayed in the online catalogue, i cannot use hard coding. I want to be able to frequently update the online catalogue whenever i have a new product in an admin page.
I can only do it for text instead of image.
if (isset($_POST['submit'])){
$name = $_POST['name'];
$add = mysql_query("INSERT INTO maritime_products(name) VALUES('$name')") or die("gg2");
}
$showname = mysql_query("SELECT * FROM maritime_products");
while($anything = mysql_fetch_array($showname)){
echo $anything['name'];
}
You DO NOT need to store images in the database.
Please, store the path to image in your database and store the image itself on the file system.
Thank you!
As Serge Velikanov already said, NO GO, store your images on the server where you host your website and only save the URL of the picture into the database. Reason: If you change the size of your image, you simply upload again to the FTP and it's done, no need to save it into the database again.
But if you still plan to do it, you might take a look at BLOB, OLE Object.
Pro and Cons you can find here (not written by me):
My experience has been that uploading large images or other binary objects to SQL Server is not very performant and is not completely reliable. The SQL Server team has made it a little better over the years (varbinary(max), etc.) but they haven't made a lot of improvement in performance. If you are going to do this, I would suggest chunking instead of uploading the entire image at once. Also, SQL Server 2008 has a new option (service) that you can run that allows the engine to stream Blob data to the file system. I haven't implemented this service as yet but I believe it works in conjunction with the Shadow Copy service and streams the file to the file system. I'm not clear on how backups would work but since a pointer would be stored in the table there's a good chance that the engine would know to backup the file along with the other data in the table. Check it out on MSDN or the SQL Server site. (Brian Custer)
And also again a duplicate?
https://stackoverflow.com/questions/805519/save-image-in-database
store image in database or in a system file?

Passing Binary Data to a Stored Procedure in SQL Server 2008

I'm trying to figure out a way to store files in a database. I know it's recommended to store files on the file system rather than the database, but the job I'm working on would highly prefer using the database to store these images (files).
There are also some constraints. I'm not an admin user, and I have to make stored procedures to execute all the commands. This hasn't been of much difficulty so far, but I cannot for the life of me establish a way to store a file (image) in the database.
When I try to use the BULK command, I get an error saying "You do not have permission to use the bulk load statement." The bulk utility seemed like the easy way to upload files to the database, but without permissions I have to figure a work-a-round.
I decided to use an HTML form with a file upload input type and handle it with PHP. The PHP calls the stored procedure and passes in the contents of the file. The problem is that now it's saying that the max length of a parameter can only be 128 characters.
Now I'm completely stuck. I don't have permissions to use the bulk command and it appears that the max length of a parameter that I can pass to the SP is 128 characters.
I expected to run into problems because binary characters and ascii characters don't mix well together, but I'm at a dead end...
Thanks
In general, we don't pass binary data in SQL. We upload the file to the server, then load the image from the server into the database.
Load the image into the database from a file:
UPDATE images
SET image = LOAD_FILE('images/myimage.jpg')
WHERE image_id = 1234
Get the image back out to a file:
SELECT image
INTO DUMPFILE 'images/myimage.jpg'
FROM images
WHERE image_id = 1234
Here is an example I've found in David Hayden's blog.
It's a c# example, but the steps should be similar in PHP:
Convert your uploaded file to a byte array
Execute dynamic TSQL on the server

Categories