Results 1 to 7 of 7

Thread: Upload to database

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2005
    Posts
    103

    Exclamation 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

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    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.
    Like Archer? Check out some Sterling Archer quotes.

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Upload to database

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

  4. #4
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Upload to database

    oops, my bad then. I just looked at/played with an old upload script I made, so you're right.
    Like Archer? Check out some Sterling Archer quotes.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Mar 2005
    Posts
    103

    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

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    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.

  7. #7
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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
  •  



Click Here to Expand Forum to Full Width