$Three3 Posted February 23, 2010 Share Posted February 23, 2010 Hi everyone, I have just got past the MySQL basics and I was wondering is there a good source on how to store files in a database such as an uploaded file or a video file? Thanks for the help in advance. Quote Link to comment Share on other sites More sharing options...
Deoctor Posted February 23, 2010 Share Posted February 23, 2010 that would eat up ur mysql space and also it is not such a good idea to store such large files in Mysql.. store it some where and give a reference of that point in mysql.. i think that would do. Quote Link to comment Share on other sites More sharing options...
aleX_hill Posted February 23, 2010 Share Posted February 23, 2010 I have to agree. I never store files in the mysql database, but instead store it in the filesystem and reference its location in the database. Quote Link to comment Share on other sites More sharing options...
$Three3 Posted February 23, 2010 Author Share Posted February 23, 2010 that would eat up ur mysql space and also it is not such a good idea to store such large files in Mysql.. store it some where and give a reference of that point in mysql.. i think that would do. I have to agree. I never store files in the mysql database, but instead store it in the filesystem and reference its location in the database. Thanks a lot for the replies. Okay, so for example, I have a website that adds 1 new video a day. My goal is to sort the videos by the newest first (Descending Order). Is this the way I would do it?: 1.) Video is uploaded to the site 2.) Insert the link, the date the video was uploaded, & size to the video in the database My question is how would I display the videos now? Is this correct so far? Quote Link to comment Share on other sites More sharing options...
aleX_hill Posted February 23, 2010 Share Posted February 23, 2010 Something like this: upload the video to videos/videoname.swf (lets say its a flash video for the moment) insert into the db an entry with the date, and "videoname.swf" like so: $date = date(); $filename = $_FILES['uploadField']['name']; mysql_query("INSERT INTO videos (date, file) VALUES ('$filename', '$date')"); Then to display, try something like: $result = mysql_query("SELECT file FROM files SORT BY date ASC"); while($row = mysql_fetch_assoc($result)) { echo $row['file']; } Obviously basics here, will probably want a more details mysql table, and convert the echo row to something a little more useful, but you get the idea. Quote Link to comment Share on other sites More sharing options...
Deoctor Posted February 23, 2010 Share Posted February 23, 2010 while storing store it with a randomly generated file name u can get the uploaded type i mean extension of the file like this $uploaded_type=strtolower(substr($_FILES['file']['name'],strrpos($_FILES['file']['name'],'.')+1)); so while storing in the database store it with this new name and then use the option like this insert into files(name,time_nw) values('blah blah.$uploaded_type', now()); what this would do is it will store the present time of the server.. so while fetching the records u can do some thing like this.. select * from files order by time_nw desc i hope this will do that Quote Link to comment Share on other sites More sharing options...
Deoctor Posted February 23, 2010 Share Posted February 23, 2010 Something like this: upload the video to videos/videoname.swf (lets say its a flash video for the moment) insert into the db an entry with the date, and "videoname.swf" like so: $date = date(); $filename = $_FILES['uploadField']['name']; mysql_query("INSERT INTO videos (date, file) VALUES ('$filename', '$date')"); Then to display, try something like: $result = mysql_query("SELECT file FROM files SORT BY date ASC"); while($row = mysql_fetch_assoc($result)) { echo $row['file']; } Obviously basics here, will probably want a more details mysql table, and convert the echo row to something a little more useful, but you get the idea. there is a small problem in uploading though.. if some one else tries to store the file with same name it will either replace the existing file or it will not upload based on your settings.. so try using a random name for the file name.. Quote Link to comment Share on other sites More sharing options...
aleX_hill Posted February 23, 2010 Share Posted February 23, 2010 there is a small problem in uploading though.. if some one else tries to store the file with same name it will either replace the existing file or it will not upload based on your settings.. so try using a random name for the file name.. I agree, hence it being a bare bones solution. While uploading with a random filename is a viable solution, if it matters what your file is called (ie if your users download them and you dont want them downloading nj44knkln.swf ) then take a look at the file_exists() function, and if it does, stop the uploading. Quote Link to comment Share on other sites More sharing options...
$Three3 Posted February 23, 2010 Author Share Posted February 23, 2010 Thanks a lot for the replies. That cleared up a lot of things for me. I will mark it as solved now Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.