ktalebian Posted August 24, 2007 Share Posted August 24, 2007 Hi there! I have two questions: 1) How do I upload files to a db? 2) Once uploaded, how do I download the file? Thank you in advance Quote Link to comment https://forums.phpfreaks.com/topic/66475-solved-mysql-upload-file/ Share on other sites More sharing options...
xyn Posted August 24, 2007 Share Posted August 24, 2007 Well i would not recommend uploading to a database but upload files to a specified folder, then store the record in a MYSQL table. i'll make a code for you, just tell me: 1. what sorts of files do you want to upload? Quote Link to comment https://forums.phpfreaks.com/topic/66475-solved-mysql-upload-file/#findComment-332826 Share on other sites More sharing options...
ktalebian Posted August 24, 2007 Author Share Posted August 24, 2007 Hi: thank you for the reply. The thing is, I do not want to allow anyone to download the files. If they are in a folder, then everyone can download them, while if it is in a db, then I can decide who can download what. I want to be able to upload the following files: zip files images movies Thank you Quote Link to comment https://forums.phpfreaks.com/topic/66475-solved-mysql-upload-file/#findComment-333410 Share on other sites More sharing options...
AdRock Posted August 24, 2007 Share Posted August 24, 2007 You must be able to have to directory protected somehow either by using php t ocheck if the useris logged in and has suffiecent right s to that folder or by using .htaccess. I would think you want to go the php way Quote Link to comment https://forums.phpfreaks.com/topic/66475-solved-mysql-upload-file/#findComment-333514 Share on other sites More sharing options...
MadTechie Posted August 24, 2007 Share Posted August 24, 2007 something kinda fun to read Images in the database (FS vs. DBFS) http://www.phpfreaks.com/forums/index.php/topic,156445.0.html Quote Link to comment https://forums.phpfreaks.com/topic/66475-solved-mysql-upload-file/#findComment-333521 Share on other sites More sharing options...
Neptunus Maris Posted August 24, 2007 Share Posted August 24, 2007 First ...how would you upload files into a Database...right now I'm actually creating a file storage system software. I dont store the files into data tables but in folders like normal but what is the code for storing them into a database? Quote Link to comment https://forums.phpfreaks.com/topic/66475-solved-mysql-upload-file/#findComment-333529 Share on other sites More sharing options...
MadTechie Posted August 24, 2007 Share Posted August 24, 2007 file-get-contents is a good start Quote Link to comment https://forums.phpfreaks.com/topic/66475-solved-mysql-upload-file/#findComment-333533 Share on other sites More sharing options...
Neptunus Maris Posted August 24, 2007 Share Posted August 24, 2007 file-get-contents is a good start Thats it? Quote Link to comment https://forums.phpfreaks.com/topic/66475-solved-mysql-upload-file/#findComment-333537 Share on other sites More sharing options...
MadTechie Posted August 24, 2007 Share Posted August 24, 2007 depends what you want to do.. that will get the data you need Quote Link to comment https://forums.phpfreaks.com/topic/66475-solved-mysql-upload-file/#findComment-333551 Share on other sites More sharing options...
ktalebian Posted August 26, 2007 Author Share Posted August 26, 2007 Sp alright, here is the code I have so far: Upload form <form action="http://localhost/admin.php?page=gallery&mode=up&folder=image&sub=add&action=cont" method="post" enctype="multipart/form-data" name="upload"> <table width="30%" cellpadding="1" cellspacing="1" align="center"> <tr><td with="40%" bgcolor="BCD7B8" align="right"> <b>Image Name:</b> </td><td width="60%" bgcolor="D4EFD0"> <input type="text" name="download_name" value="**FILE NAME**"> </td></tr> <tr><td bgcolor="BCD7B8" align="right"> <b>Gallery:</b> </td><td bgcolor="D4EFD0" align="left"> <select name="id"> <?php // Connect to db $query = "SELECT name, id FROM `user_file` WHERE type = 'gall'"; $result = mysql_query($query); while($row = mysql_fetch_array($result)) { $id = $row["id"]; $name = $row["name"]; ?> <option value="<?php echo("$id");?>"><?php echo("$name");?></option> <?php } mysql_close(); ?> </select> </td></tr> <tr><td bgcolor="BCD7B8" align="right"> <b>File:</b> </td><td bgcolor="D4EFD0" align="left"> <input type="hidden" name="MAX_FILE_SIZE" value="2000000000"> <input name="userfile" type="file" class="box" id="userfile"> </td></tr> <tr><td bgcolor="BCD7B8"> </td><td bgcolor="D4EFD0" align="center"> <input type="submit" name="upload" value="Upload"> </td></tr></table></form> and here is the PHP Code <?php $download_name = $_POST["download_name"]; $id = $_POST["id"]; $fileName = $_FILES['userfile']['name']; $tmpName = $_FILES['userfile']['tmp_name']; $fileSize = $_FILES['userfile']['size']; $fileType = $_FILES['userfile']['type']; $fp = fopen($tmpName, 'rb'); $content = fread($fp, fileSize); $content = addslashes($content); fclose($fp); // Connect to db $query = "INSERT INTO upload (name, size, type, content, download_name, type_id ) ". "VALUES ('$fileName', '$fileSize', '$fileType', '$content', '$download_name', '$id')"; mysql_query($query) or die('Error, query failed'); header("location: http://localhost/admin.php?page=gallery&mode=up&folder=image&sub=add&action=done"); ?> the problem with this is that everything works fine but the content is only "[blob - 0b]". Why? I mean the info (size/name/...) everything else is uploaded! Quote Link to comment https://forums.phpfreaks.com/topic/66475-solved-mysql-upload-file/#findComment-334236 Share on other sites More sharing options...
MadTechie Posted August 26, 2007 Share Posted August 26, 2007 apart from the $content = addslashes($content); everything else looks ok, the "[blob - 0b]" is just a way of PMA saying its raw data Quote Link to comment https://forums.phpfreaks.com/topic/66475-solved-mysql-upload-file/#findComment-334237 Share on other sites More sharing options...
ktalebian Posted August 26, 2007 Author Share Posted August 26, 2007 What do you mean by "apart from"? What is wrong with $content = addslashes($content); ? And why shouldn't the blob be more than 0 bytes? And is this how you view a file? // Connect to db $query = "SELECT name, type, size, content, download_name " . "FROM upload WHERE id = '23'"; $result = mysql_query($query) or die('Error, query failed'); list($name, $type, $size, $content) = mysql_fetch_array($result); echo("$content"); header("Content-length: $size"); header("Content-type: $type"); header("Content-Disposition: attachment; filename=$name"); echo $content; sorry, ok the code works, but I get to download the file! This is an image file and I want to be able to display it! How can I disply the file? EDITED: actually the code does not work at all! I mean I downloaded something, but it is nothing! I mean it actually is 0bytes! Quote Link to comment https://forums.phpfreaks.com/topic/66475-solved-mysql-upload-file/#findComment-334248 Share on other sites More sharing options...
MadTechie Posted August 26, 2007 Share Posted August 26, 2007 humm, the code looks ok, so unless the DB is wrong i can' think why your having problems as for the addslahes i personal encode the data first.. are the sizes etc being stored correctly ? also i assume the file is under 16mb and the contents is a "MEDIUMBLOB" Quote Link to comment https://forums.phpfreaks.com/topic/66475-solved-mysql-upload-file/#findComment-334260 Share on other sites More sharing options...
ktalebian Posted August 26, 2007 Author Share Posted August 26, 2007 The sizes and everything else is ok. I keep getting this error: Warning: fread(): supplied argument is not a valid stream resource in H:\wamp\www\test.php on line 31 could this be because the php config does not allow reading files? Quote Link to comment https://forums.phpfreaks.com/topic/66475-solved-mysql-upload-file/#findComment-334309 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.