This tutorial tell you how to Upload image into folder and save its name into database.
You should follow these two steps:
You should follow these two steps:
- Creat a database named 'olympic' and a table named 'acc_images'.CREATE TABLE IF NOT EXISTS `acc_images` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `acc_id` int(11) unsigned NOT NULL, `image` varchar(255) NOT NULL, `status` varchar(20) NOT NULL, PRIMARY KEY (`id`), );
- Copy and past code given below.<?php
// Assigning value about your server to variables for database connection
$hostname_connect= "localhost";
$database_connect= "olympic";
$username_connect= "root";
$password_connect= "";
$connect_solning = mysql_connect($hostname_connect, $username_connect, $password_connect) or trigger_error(mysql_error(),E_USER_ERROR);
@mysql_select_db($database_connect) or die (mysql_error());
if($_POST)
{
// $_FILES["file"]["error"] is HTTP File Upload variables $_FILES["file"] "file" is the name of input field you have in form tag.
if ($_FILES["file"]["error"] > 0)
{
// if there is error in file uploading
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
// check if file already exit in "images" folder.
if (file_exists("images/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{ //move_uploaded_file function will upload your image. if you want to resize image before uploading see this link http://b2atutorials.blogspot.com/2013/06/how-to-upload-and-resize-image-for.html
if(move_uploaded_file($_FILES["file"]["tmp_name"],"images/" . $_FILES["file"]["name"]))
{
// If file has uploaded successfully, store its name in data base
$query_image = "insert into acc_images (image, status, acc_id) values ('".$_FILES['file']['name']."', 'display','')";
if(mysql_query($query_image))
{
echo "Stored in: " . "images/" . $_FILES["file"]["name"];
}
else
{
echo 'File name not stored in database';
}
}
}
}
}
?>
<html>
<body>
<form action="" method="post"enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
hi, once successful upload, how to display it out then?
ReplyDeletethanks for ur help
then select the image name form database and show it in images src after folder or directory name
ReplyDeletee.g.
<img src="skin1/images/$image_name">
hi..i like your tutorial..
ReplyDeleteit's work successful.
but how to i upload all photos in one process?
hi thank very much .....
ReplyDeletegiving a code ..
for upload and view imgs...
i search so many web sites for this most of are uncompleted .....
thanks once again ....
its working good..can any one tell me in clear how to display????
ReplyDeleteHi gays see this post it is very very important for learners.
ReplyDeleteHow to change states list depending on selected country
Below link is for showing images stored in database.
ReplyDeleteHow to display images stored in database
the tutorial is a success man, i would like to have small sizes images when displaying because i want to upload some passport photo and crop them before they are sent to the directory. Regards
ReplyDelete
ReplyDeletehow to upload and resize image for thumbnail in php
I am sending you a link which helps you to upload multiple images at once
ReplyDeletehow to upload many images at once in php
thanks for the code...
ReplyDeleteCan u plz upload the code,where i can store images and store data in db,with validation....
I have tried d above code for storing images and other form data,it was working fine until i add validation code
This link will be help full for you for form validation.
ReplyDeleteFind if(count($error)==0) and then place above image uploading code between this condition.
PHP Validation for complete SignUp form
thank you .........
ReplyDeleteI have tried dis code for uploading videos by making few changes,videos was uploaded successfully..
ReplyDeleteBut when i merge both d code i.e(image and video uploading). it refresh d same page without storing values in db....
Thanks for your contribution. but after running this script in Mozila, massage is displaied as "No file selected" could you help anybody to solve this problem....
ReplyDeletethank u so much. It is really nice
ReplyDeletereally awesome...
ReplyDelete