Jump to content

Image Upload


GrizzlyBear

Recommended Posts

Hey

 

I was wondering if anyone is able to help me out. I have been trying to look for an image upload script on most sites on the internet, but i aven't come across a success script yet. Basically what I want is, I have a file field where a user will browse an image to upload, the file will then get upload to a directory, but also reflect on a database which holds all the contact information of the user. So there will be another row with the reference 'picture' for example. Then I wish to use this reference to call the image from the directory and display it onto a webpage along with all the users contact details.

 

I have managed to upload the contact information and display it on a webpage, but the image is proving to be a major challange.

 

Thanks,

Grizzly

Link to comment
https://forums.phpfreaks.com/topic/116747-image-upload/
Share on other sites

Oh kewl... I have 5 seperate files that preform the whole task from browsing for the image to the webpage the image needs to be displayed in...

 

Firstly connecting to the database..

 

called connect.php

 

<?php

mysql_connect("localhost", "root",
"")or
die("Cannot Connect to Server");

mysql_select_db('statinfo')or
die("Cannot Select databse".mysql_error());
?> 

 

User register page

 

called registerUer.php

<?php



include('topHalf.php');

echo "

<form action='register.php' method='post' name='register'>

	<table border='0%' width='100%' height='100%' cellspacing='5%' cellpadding='0%' class='blackText'>";

		include('menu.php');

		echo "

		<tr valign='bottom'>

			<td>

				<table border='0%' width='100%' height='100%' cellspacing='5%'

				cellpadding='0%' class='blackText'>

					<tr valign='bottom'>

						<td width='40%'>

							<b>

								User's Login Details

								<hr>

							</b>

						</td>

					</tr>

				</table>

			</td>

		</tr>



		<tr>

			<td>

				<table border='0%' width='100%' height='100%' cellspacing='5%'

				cellpadding='0%' class='blackText'>

					<tr valign='top'>

						<td align='right' width='40%'>

							Username:

						</td>

						<td>

							<sup>*</sup>

							<input type='text' name='username' size='31' class='greyText' maxlength='11'><br>

							<font class='greyText'><i>Maximum: 11 Charachters</i></font>

						</td>

					</tr>



					<tr>

						<td align='right' width='40%'>

							Password:

						</td>

						<td>

							<sup>*</sup>

							<input type='password' name='password' size='31' class='greyText'>

						</td>

					</tr>



					<tr>

						<td align='right' width='40%'>

							Confirm Password:

						</td>

						<td>

							<sup>*</sup>

							<input type='password' name='repassword' size='31' class='greyText'>

						</td>

					</tr>

				</table>

			</td>

		</tr>



		<tr valign='bottom'>

			<td>

				<table border='0%' width='100%' height='100%' cellspacing='5%'

				cellpadding='0%' class='blackText'>

					<tr valign='bottom'>

						<td width='40%'>

							<b>

								Contact Person's Details

								<hr>

							</b>

						</td>

					</tr>

				</table>

			</td>

		</tr>



		<tr>

			<td>

				<table border='0%' width='100%' height='100%' cellspacing='5%' cellpadding='0%' class='blackText'>

					<tr valign='bottom'>

						<td align='right' width='40%'>

							First Name:

						</td>

						<td>

							<sup>*</sup>

							<input type='text' name='firstname' size='31' class='greyText'>

						</td>

					</tr>



					<tr>

						<td align='right' width='40%'>

							Lastname:

						</td>

						<td>

							<sup>*</sup>

							<input type='text' name='lastname' size='31' class='greyText'>

						</td>

					</tr>



					<tr>

						<td align='right' width='40%'>

							Age:

						</td>

						<td>

							<sup>*</sup>

							<input type='text' name='age' size='31' class='greyText'>

						</td>

					</tr>



					<tr>

						<td align='right' width='40%'>

							Cell Phone:

						</td>

						<td>

							<sup>*</sup>

							<input type='text' name='cell' size='31' class='greyText' maxlength='17'><br>

							<font class='greyText'><i>This number will NOT be seen by viewers</i></font>

						</td>

					</tr>

				</table>

			</td>

		</tr>



		<tr valign='bottom'>

			<td>

				<table border='0%' width='100%' height='100%' cellspacing='5%'

				cellpadding='0%' class='blackText'>

					<tr valign='bottom'>

						<td width='40%'>

							<b>

								Business Details

								<hr>

							</b>

						</td>

					</tr>

				</table>

			</td>

		</tr>



		<tr>

			<td>

				<table border='0%' width='100%' cellspacing='5%' cellpadding='0%' class='blackText'>

					<tr valign='bottom'>

						<td align='right' width='40%'>

							Business Name:

						</td>

						<td>

							<sup>*</sup>

							<input type='text' name='organization' size='31' class='greyText'>

						</td>

					</tr>



					<tr>

						<td align='right' width='40%'>

							Address (Line 1):

						</td>

						<td>

							<sup>*</sup>

							<input type='text' name='address1' size='31' class='greyText'>

						</td>

					</tr>



					<tr>

						<td align='right' width='40%'>

							Address (Line 2):

						</td>

						<td>

							  

							<input type='text' name='address2' size='31' class='greyText'>

						</td>

					</tr>



					<tr>

						<td align='right' width='40%'>

							City:

						</td>

						<td>

							<sup>*</sup>

							<input type='text' name='city' size='31' class='greyText'>

						</td>

					</tr>



					<tr>

						<td align='right' width='40%'>

							Province:

						</td>

						<td>

							<sup>*</sup>

							<input type='text' name='province' size='31' class='greyText'>

						</td>

					</tr>



					<tr>

						<td align='right' width='40%'>

							Country:

						</td>

						<td>

							<sup>*</sup>

							<input type='text' name='country' size='31' class='greyText'>

						</td>

					</tr>



					<tr>

						<td align='right' width='40%'>

							Phone:

						</td>

						<td>

							<sup>*</sup>

							<input type='text' name='phone' size='31' class='greyText' maxlength='17'><br>

							<font class='greyText'><i>This number will be seen by viewers</i></font>

						</td>

					</tr>



					<tr>

						<td align='right' width='40%'>

							Fax Number:

						</td>

						<td>

							  

							<input type='text' name='fax' size='31' class='greyText' maxlength='17'>

						</td>

					</tr>



					<tr>

						<td align='right' width='40%'>

							Website:

						</td>

						<td>

							  

							<input type='text' name='website' size='31' class='greyText'>

						</td>

					</tr>



					<tr>

						<td align='right' width='40%'>

							eMail:

						</td>

						<td>

							<sup>*</sup>

							<input type='text' name='email' size='31' class='greyText'>

						</td>

					</tr>


					<tr>

						<td align='right' width='40%'>

							Picture:

						</td>

						<td>

							<sup>*</sup>

							<input type='file' name='picture' size='31' class='greyText'>

						</td>

					</tr>

				</table>





				<table border='0%' width='100%' cellspacing='5%' cellpadding='0%' class='blackText'>

					<tr>

						<td align='center' valign='top'>

							<div align='left'>

								<b>Description:</b>

								<p>

							</div>

							<input type='text' name='a' class='greyText' size='5'

							style='border:6px; text-align: right; background-color: #f0f0f0' readonly>

							 Characters left

							<br>

							<textarea class='greyText' cols='80' rows='25' name='description'

							onkeydown=\"this.value = this.value.slice(0, 2000);

							window.document.register.a.value=2000-this.value.length;\"></textarea>

						</td>

					</tr>

				</table>

			</td>

		</tr>



		<tr>

			<td>

				<b>Search Listing details:</b>

				<hr>

				<b>Select area you want to be listed under:</b>

				<br>

				<table border='0%' width='100%' class='blackText'>";

					include('connect.php');

				  	$query = ("SELECT * FROM tblcategory");

					$result = mysql_query($query) or die(mysql_error());

					$num_results = mysql_num_rows($result);



					for($i=0;$i<$num_results; $i++)

					{

						$r = mysql_fetch_array($result);

					echo "

						<tr>

							<td align='left' width='20%'>

								<input type='radio' name='type' value='" . $r['id'] . "' class='greyText'>

								" . $r['category'] . "

							</td>";



							$row_count++;

							$r = mysql_fetch_array($result);



						echo "

							<td align='left' width='20%'>

								<input type='radio' name='type' value='" . $r['id'] . "' class='greyText'>

								" . $r['category'] . "

							</td>

						</tr>";

						$row_count++;

						$i++;

					}

					echo "

				</table>

				<table border='0%' width='100%' class='blackText'>

					<tr>

						<td align='left' width='20%'>

							<input type='radio' name='type' value='27' class='greyText'>

							Others

							        

							Specify:

							<input type='text' name='otherValue' size='31' class='greyText'>

						</td>

					</tr>

				</table>

				<hr>



				<table border='0%' width='100%' class='blackText'>

					<tr>

						<td align='right' width='40%'>

							<br>

							<input type='checkbox' name='tac' size='31' class='greyText'>

						</td>

						<td>

							<br><p>

							I have read and agree with <a href='tac1.php' target='_blank'>Terms and Conditions</a>.

						</td>

					</tr>



					<tr>

						<td align='right' width='40%'>

							<br>

							<input type='submit' name='submit' value='Register' class='blackText' />

						</td>

						<td>

							<br>

							      

							<input type='reset' name='reset' value='Clear Form' class='blackText' />

						</td>

					</tr>

				</table>

			</td>

		</tr>

	</table>

