|
-
Oct 2nd, 2006, 09:31 AM
#1
Thread Starter
Lively Member
Upload to database
Hello.
Is there a way, that I have a user uploading a file (e.g. blablabla.txt) to my server. but now i don't want the file to be saved there but the text within the file saved to my mysql database... is there a way to realize that since it's pretty complicated to have the user open the file, copy all, paste it into a textarea, submit it.....
plz help!
FES
-
Oct 2nd, 2006, 03:42 PM
#2
Re: Upload to database
well, you will have to save it somehow, even if it's just temporarily.
you could try using this (although, it does not have any code to actually upload the file, you'll have to write or find that yourself):
PHP Code:
<?
$upload_dir = "./uploads/";
//upload file here
$thefile = "blah.txt";
//check if the upload was successful or not
if(file_exists($upload_dir . $thefile)){
//open and read in the file
$fh = fopen($upload_dir . $thefile, "r");
$fr = fread($fh, filesize($upload_dir . $thefile));
fclose($fh);
//connect to the database
@mysql_pconnect("host", "user", "pass") or die(mysql_error());
@mysql_select_db("database") or die(mysql_error());
//insert the file information into the database
mysql_query("INSERT INTO thetable VALUES ('" . $fr . "');") or die(mysql_error());
//now, get rid of the text file
if(unlink($upload_dir . $thefile))
echo "deleted file";
else
echo "couldn't delete file";
}else{
echo "file does not exist";
}
?>
this is untested, but it should be fine.
if you don't know how to upload a file, i can post some sample code for that as well.
-
Oct 2nd, 2006, 04:38 PM
#3
Re: Upload to database
 Originally Posted by kows
well, you will have to save it somehow, even if it's just temporarily.
No, PHP does that for you. Just read in the contents of the temp file, save them in the DB, then delete the temp file.
The PHP documentation for file uploads is very good. Look under the Features main heading.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 2nd, 2006, 05:00 PM
#4
Re: Upload to database
oops, my bad then. I just looked at/played with an old upload script I made, so you're right.
-
Oct 4th, 2006, 09:11 AM
#5
Thread Starter
Lively Member
Re: Upload to database
thanks a lot!!!! is there a way to do the same thing the other way around, if the user wants to download the file.. i know how to create the file, but how do i delete it automatically when the user is done downloading it?
fes
-
Oct 4th, 2006, 09:14 AM
#6
Re: Upload to database
Don't create a file. Just read the BLOB from the database and directly pass it to the user, after emitting the correct headers.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 4th, 2006, 09:30 AM
#7
Re: Upload to database
It would be more efficient to store the file physically instead of in the DB though.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|