473,387 Members | 1,318 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

upload, save and edit photo using php

hi, can i ask for a very simple example php code on how to save a photo from a form to the database or something like that? and how to get it and display it in the page? i just want a very simple example so that i can understand. i am new with php.

thanks
fariba
Oct 19 '06 #1
1 23958
brid
13
Hi it's my very simple example (4 files named as typed in comments):

First create a database (images) and table in it:
--
-- Database: `images`
--
-- Table structure for table `image`
--

CREATE TABLE `image` (
`id` int(11) NOT NULL auto_increment,
`type` varchar(16) NOT NULL default '',
`stream` blob NOT NULL,
KEY `id` (`id`)
) TYPE=MyISAM;

more create files:

[PHP]
<!-- upload_image.php-->

<form action="insert_image.php" method="post" enctype="multipart/form-data" name="form">
<input type="file" name="file" value="*.*" size="50">
<input type="submit" value="Submit">
</form>
[/PHP]
[PHP]
<!-- insert_image.php-->
<?

$filename = $_FILES['file']['tmp_name'];
if (($handle = fopen($filename, "rb"))) {
$stream = fread($handle, filesize($filename));
fclose($handle);
unlink($_FILES['file']['tmp_name']);
$type = $_FILES['file']['type'];

$dbh = mysql_connect("localhost", "user", "pass");
mysql_select_db("images", $dbh);

$qstr = sprintf("INSERT INTO `image` VALUES ('', '%s', '%s')",
mysql_real_escape_string($type),
mysql_real_escape_string($stream));
mysql_query($qstr, $dbh) or die(mysql_error());
}
header("Location: show_image.php");
?>
[/PHP]
[PHP]
<!-- show_image.php-->

<?

$dbh = mysql_connect("localhost", "user", "pass");
mysql_select_db("images", $dbh);

$qstr = "SELECT `id` FROM `image`";
$res = mysql_query($qstr, $dbh) or die(mysql_error());
while ($row = mysql_fetch_assoc($res)) {
echo 'Image: <img src="image.php?id='.$row["id"].'"><br>';
}

?>
<a href="upload_image.php">Upload more...</a>
[/PHP]
[PHP]
<!-- image.php-->

<?
$dbh = mysql_connect("localhost", "user", "pass");
mysql_select_db("images", $dbh);

$qstr = "SELECT * FROM `image` WHERE `id`=".intval($_REQUEST["id"]);
$result = mysql_query($qstr, $dbh);

if (mysql_num_rows($result) > 0) {
$row = mysql_fetch_assoc($result);
$type = $row["type"];
header("Content-type: ".$type);
echo $row["stream"];
}
?>

[/PHP]

P.S.
Images no more than 64Kb otherwise field `stream` must be another type.
In strings mysql_connect("localhost", "user", "pass") change "user", "pass" for your value.
Oct 19 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: bob garbados | last post by:
I am trying to create a database-driven photo gallery for a friend with an admin form to upload images... I can upload a file to the web server, but I want to store the image in a database and I...
15
by: David Lozzi | last post by:
Howdy, I have a function that uploads an image and that works great. I love ..Nets built in upload, so much easier than 3rd party uploaders! Now I am making a public function that will take the...
0
by: SEMIH DEMIR | last post by:
Sitelerden birinde verilen yabancı kaynakli bir scriptti duzenledim yanlız birseyin içinden bir turlu cıkamadım işin aslı ilk defa persistin upload componentini kullanacam yanlız suanki haliyle...
1
by: fariba123 | last post by:
hi i am working with php. i am designing a site which will store employee information along with his/her photo and also edit information. i have completed the information save and edit part. now i...
2
by: RickVidallon | last post by:
I have a .Net application written in C# where members may upload their photos and have them displayed on our website. We are trying to maintain the best quality using best settings in .Net for...
21
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Uploading files from a local computer to a remote web server has many useful purposes, the most...
24
by: owz2008 | last post by:
This has probably been covered before but could not find a similar thread. Basically I have created a form which can be viewed at www.icomworks.co.uk/canvaspayform.html I want to submit the...
3
by: premprakashbhati | last post by:
hi, good evening.. i am going to upload an image in a web form .....for that iam using HTML input(file) control and one web control button i.e., Upload_Button() here is the code ...its work fine...
2
by: djpaul | last post by:
Hmm, damn, why does this not work?? echo '<div id="edittext"><form method="post" action="" enctype="multipart/form-data"> <textarea name="txtarea" id="textfield">' . $edit . '</textarea><br...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.