</form>";

include('bottomHalf.php');



?>

 

 

The register action script

 

called register.php

<?

ob_start();



if (($_POST['password']) != ($_POST['repassword']))

{

header('location: passwordMisMatch.php');

}

else

{

if( (empty ($_POST['firstname'])) || (strlen($_POST['username']) > 11) || (empty ($_POST['lastname'])) ||  (empty ($_POST['age'])) || 

(empty ($_POST['username'])) || (empty ($_POST['password'])) || (empty ($_POST['repassword'])) || (empty ($_POST['email'])) || 

(empty ($_POST['address1'])) || (empty ($_POST['city'])) || (empty ($_POST['province'])) || (empty ($_POST['country'])) || 

(empty ($_POST['cell'])) || (strlen($_POST['cell']) < 10) || (empty ($_POST['organization'])) || (empty ($_POST['tac'])) ||

(strlen($_POST['phone']) < 10) || (empty ($_POST['type'])) || (empty ($_POST['picture'])) )

{

	header('location: invalidData.php');

}

else

{

	$firstname = ucfirst($_POST['firstname']);

	$lastname = ucfirst($_POST['lastname']);

	$age = $_POST['age'];

	$username = $_POST['username'];

	$password = $_POST['password'];

	$organization = ucfirst($_POST['organization']);

	$email = $_POST['email'];

	$picture = $_POST['picture'];

	$address1 = $_POST['address1'];

	$address2 = $_POST['address2'];

	$city = $_POST['city'];

	$province = $_POST['province'];

	$country = $_POST['country'];

	$phone = $_POST['phone'];

	$cell = $_POST['cell'];

	$fax = $_POST['fax'];

	$website = $_POST['website'];

	$type = $_POST['type'];

	$otherValue = $_POST['otherValue'];

	$description = nl2br($_POST['description']);



	include('connect.php');

	$query = "SELECT * FROM tblusers where username = '$username'";

	$result = mysql_query($query) or die(mysql_error());



	$num_results = mysql_num_rows($result);



	if ($num_results == 1)

	{

		include('topHalf.php');

		echo "

		<table border='0%' width='100%' height='100%' cellspacing='0%' cellpadding='0%' align='center'>";

			include('menu.php');

			echo "

			<tr valign='top' height='90%'>

				<td align='center'>

					<font style='font-family:verdana,arial; color:red; text-decoration: blink; font-weight:bold'>

						ERROR

					</font>

					<p>

					<font class='blackText' style='font-weight:bold'>

						The user name: <font color='blue'>" . $username . "</font> is Taken<br>

						Kindly go back and choose another username.

					</font>

				</td>

			</tr>

		</table>";

		include('bottomHalf.php');

	}

	else

	{

		if($_POST)

		{

			$addUser = "insert into tblusers(id, username, password, firstname, lastname, age, admin, status, email, picture, address1, address2, city,

			province, country, phone, cell, fax, organization, website, type, other, description) values(0, '$username', '$password',

			'$firstname','$lastname', '$age',	'0', '0', '$email', '$picture', '$address1', '$address2', '$city', '$province', '$country', '$phone',

			'$cell', '$fax', '$organization', '$website', '$type', '$otherValue', '$description')";





			mysql_query($addUser) or die(mysql_error());



			/*

			Getting data for page creation

			$query = "SELECT * FROM tblusers where username = '$username'";

			$result = mysql_query($query) or die(mysql_error());



			$num_results = mysql_num_rows($result);

			$row_count = 0;

			$row = mysql_fetch_array($result);

			$page = 'firms/' . $row['id'] . '.php';

			require('createCompanyPage.php');

			*/



			header('location: regSuccess.php');

		}

	}

}

}



