Here's the file upload class making your life easier:

<?php
 /*
  @class FileManager
  @description This class handles interaction with Files
  @copyright itoctopus 2007 - The Genoc Library
 */
 class FileManager{
  /*
   [EMAIL PROTECTED] save
   [EMAIL PROTECTED] this function saves the file in the database
   [EMAIL PROTECTED] array $file_handle A handle on the file (ex. 
$_FILES['the_file'])
   [EMAIL PROTECTED] string $field_name The name of the field
   [EMAIL PROTECTED] string $action Update or save the file in the table. 
Defaults to 
save.
   [EMAIL PROTECTED] string $file_source The name of the source table saving 
the file 
(such as realestate)
   [EMAIL PROTECTED] string $file_source_id The id of the row in the source 
table
   [EMAIL PROTECTED] object $db The database handle
  */
  static function save($file_handle, $file_source, $file_source_id, 
$action='save', $allowed_types = array(), $db){
   if (empty($file_handle['tmp_name']))
    return;
   $data = addslashes(fread(fopen($file_handle['tmp_name'], "r"), 
$file_handle['size']));
   if ($action == 'save'){
    $creationdate = $lastupdatedate = Date("Y-m-d H:i:s");
    $sql = 'INSERT INTO file (file_name, file_type, file_size, file_source, 
file_source_id, file_binary, file_creationdate, file_lastupdatedate) VALUES 
(\''.$file_handle['name'].'\', \''.$file_handle['type'].'\', 
\''.$file_handle['size'].'\', \''.$file_source.'\', \''.$file_source_id.'\', 
\''.$data.'\', \''.$creationdate.'\', \''.$lastupdatedate.'\')';

    //now if the type is an image, then create a thumbnail (resize should be 
relative)

   }
   else{
    $lastupdatedate = Date("Y-m-d H:i:s");
    $sql = 'UPDATE file SET file_name=\''.$file_handle['name'].'\', 
file_type=\''.$file_handle['type'].'\', file_source=\''.$file_source.'\', 
file_source_id=\''.$file_source_id.'\', file_binary=\''.$data.'\', 
file_lastupdatedate=\''.$lastupdatedate.'\'';

    //now if the type is an image, then update a thumbnail

   }
   $result= $db->query($sql);
  }

  /*
   [EMAIL PROTECTED] get
   [EMAIL PROTECTED] This function returns a link to the file based on the id
   [EMAIL PROTECTED] string $file_id The id of the file in the database
   [EMAIL PROTECTED] object $db The database handle
   [EMAIL PROTECTED] void
  */
  static function get($file_id, $db){
   $sql = 'SELECT file_id, file_name, file_type, file_size, file_binary FROM 
file where file_id=\''.$file_id.'\'';
   $result= $db->query($sql);
   header('Content-length:'.$result[0]['file_size']);
   header('Content-type:'.$result[0]['file_type']);
   //if it's not an image then download it, otherwise display it
   if (strpos($result[0]['file_type'], 'image') !== FALSE)
    header("Content-type: ".$result[0]['file_type']."; 
filename=".$result[0]['file_name']);
   else
    header("Content-Disposition: attachment; 
filename=".$result[0]['file_name']);
   echo($result[0]['file_binary']);
  }

  /*
   [EMAIL PROTECTED] delete
   [EMAIL PROTECTED] This function delete a file from the database
   [EMAIL PROTECTED] integer $file_id The id of the file to be deleted
   [EMAIL PROTECTED] object $db The database handle
   [EMAIL PROTECTED]
  */
  static function delete($file_id, $db){
   $sql = 'DELETE FROM file WHERE file_id=\'$file_id\'';
   $result= $db->query($sql);
  }

 }
?>

-- 
itoctopus - http://www.itoctopus.com
"Marcelo Wolfgang" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> Hi all,
>
> I'm developing for my first time an upload file form which will populate a 
> sql insert query, I think I got everything working fine, but the data 
> isn't been saved on the database. Can someone help me with what I'm doing 
> wrong here ?
>
> the code follow:
>
> <?php
> if (($_FILES["file"]["type"] == "application/msword")
> || ($_FILES["file"]["type"] == "application/pdf")
> && ($_FILES["file"]["size"] < 2000000))
>   {
>   if ($_FILES["file"]["error"] > 0)
>     {
>     echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
>     }
>   else
>     {
>     if (file_exists("../downloads/" . $_FILES["file"]["name"]))
>       {
>       echo $_FILES["file"]["name"] . " already exists. ";
>       }
>     else
>       {
>       move_uploaded_file($_FILES["file"]["tmp_name"],
>       "../downloads/" . $_FILES["file"]["name"]);
>       }
>     }
>   }
> else
>   {
>   echo "Invalid file";
>   }
> $title = $_POST["title"];
> $filePath =   "../downloads/" . $_FILES["file"]["name"];
> if($_FILES["file"]["type"] == "application/pdf"){
> $fileType = "pdf";
> } else if ($_FILES["file"]["type"] == "application/msword"){
> $fileType = "doc";
> }
> echo($title) . "<br />"; //outputs 'yada' ( correctly as I've typed on the 
> form;
> echo($filePath) . "<br />"; //outputs '../downloads/66321-Estrutura.doc' 
> and I can check that the file is there;
> echo($fileType) . "<br />"; //outputs 'doc' this is correct;
>
> mysql_connect("localhost",$db_user,$db_pass) or die (mysql_error());;
> mysql_select_db ($db_table);
> $user_Query = mysql_query("INSERT INTO tb_downloads (var_title, 
> var_filepath, var_filetype, dt_data, bol_active) VALUES ('$title', 
> '$filePath','$fileType','NOW(),1)");
> mysql_close();
>
> echo($user_Query) . "<br />"; //outputs nothing (? I suck at debugin 
> queries)
>
> header("Location: http://www.w3ol.com.br/50congresso/adm/downloads.php";); 
> // I know that this won't work while I echo something on the page, but the 
> echo is there for debug only
>
> ?>
>
> TIA
> Marcelo Wolfgang 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to