Jump to content

MySQL File Storage


$Three3

Recommended Posts

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?

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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..

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.