ob_end_flush();

?> 

 

 

The webpage that displays the information and the picture.

 

called callingInfo.php

<?php

include('connect.php');

$data = mysql_query("SELECT * FROM tblusers")
or die(mysql_error());
Print "<table border=\"0\" cellpadding=\"2\" cellspacing=\"0\">";
while($info = mysql_fetch_array( $data ))
{

Print   "<tr>
			<td width=\"20%\"><div align=\"right\">Firstname:</div></td>
			<td width=\"20%\"><div align=\"left\">".$info['firstname'] . "</div></td>
           	<td width=\"20%\"><div align=\"right\">Company:</div></td>
			<td width=\"20%\"><div align=\"left\">".$info['organization'] . "</div></td>
			<td rowspan=\"6\" width=\"120\"><IMG SRC=\"SecondType.php?id=$row->id\"></td>
		</tr>
		<tr>
			<td width=\"20%\"><div align=\"right\">Lastname:</div></td>
			<td width=\"20%\"><div align=\"left\">".$info['lastname'] . "</div></td>
			<td width=\"20%\"><div align=\"right\">Address:</div></td>
			<td width=\"20%\"><div align=\"left\">".$info['address1'] . "</div></td>
		</tr>
		<tr>
			<td width=\"20%\"><div align=\"right\">Tel Number:</div></td>
			<td width=\"20%\"><div align=\"left\">".$info['phone'] . "</div></td>
			<td width=\"20%\"><div align=\"right\">City:</div></td>
			<td width=\"20%\"><div align=\"left\">".$info['city'] . "</div></td>
		</tr>
		<tr>
			<td width=\"20%\"><div align=\"right\">Cel Number:</div></td>
			<td width=\"20%\"><div align=\"left\">".$info['cell'] . "</div></td>
			<td width=\"20%\"><div align=\"right\">Province:</div></td>
			<td width=\"20%\"><div align=\"left\">".$info['province'] . "</div></td>
		</tr>
		<tr>
			<td width=\"20%\"><div align=\"right\">Fax Number:</div></td>
			<td width=\"20%\"><div align=\"left\">".$info['fax'] . "</div></td>
			<td width=\"20%\"><div align=\"right\">Country:</div></td>
			<td width=\"20%\"><div align=\"left\">".$info['country'] . "</div></td>
		</tr>
		<tr>
			<td width=\"20%\"><div align=\"right\">Email:</div></td>
			<td width=\"20%\"><div align=\"left\">".$info['email'] . "</div></td>
			<td width=\"20%\"><div align=\"right\">Age:</div></td>
			<td width=\"20%\"><div align=\"left\">".$info['age'] . "</div></td>								
		</tr>
		<tr>
		<td colspan=\"5\"></br></br><hr width=\"70%\"></br></br></td>
		</tr>";



}
Print "</table>";


				    ?>

 

And Finally the code that is supposed to call the image from the directory through the database

 

called SecondType.php

<?php
include('connect.php');
$image = stripslashes($_REQUEST[picture]);
$rs = mysql_query("select * from tblusers where filename=\"".
addslashes($image).".jpg\"");
$row = mysql_fetch_assoc($rs);
$imagebytes = $row[picture];
header("Content-type: image/jpeg");
print $imagebytes;
?>

 

 

Sorry about so much coding, but im not even sure if this script does what I want it to do as described above.

 

Thanks Hey,

Grizzly

Link to comment
https://forums.phpfreaks.com/topic/116747-image-upload/#findComment-600402
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.