here is some code i wrote for a guy.
<?php session_start();
// redefine the user error constants - PHP 4 only define("FATAL", E_USER_ERROR); define("ERROR", E_USER_WARNING); define("WARNING", E_USER_NOTICE);
// set the error reporting level for this script error_reporting(FATAL);
//Declarations $myfile = ""; $article = ""; $username = ""; $FileFlag = 0; $DBFlag = 0; $ConStartFlag = 0; $LoggedInFlag = 0;
//--------------------------------------------------------------------------------------------
//logon the user on
if (($_SESSION['username']=="")&&(!$_POST['submit']=="Log In")){
logon(); //show the logon form
die();
}
//logon form submitted so check button and logon state if (($LoggedInFlag==0)&&($ConStartFlag==0)&&($_POST['submit']=="Log In")){ confirm_logon(); }
//-------------------------------------------------------------------------------------------- //main form code
//handle the data inputs if (!$_POST['submit']=="Submit Entry"){ show_form(); die(); }else{ $article = $_POST['article']; }//end if
//check for the presence of a file
$tempfile = $_FILES['userfile']['tmp_name']; //get the temporary file name from the upload dir
if (is_uploaded_file($tempfile)){
upload();
}else{
$FileFlag = 0;
}//end if
//check there is a value for article if ($article!=''){ echo "Processing entry.<br>"; load_db($article); }
//article submission confirmed and records / files updated uploaded if ($DBFlag == 1){ confirmation(); }
function show_form()
{
?>
<form enctype="multipart/form-data" action="<? echo $_SERVER['PHP_SELF'];?>" method="post">
<table><tr><td valign="top">
Composition:</td><td><textarea name="article" rows="25" cols="100"></textarea></td>
<input type="hidden" name="MAX_FILE_SIZE" value="30000" />
</tr><tr><td align="center" colspan="2">Send this file: <input name="userfile" type="file" /></td>
</tr><tr><td align="center" colspan="2"><input type="submit" name="submit" id="submit" value="Submit Entry" /> </td></tr></table
</form>
<? }
function upload()
{
// In PHP versions earlier than 4.1.0, $HTTP_POST_FILES should be used instead
// of $_FILES.
global $article, $FileFlag,$myfile;
$uploaddir = '../bastien/uploads/'; //change to match your dir
$uploadfile = $uploaddir . $_FILES['userfile']['name'];
$myfile = $_FILES['userfile']['name'];
//echo "Filename is:".$myfile."<br>";
print "<pre>";
//check the file extension (only doc allowed) and check the mime type of the file
if ($_FILES['userfile']['type']!='application/msword'){
echo "filetype=".strtolower(substr($_FILES['userfile']['name'],-3));
if (strtolower(substr($_FILES['userfile']['name'],-3))=="doc"){
//check the file size
if ($_FILES['userfile']['size']<30000){
if (copy($_FILES['userfile']['tmp_name'], $uploadfile)){ //windows
//if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) { //unix
$FileFlag = 1;
} //end if copy file
}//end if file size
}//end if file type
}else{
show_form($article);
die("File is of the wrong format and has been rejected. Please try again.");
}
print "</pre>";
}//end function
function load_db($article) { global $DBFlag, $myfile, $FileFlag; //get the data to load into the db $sql = ""; $time_out = ""; $time_out = date("Y-m-D H:i:s");
//get the connection info require("dbconng.php"); //create and run the query to load the data into the db $sql = "update contest set article = '$article', time_out = now() ";
if ($FileFlag == 1){
$sql.=", file_name = '$myfile' ";
}
$sql.= "where user_id=".$_SESSION['user_id'];
echo $sql;
$result = mysql_query($sql,$conn)or die ("Can't insert data because ".mysql_error());
if (!$result){
echo "There was an error with the database. Hit the back button and try again.";
}else{
$DBFlag = 1;
}//end if
}//end function
function confirmation()
{
global $FileFlag, $DBFlag;
//show the confirmation screen
if (($FileFlag == 1) && ($DBFlag ==1)){
$msg = "File and contest entry have been successfully uploaded.<br />Good luck in the contest";
}else{
$msg = "Contest entry have been successfully uploaded.<br />Good luck in the contest";
}
echo $msg; session_destroy();
} //end function
function logon()
{
//show the logon form
echo "<div style=\"position:absolute; top:250; left:300; width:300; height:250;\">
<center><h2>Rich's Writing Club Contest</h2></center>
<form name=\"logon\" action=\"".$_SERVER['PHP_SELF']."\" method=\"post\">
<table>
<tr><td width=\"100\">User Name:</td><td><input type=\"text\" name=\"username\" size=\"20\"></td></tr>
<tr><td>Password :</td><td><input type=\"password\" name=\"pass\" size=\"20\"></td></tr>
<tr><td colspan=\"2\" align=\"center\"><input type=\"submit\" name=\"submit\" value=\"Log In\"></td></tr>
</table>
</form>
</div>";
}//end function
function confirm_logon() { global $ConStartFlag; //get the connection info require("dbconng.php");
//do the logon check $username = ""; $user_id = ""; $pass = ""; $sql = ""; $sql2 = ""; $time_in = ""; $time_in = date("Y-m-D H:i:s");
//assume only alpha and numeric chars allowed in username & passwords
if((eregi("[[:alnum:]]",$_POST['username']))&&(eregi("[[:alnum:]]",$_POST['pass']))){
$username = $_POST['username'];
$pass = $_POST['pass'];
}else{
logon(); die("Invalid login attempt"); }//end if
$sql = "select user_id from users where user_name = '$username' and pass = '".md5($pass)."'";
$result = mysql_query($sql,$conn)or die ("Can't insert data because ".mysql_error());
if (mysql_num_rows($result)==1){ $_SESSION['username'] = $username; //get the user id from the db while ($row = mysql_fetch_array($result)){ $_SESSION['user_id'] = $row['user_id']; $user_id = $row['user_id'];
}//end while }else{ logon(); die("Invalid login attempt!"); }//end if
if ($user_id){
//sql to register the user as logged on and the time they signed in
$sql2 = "insert into contest (user_id,time_in) values ($user_id, now())";
echo $sql2;
$result2 = @mysql_query($sql2,$conn)or die ("Can't insert data because ".mysql_error());
if (!$result2){
echo "There was an error. Try again.";
if (mysql_errno($result)==1062){
echo "Our records indicate that you have already tried to submit for the contest.";
$sql_check = "select * from contest where user_id = $user_id";
$result3 = mysql_query($sql_check,$conn)or die ("Can't insert data because ".mysql_error());
if ($result3){
while ($rows= mysql_fetch_array($result3)){
$article = $rows['article'];
$file_name = $rows['file_name'];
$user = $rows['user_id'];
}//end while
}
}
logon();
die();
}else{
//start the contest and show the form
$ConStartFlag = 1;
show_form();
}//end if
}//end if
}//end function
?>
bastien
From: "Phill" <phill@xxxxxxxxxxxxxxxxx>
To: php-db@xxxxxxxxxxxxx, php-general@xxxxxxxxxxxxx
Subject: [PHP-DB] Re: Office document upload to website (and inserting in MySQL if possible)
Date: Fri, 1 Oct 2004 17:07:33 +0100
"Pugi!" <geenzever.pugilator@xxxxxxxxx> wrote in message
news:c2ig06$1kq$1@xxxxxxxxxxxxxxxxxxxxxx
> Hi,
>
> I would like to upload office-document (doc, xls, ...) using a form to a
> website (apache, php, mysql) in a specific directory and if possible
> insert
> it into a table (MySQL-db). Is this possible. If yes, how ? You don't have
> to explain it in detail, a link to a website or online manual or example
> is
> fine.
>
> Thanks,
>
> Pugi!
>
>
>
> Please reply to group or to pugilator at yahoo dot com
>
I think I get what you want to do, do you want to have a form on a webpage, where you specify the path of it on that pc, and then be able to upload it to a folder on the server. Then, you store the details, such as original filename, current filename, location etc. in the database.
I know this is possible, but I've never managed to do it myself. Try looking
in PHP and MySQL, it's by Larry Ullman and is published by Peachpit Press.
There is something towards the back of the book about file uploads, and it
gives an example of a solution like I explained above
-- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
_________________________________________________________________
Take charge with a pop-up guard built on patented Microsoft® SmartScreen Technology. http://join.msn.com/?pgmarket=en-ca&page=byoa/prem&xAPID=1994&DI=1034&SU=http://hotmail.com/enca&HL=Market_MSNIS_Taglines Start enjoying all the benefits of MSN® Premium right now and get the first two months FREE*.
-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php