answersLogoWhite

0


Best Answer

<html>

<head>

<title>Image Upload</title>

</head>

<body>

<form action="img.php" method="POST" enctype="multipart/form-data">

File:

<input type="file" name="myfile">

<br>

<input type="submit" value="upload">

</form>

<?php

// conecting to database

mysql_connect("localhost","root","") or die("cant connect to server.");

mysql_select_db("databaseimage") or die("cant connect to database");

// file properties

/* here we are declaring a variable which uses a builtin function of $_FILES and the first part of it is the name of the image, which we have selected in input name ="image" section. and the second part is the temporary name and location where the file is being uploaded, usually its the temp folder of the server....you can check it by writing echo before the $file */

echo "name: ";

echo $name = $_FILES['myfile']['name'];

echo "<BR>";

echo "type ";

echo $type = $_FILES['myfile']['type'];

echo "<BR>";

echo "Size: ";

echo $size = $_FILES['myfile']['size'];

echo "<BR>";

echo "temp: ";

echo $temp = $_FILES['myfile']['tmp_name'];

echo "<BR>";

echo "error code: ";

echo $error = $_FILES['myfile']['error'];

echo "<BR>";

echo "<BR>";

echo "file contents: "; // this will be used to save in database as BLOB (Binary Large Object) data type.

echo $file_contents = addslashes(file_get_contents($_FILES['myfile']['tmp_name']));

echo "<BR>";

// now inserting the data into database

if (!$insert = mysql_query("INSERT INTO store VALUES ('','$name','$file_contents')"))

echo "problem uploading file";

else

{

$last_id = mysql_insert_id();

echo "Image uploaded.";

echo "<BR>";

echo "<img src=get.php?id=$last_id>";

}

?>

</body>

</html>

User Avatar

Wiki User

12y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How store image path in database in php and how to retrive it?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How do you save image url in database by using php?

If you can get the image url then just assign it to a php variable say: $image = (See related Link) Then you just have to add '$image' to the insert statement of your sql query Note: It would be better to store the folder path where your image is stored. That way your image will not be lost if your site moves to another server.


How do you store pdf file to database using PHP?

Files cannot be directly stored in a MySQL database. However, a path to the file or the contents of the file can.


How can you store pictures on SQL Database?

In short, the SQL database column has to be defined as either an image column or a binary column (SQL2k) In earlier versions, 6.5 &amp; 7, they should have been defined as a BLOB (Binary Large Object). A more precise answer can be given if you supply additional information such as DB version, programming langauge used. ** As explained here you can store images within SQL server however I would not recommend this. In doing so you will bloat the database dramatically, and the reconstitution of the images when recalled from the database is a big performance hit. I would recommend storing the images within the file system (saved to your hard drive as normal) and linking the images to a database record by defining a column that hold the path and file name to the image. In doing so your database overhead is simply that field and not the entire image.


How do you retrieve image from database using php source code with demo?

Putting an image in a database is a bad practise in itself. Instead put the image name along with extension in the database file and use relative path to retrieve the image from the image folder.For example: If your image is in a folder named images/users then write the following code. The image path variable named $img_path will be stored in database.function upload_Image(){$uploaddir = realpath($_SERVER['DOCUMENT_ROOT']);$filename = $_FILES['file']['name'];$uploadfile = $uploaddir.'/project_name/image/users/'.basename($filename);$error = $_FILES['file']['error'];$tmp_file = $_FILES['file']['tmp_name'];$size = ($_FILES['file']['size']/1024); // File Size will be displayed in kilo bytes$type = $_FILES['file']['type'];if(($type=="image/jpeg"$type=="image/png"$type=="image/gif")&&($size0){echo "Invalid File";}else if($filename==""){echo "Filename not found";}else{if(file_exists($uploadfile)){"File already exists!!"; // To avoid duplication of files}else{if(move_uploaded_file($tmp_file, $uploadfile)){echo "File Uploaded";$imgpath = basename($filename);// connection// select database$query = mysql_query("INSERT INTO db_name (Image Path) VALUES('$imgpath')");if(!$query)echo "File cannot be added to database: ".mysql_error();}elseecho "Could not upload files";}}}else{echo "File is not image type";}}


What are the advantages of storing sessions in database?

If you store a session in a database you have several advantages:&bull;Improving the security because on many hosting packages (shared host)PHP uses the same path for storing sessions for all the users,somewhere that is not in your folders.&bull; You can track who is online etc.&bull; For application that are running on multiple servers, you can store all the session data in one database.


How will you add image in HTML and servlet?

&lt;img src="image Path"&gt;


What is coding for inserting Image in HTMl language?

In HTML: &lt;img src="path/to/image.jpg" alt="Desc. of the Image"&gt; In XHTML: &lt;img src="path/to/image.jpg" alt="Desc. of the Image" /&gt;


HTML code for adding JPEG?

Simply type .Make sure you replace 'destination path to image' with the path to your image (eg. http://www.yourwebsite.com/image.jpg).


How do you do an insert?

In most cases, you can either:copy and paste an image into the program you are usingclick on an image button, if there is oneif there are any, look in the Menus at the top of the window.Inserting an image with code:HTML: CSS: background-image: url('path/to/image.jpg');BBCode: [img]path/to/image.jpg[/img]Answers.com: just put the path/to/image.jpg on a line by itself.


How do you put and image on webpage?

Putting an image is as simple as putting a text. You just have to put an image tag and its path alongside.


Where is the location or the path of active directory database?

C:/windows/System32/ntds.dit


What Items would you be most likely to keep a database?

The most likely type of data you will store in a database is text data. Suppliers, customers, employees, product descriptions and so on are all largely composed of text. Although a good proportion of any database will also be composed of numeric data, that data still has to be associated with something a bit more tangible. For instance, a product's selling price would be useless if we didn't know which product it related to, which means you need a text description. There might also be an image associated with the product, but that image will typically be stored outside of the database itself, only the path or URL to the image resource will be stored and even that is composed entirely of text. You probably have a media player on your computer and it will maintain a database of all your music. However, the audio is not stored in the database, the database simply keeps track of where each audio file is stored and extracts the meta-data from the file (or downloads it from an online provider), the bulk of which is purely text information (album, artist, track name, composer, etc). There is no limit to the actual type of record you can store in a database, but no matter what a record physically represents, text data will typically make up the bulk of its data. In some cases you may find binary data embedded in a record, but binary data can be used to represent absolutely anything from a thumbnail image to a video to an entire machine code program. However, in most cases, binary resources will be stored externally to the database (often on a completely separate machine), and a text-based URL will refer to the resource instead. This helps keep the actual database size to a minimum and thus speeds up data retrieval, the bulk of which will be text.