Re: upload data to MySql

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Just wrote an app that does this myself.  Loads inmages/movies/audio and 
what not into a database.  I've posted the code below, feel free to try 
it out.  This file does all the uploading.  It took me a while to figure 
out how to do this, so don;t worry it it seams like it's tough.  I 
actualy store the files dirrectly in the databse in a longblob, it may 
be better to save the files in a folder (how I most people do it).  Feel 
free to ask any questions you have.  The app is in beta right now, it 
will go on www.supras.com when its done.

HTH - Anthony

<?php
/* add.php  -- part of the SOGI website media module
    writen by Knightrider version 1.0 9-24-02

    This script will alow a user to upload files into the database
*/

// lets include some files so that this crap will work
include 'database.inc';
include 'functions.inc';

// get some info from the config record so we know about upload settings
getConfig();
//echo "<pre>Diag mode:<BR>";
//print_r($HTTP_POST_VARS);

   if (empty($_POST['short']) || empty($_FILES['newfile']) || 
empty($_POST['gen']) || empty($_POST['category']))
   {
?>
     <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd";>
     <html>
     <head>
       <title>SOGI Media Module - Upload a file</title>
	  <link rel='stylesheet' type='text/css' href='media.css'>
     </head>
     <body>
	<table border='0' width='100%'>
		<tr>
			<td width='50%' valign='top'class='pageheader'>
				Media - Add a file
			</td>
			<td width='50%'> 
			
			</td>
		</tr>
	</table>
	<HR class='headertxt'>
	<table width='500' class='border'>
	<tr>
	<td align='center'>
     <form method='post' action='add.php' enctype='multipart/form-data'>
	
	    Upload an Image File
     	Please fill in the details below to upload your file.<BR>
		Fields shown in <font class='bad'>red</font> are mandatory.
	
		<?
			if (isset($_POST['submit']))
				echo "<p class='bad'>You did not enter info into a required field!</p>";
		?>
	
     <table width='100%' class='border'>
     <col span='1' align='right'>

     <tr>
     	<td valign='top'>
	   		<font class='bad'>Short description:</font>
		</td>
        	<td>
			<input type='text' name='short' size=50><BR>
	</tr>
	<tr>
		<td>
			Long Description
		</td>
		<td>
			<textarea rows='5' name='longdesc' cols='33'></textarea><BR>
		</td>
	</tr><tr>
		<td>
			Uploaded by:
		</td>
		<td><input type='text' name='user' cols='33'>
		</td>
	</tr><tr>
		<td>
	   <font class='bad'>Select a Car Type</font>
	   </td>
	   <td><select size='1' name='gen' class='smalltext'>
			<option selected >Select One</option>
				<? catDropDown('1'); ?>
			</select>
	</tr><tr>
		<td>
			<font class='bad'>Select a media depiction</font>
		</td><td>
			<select size='1' name='category' class='smalltext'>
				<option selected >Select One</option>
				<? catDropDown('2'); ?>
	         </select>	
	   </td>
     </tr>

     <tr>
        <td><font class='bad'>File:</font></td>
        <td><input name='newfile' type='file'></td>
     </tr>
	<tr>
       <td align='right'>
	   <input type='hidden' name='MAX_FILE_SIZE' value='16777216'>
	   <input name='submit' class='smalltext' type='submit' value='Upload 
your file'>
	  </td>
     </tr>
     </table>
	</td>
	</tr>
	</table>
	</form>
     <div align="center">
  	 <table border="0" width="300" cellspacing="0" cellpadding="0">
  		<tr>
       <td width="100%" align="center" class="smalltext">
         <hr class="headertxt">
         <a href="default.php">Browse Media</a>
         </td>
     </tr>
   </table>
</div>
     </body>
     </html>
<?php
   }
   else
   {


	$s="";	// set the var blank to add to query
      // Was a file uploaded?
	if (is_uploaded_file($_FILES['newfile']['tmp_name']))
     	{			
			// open and read the uploaded file
			$file1=fopen($_FILES['newfile']['tmp_name'], "r");
			$file=fread($file1, $_FILES['newfile']['size']);

			// Escape special characters in the file
			$file=AddSlashes($file);
			
			$tmp=split('/',$_FILES['newfile']['type']);
			if ($tmp[0]=='image')
				{
					$size = getimagesize($_FILES['newfile']['tmp_name']);
					$s=",width='{$size[0]}', height='{$size[1]}'";
				}		
			
		}
	else
     $file=NULL;

	$insertQuery = "INSERT INTO media_files SET
	 
					shortdesc='{$_POST['short']}',
						longdesc='{$_POST['longdesc']}',
						username='{$_POST['user']}',
						mimetype='{$_FILES['newfile']['type']}',
						filedata='{$file}',
						filesize='{$_FILES['newfile']['size']}',
						filename='{$_FILES['newfile']['name']}',
						uploaded=CURDATE(),
						category1='{$_POST['gen']}',
						category2='{$_POST['category']}'						
						";
	$insertQuery.=$s;
	// check for unknown file type, if it's unknown, automatically censor it.						
	$foo=split("/",$_FILES['newfile']['type']);
	if (AUTOCENSOR or !($foo[0]=="image" or $foo[0]=="video" or 
$foo[0]=="audio"))
		$insertQuery.=", censor='t'";
		
     if ((doQuery($insertQuery)))
	 
	{
			//&& @ mysql_affected_rows() == 1)
			$lastid=mysql_insert_id();
			$statement="INSERT INTO media_stats SET id=$lastid";
			if ((doQuery($statement)))
				{
		    		header("Location: receipt.php?status=T&file=$lastid");										
				}
			else
				{
					echo getError(); 
				
				}
		}
      else
	 
	echo getError();
   }
