<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>PHP &amp; MySQL Tutorials | Student Projects</title>
	<atom:link href="https://studentprojects.in/category/software-development/php-mysql/php-mysql-tutorials/feed/" rel="self" type="application/rss+xml" />
	<link>https://studentprojects.in</link>
	<description>Microcontroller projects, Circuit Diagrams, Project Ideas</description>
	<lastBuildDate>Sat, 10 Dec 2022 05:31:03 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.1.7</generator>
	<item>
		<title>How to upload image to database using PHP/MySQL</title>
		<link>https://studentprojects.in/software-development/php-mysql/php-mysql-tutorials/upload-image-database-phpmysql/</link>
					<comments>https://studentprojects.in/software-development/php-mysql/php-mysql-tutorials/upload-image-database-phpmysql/#comments</comments>
		
		<dc:creator><![CDATA[Editorial Team]]></dc:creator>
		<pubDate>Thu, 01 Sep 2011 07:42:36 +0000</pubDate>
				<category><![CDATA[PHP & MySQL Tutorials]]></category>
		<category><![CDATA[php upload file]]></category>
		<category><![CDATA[mysql upload file]]></category>
		<category><![CDATA[store file in sql]]></category>
		<category><![CDATA[$_files]]></category>
		<guid isPermaLink="false">http://studentprojects.in/?p=1792</guid>

					<description><![CDATA[<p>Here is the simple code to upload files to a particular folder also this will make an entry in database. For example if you are making user registration, this code can be used to store user photo. Use the same user table to store user image. In this code we have used &#8220;file_tbl&#8221; table to</p>
<p>The post <a href="https://studentprojects.in/software-development/php-mysql/php-mysql-tutorials/upload-image-database-phpmysql/">How to upload image to database using PHP/MySQL</a> first appeared on <a href="https://studentprojects.in">Student Projects</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>Here is the simple code to upload files to a particular folder also this will make an entry in database. For example if you are making user registration, this code can be used to store user photo. Use the same user table to store user image. </p>
<p>In this code we have used &#8220;file_tbl&#8221; table to store user image path. Change the table and database name. </p>
<p><strong>SQL dump: </strong></p>
<pre lang="sql" escaped="true" line="1">
--
-- Table structure for table `file_tbl`
--

CREATE TABLE IF NOT EXISTS `file_tbl` (
  `id` int(11) NOT NULL auto_increment,
  `path` varchar(200) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;
</pre>
<p><strong>PHP file:</strong></p>
<pre lang="php" escaped="true" line="1">
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="image" id="image" size="40">
<input name="" type="submit" value="upload" />

</form>
<?php 
$con = mysql_connect('localhost', 'root', ''); //Update hostname
mysql_select_db("my_db", $con); //Update database name

define ("MAX_SIZE","1000"); 
function getExtension($str)
{
	 $i = strrpos($str,".");
	 if (!$i) { return ""; }
	 $l = strlen($str) - $i;
	 $ext = substr($str,$i+1,$l);
	 return $ext;
}

$errors=0;
$image=$_FILES['image']['name'];
if ($image) 
{
	$filename = stripslashes($_FILES['image']['name']);
	$extension = getExtension($filename);
	$extension = strtolower($extension);
	if (($extension != "jpg") &#038;&#038; ($extension != "jpeg") &#038;&#038; ($extension != "png") 
		&#038;&#038; ($extension != "gif")&#038;&#038; ($extension != "JPG") &#038;&#038; ($extension != "JPEG") 
		&#038;&#038; ($extension != "PNG") &#038;&#038; ($extension != "GIF")) 
	{
		echo '<h3>Unknown extension!</h3>';
		$errors=1;
	}
	else
	{
		$size=filesize($_FILES['image']['tmp_name']);

		if ($size > MAX_SIZE*1024)
		{
			echo '<h4>You have exceeded the size limit!</h4>';
			$errors=1;
		}

		$image_name=time().'.'.$extension;
		$newname="images/".$image_name;
		
		$copied = copy($_FILES['image']['tmp_name'], $newname);
		if (!$copied) 
		{
			echo '<h3>Copy unsuccessfull!</h3>';
			$errors=1;
		}
		else echo '<h3>uploaded successfull!</h3>';
		
		mysql_query("insert into file_tbl (path) values('".$newname."')");
	}
	
	//Display image
	$rs=mysql_query("select * from file_tbl");
	if($rs)
		while($row=mysql_fetch_array($rs))
		{
		 ?>
		 <img decoding="async" width="150" src="<?php echo $row['path'];?>"><br>
		 <?php 
		}
}
?>
</pre>
<p>By using the global PHP $_FILES array you can upload files from a client computer to the remote server.</p>
<p>The first parameter is the form&#8217;s input name and the second index can be either &#8220;name&#8221;, &#8220;type&#8221;, &#8220;size&#8221;, &#8220;tmp_name&#8221; or &#8220;error&#8221;. Like this:</p>
<ul>
<li> $_FILES[&#8220;image&#8221;][&#8220;name&#8221;] &#8211; the name of the uploaded file</li>
<li>    $_FILES[&#8220;image&#8221;][&#8220;type&#8221;] &#8211; the type of the uploaded file</li>
<li>    $_FILES[&#8220;image&#8221;][&#8220;size&#8221;] &#8211; the size in bytes of the uploaded file</li>
<li>   $_FILES[&#8220;image&#8221;][&#8220;tmp_name&#8221;] &#8211; the name of the temporary copy of the file stored on the server</li>
<li>   $_FILES[&#8220;image&#8221;][&#8220;error&#8221;] &#8211; the error code resulting from the file upload</li>
</ul>
<p>This is a very simple way of uploading files. For security reasons, you should add restrictions on what the user is allowed to upload. </p><p>The post <a href="https://studentprojects.in/software-development/php-mysql/php-mysql-tutorials/upload-image-database-phpmysql/">How to upload image to database using PHP/MySQL</a> first appeared on <a href="https://studentprojects.in">Student Projects</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://studentprojects.in/software-development/php-mysql/php-mysql-tutorials/upload-image-database-phpmysql/feed/</wfw:commentRss>
			<slash:comments>28</slash:comments>
		
		
			</item>
	</channel>
</rss>
