Re: databases

From: Ian.H (ian_at_WINDOZEdigiserv.net)
Date: 08/07/04


Date: Sat, 07 Aug 2004 19:02:06 GMT

On Sat, 07 Aug 2004 17:42:41 +0100, Goofy wrote:

[ snip ]

> do u know how should save the paths and file name in the database? is there
> any tutorial? because i only know how to store text in the database. how
> should i build the links and
>> references from that information? any on-line tutorial? and something
> else. which command should i use ( php) in order to retrieve these
> information?

I'd store just the filename (grab this value from
$_FILES['field_name']['name'] (where 'field_name' is the name of your form
element).

Unless you have a highly configurable site where the upload directory
maybe dynamic, you could just define the dir and concatenate to create the
full path:

[ upload.php ]
<?php
  /**
   * If upload.php is in your Web root, this would prevent
   * files being accessed directly without going through a script
   */
  define('UPLOAD_DIR', dirname(__FILE__) . '/../files/');

  $filename = $_FILES['upload_file']['name'];

  /**
   * Do bulk of processing
   * [ ... ]
   */
   if (move_uploaded_file($src, UPLOAD_DIR . $filename) {
     /**
      * Store $filename in database
      */
   }
?>

[ download.php ]
[ example link: foo.com/download.php?id=1 ]
<?php
  define('UPLOAD_DIR', dirname(__FILE__) . '/../files/');

  /**
   * Grab filename from database
   * that matches ID #1
   * (Assuming $row is your assoc array from DB)
   */
   $filename = $row['filename'];

   /**
    * Send headers etc (see php.net under 'header')
    */
    header('Content-type: application/octet-stream');
    /* etc etc etc */

    @readfile(UPLOAD_DIR . $filename);
    exit;
?>

This is a really quick outline of how I normally do things (please note
the lack of validation in this example code) but it works pretty well.

You may want to define more info like a list (array) of mime-types
according to file extension and making the above header() call relative to
the mime-type rather than sending everything application-octet-stream:

  $mimeTypes = array('
    'zip' => 'archive/x-zip',
    'jpg' => 'image/jpeg'
  );

for example.. but there's lots you can play with maybe once you have the
base of the code sorted =)

HTH.

Regards,

  Ian

-- 
Ian.H
digiServ Network
London, UK
http://digiserv.net/


Relevant Pages

  • Re: Storing form data on client?
    ... and store it in a MySQL database. ... Is there a way for me to store the form information on the visitor's ... No, Cookies won't help you. ... PHP typically only delivers the HTML to a browser, ...
    (comp.lang.php)
  • Re: $_GET, MySQL, query variable
    ... if the database holds ids 1-30 and the user enters ... Check if you got the id sent, if not redirect with a header to another page ... PHP parser when we can use fast pure html? ...
    (alt.php)
  • Re: [PHP] Where to start!
    ... I want to use PHP to build a site that uses MySQL, ... or should I designs the db and design the site are ... After you decide what you want to gather, then design a flat database to ... store that information. ...
    (php.general)
  • Re: Reading Structured Binary Files
    ... I was asked to convert some old binary files for a client and store ... However since I'm a database guy, I'm at a disadvantage with this ... If any of the binary data files you're converting has data that isn't ... the OP should be fine sticking with PHP; ...
    (comp.lang.php)
  • Re: How does an SE know if a page has been updated?
    ... but to the date it was modified in the database. ... You could use PHP to set the Last-Modified header to something sensible. ...
    (alt.internet.search-engines)