Re: Putting/retriving files into a database

From: Michael Fuhr (mfuhr_at_fuhr.org)
Date: 11/10/03

  • Next message: Murali: "PHPSELF when used with file function"
    Date: 10 Nov 2003 00:04:08 -0700
    
    

    Eric Kincl <Eric@Kincl.net_NO_SPAM_> writes:

    > I was wondering how you stick a file into a database, and then retrive it
    > again for the user with PHP/MySQL. I tried the following which apparently
    > didnt work...

    Have you looked at the chapter in the PHP manual entitled "Handling
    File Uploads"?

    http://www.php.net/manual/en/features.file-upload.php

    > Very quick overview of what I did...
    >
    > html
    > ------
    > <input type="file" name="file"><input type="submit>

    What does your <FORM> tag look like? Does it have
    ENCTYPE="multipart/form-data"?

    > PHP
    > ------
    > $SQL = "INSERT INTO table (file) VALUES (" + $_REQUEST['file'] + ");";
    >
    > I didn't even bother running the SQL querry, I just echoed it and I got the
    > location of the file (ie: /home/eric/blah...)

    I suspect that you didn't specify ENCTYPE correctly or at all in
    your <FORM> tag. If you had, then $_REQUEST['file'] shouldn't be
    set all; instead, $_FILES['file'] should have the info you're looking
    for. See the aforementioned chapter on handling file uploads for
    details.

    Also, *never* put user-supplied input (e.g., form data) in an SQL
    statement without first making sure it's sanitized. See the Security
    chapter in the PHP manual for more information, and pay particular
    attention to what it says about SQL Injection in the "Database
    Security" section. Even on a private server that the Bad Guys can't
    get to, it's a good idea to use good programming habits so they'll
    be familiar if you ever have to work on a public-facing application.

    http://www.php.net/manual/en/security.index.php

    > How do I get the file into the database, and once its there, how do i get it
    > back out?

    You have to get the file's contents before you can insert them into
    the database. Study the "Handling File Uploads" chapter in the PHP
    manual and post a follow up if it doesn't answer your questions.

    Once you learn how to get the file's contents, you can store them
    in a database with an INSERT statement (making sure to sanitize the
    data) and retrieve them with a SELECT query. If you continue to
    have problems, then please post a small but complete sample of your
    code so we can see what you're doing.

    -- 
    Michael Fuhr
    http://www.fuhr.org/~mfuhr/
    

  • Next message: Murali: "PHPSELF when used with file function"

    Relevant Pages

    • Re: Which database should I use?
      ... > and alter the database. ... That is exactly what is possible with SQL server and for a fact with all ... other RDBMS systems but not with Access in combination with a Workgroup ... >only weak point is on security. ...
      (microsoft.public.dotnet.languages.vb)
    • Re: External .mdb inside Forms
      ... > You can set a form's recordsource or a combo box's rowsource to a SQL ... > Note that this method can be used if the external database is secured ... > security, but this is fundamentally insecure and I don't think there's ...
      (microsoft.public.access.forms)
    • Re: Multiple Database Security - How to handle
      ... There is no 'execute as' in SQL Server but you can simplify security ... Assuming the DM database contains tables that are accessed only by ...
      (microsoft.public.sqlserver.security)
    • Re: Microsoft Access Security Best Practices when linking to a SQL back end
      ... Before Access can verify the username and password in the SQL ... Access authenticates the user before the database actually opens. ... own security algorithm is not going to be an easy task. ... > was originally written to use access's own .mdw file for security. ...
      (microsoft.public.access.security)
    • Re: dbdebunk Quote of Week comment
      ... > a lot of really bad SQL programmers. ... But SQL does not have a pointer data type or the ... > being told to design a database. ... But why is little Cindy Lou Who employee ...
      (comp.databases.theory)