?>


HTH - Anthony






Boa Constructor wrote:
> I'm new to file uploads, however I managed to get it working.  Are you
> copying the file after its been uploaded?  If not you need to do this as the
> temp file is destroyed after the script has finished running so you need to
> copy it if you wish to store it.  You also will have to chmod 777 to the
> directory you wish to copy the file to.  It may help if we can see the
> tutorial to try and find out exactly what you are doing.
> 
> I've been working from "Build Your Own Database Driven Website Using PHP &
> MySQL" By Kevin Yank and its really helpful it covers file uploads.  I'm
> also using "PHP and MySQL Web Development" by Luke Wellling and Laura
> Thomson.  This covers file uploads also, infact I found it more helpful for
> file uploads.
> 
> 
> Hope this helps.
> 
> Cheers,
> 
> G :)
> 
> ----- Original Message -----
> From: "Seabird" <jacco@vliegt.nl>
> To: <php-db@lists.php.net>
> Sent: Wednesday, November 13, 2002 8:47 PM
> Subject:  upload data to MySql
> 
> 
> 
>>Hi everyone,
>>
>>I don't get my upload to work properly. It's a tutorial of the web, but it
>>doesn't function (unless I made a mistake). Please help me...
>>I have a form passing on 5 fields (text for testing), name: year, make,
>>model, price, picture and submit to PHP_SELF
>>
>><?php
>>if ($submit) {
>>
>>$db = mysql_connect("localhost","myname","mypassword");
>>
>>mysql_select_db("$test",$db);
>>//DB Test for testing...
>>
>>$sql = "INSERT INTO test (year,make,model,price,picture) VALUES
>>('$year,$make,$model,$price,$picture')";
>>//I created these columns in the table test
>>
>>
>>echo "year: $year<br>\n";
>>echo "make: $make<br>\n";
>>echo "model: $model<br>\n";
>>echo "price: $price<br>\n";
>>echo "picture: $picture<br>\n";
>>}
>>
>>?>
>>
>>I know I need globals on for this.
>>Thanx for the help,
>>Jacco
>>--
>>http://seabird.jmtech.ca
>>
>>Attitude is Everything!
>>But Remember, Attitudes are Contagious!
>>Is Yours worth Catching????
>>
>>
>>
>>--
>>PHP Database Mailing List (http://www.php.net/)
>>To unsubscribe, visit: http://www.php.net/unsub.php
>>
> 
> 



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


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux