Re: File uploading and saving info on mysql
| From: | itoctopus | Date: | Fri, 04 May 2007 00:15:12 +0000 |
| Subject: | Re: File uploading and saving info on mysql | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-254378@lists.php.net to get a copy of this message | ||
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{
/*
*@function save
*@desc this function saves the file in the database
*@param array $file_handle A handle on the file (ex. $_FILES['the_file'])
*@param string $field_name The name of the field
*@param string $action Update or save the file in the table. Defaults to
save.
*@param string $file_source The name of the source table saving the file
(such as realestate)
*@param string $file_source_id The id of the row in the source table
*@param 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);
}
/*
*@function get
*@desc This function returns a link to the file based on the id
*@param string $file_id The id of the file in the database
*@param object $db The database handle
*@return 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']);
}
/*
*@function delete
*@desc This function delete a file from the database
*@param integer $file_id The id of the file to be deleted
*@param object $db The database handle
*@static
*/
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" <marcelo@w3ol.com.br> wrote in message
news:01.BE.27633.BB44A364@pb1.pair.com...
> 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