Create a file : form.html
<form action=addmembers.php method=post enctype="multipart/form-data">
<table border="0" cellspacing="0" align=center cellpadding="3" bordercolor="#cccccc">
<tr>
<td>File:</td>
<td><input type="file" name="filep" size=45></td>
</tr>
<tr>
<td colspan=2><p align=center>
<input type=submit name=action value="Load">
</td>
</tr>
</table>
</form>
Create another file : addmembers.php
<?
if ($_POST["action"] == "Load")
{
$folder = "images/";
move_uploaded_file($_FILES["filep"]["tmp_name"] , "$folder".$_FILES["filep"]["name"]);
echo "<p align=center>File ".$_FILES["filep"]["name"]."loaded...";
$result = mysql_connect("localhost", "****", "****") or die ("Could not save image name Error: " . mysql_error());
mysql_select_db("test") or die("Could not select database");
mysql_query("INSERT into picture (URL) VALUES('".$_FILES['filep']['name']."')");
if($result) { echo "Image name saved into database"; }
else {
//Gives and error if its not
echo "Sorry, there was a problem uploading your file.";
}
}
?>
For Display the file use code :
<?
//Retrieves data from MySQL
mysql_connect("localhost", "****", "*****") or die ("Could not save image name Error: " . mysql_error());
mysql_select_db("test") or die("Could not select database");
$data = mysql_query("SELECT `URL` FROM picture ") or
die(mysql_error());
//Puts it into an array
$file_path = 'http://localhost/test/images/';
while($row = mysql_fetch_assoc( $data ))
{//Outputs the image and other data
$src=$file_path.$row['URL'];
echo "<img src=".$src."> <br>";
}
?>
<form action=addmembers.php method=post enctype="multipart/form-data">
<table border="0" cellspacing="0" align=center cellpadding="3" bordercolor="#cccccc">
<tr>
<td>File:</td>
<td><input type="file" name="filep" size=45></td>
</tr>
<tr>
<td colspan=2><p align=center>
<input type=submit name=action value="Load">
</td>
</tr>
</table>
</form>
Create another file : addmembers.php
<?
if ($_POST["action"] == "Load")
{
$folder = "images/";
move_uploaded_file($_FILES["filep"]["tmp_name"] , "$folder".$_FILES["filep"]["name"]);
echo "<p align=center>File ".$_FILES["filep"]["name"]."loaded...";
$result = mysql_connect("localhost", "****", "****") or die ("Could not save image name Error: " . mysql_error());
mysql_select_db("test") or die("Could not select database");
mysql_query("INSERT into picture (URL) VALUES('".$_FILES['filep']['name']."')");
if($result) { echo "Image name saved into database"; }
else {
//Gives and error if its not
echo "Sorry, there was a problem uploading your file.";
}
}
?>
For Display the file use code :
<?
//Retrieves data from MySQL
mysql_connect("localhost", "****", "*****") or die ("Could not save image name Error: " . mysql_error());
mysql_select_db("test") or die("Could not select database");
$data = mysql_query("SELECT `URL` FROM picture ") or
die(mysql_error());
//Puts it into an array
$file_path = 'http://localhost/test/images/';
while($row = mysql_fetch_assoc( $data ))
{//Outputs the image and other data
$src=$file_path.$row['URL'];
echo "<img src=".$src."> <br>";
}
?>
hi,
ReplyDeletehow to store the image in folder?
DeleteHi Utterly Anna use this
Delete$target_path="upload/";
it doesn't work! its showing in db but not in folder.
DeleteIts working fine, already tested and implemented. Check your folder permission or path.
DeletePermission mean folder is writeable by web server (apache, nginx, etc).
Deleteits not storing the path into my database...
ReplyDeletetable name: images
table fields: url(1 field only)
First Check database connection if its correct. Check Field name and Database name.
ReplyDeleteIn above script:
table name : picture
table fields : URL
how to upload the files into db and retreiving the file location and dipalying it on the webbrowser with mysqli and php
DeletePHP script is same but you need to use mysqli() functions in place if mysql().
DeleteFor more information regarding mysqli() functions read this: http://goo.gl/zH4k7s
Thanks a mil! I've been searching the net for a script that will store file on server and display link to download it. I got it working now.
ReplyDeleteThanks.
Thanks but I was looking for something which can store Links in my database(like: http://www.avsysnepal.com/av.png)
ReplyDeleteit only works for pics how can i store videos
ReplyDeleteTry this may be helpful :-
ReplyDeletehttp://bytes.com/topic/php/answers/941830-how-upload-videos-database
Display the file use code not working also no error
ReplyDeletei get broken files why is that?
ReplyDelete@Dyyn Elaborate your problem in detail?
ReplyDeletehow ? to select one dropdown list store the image selected listed name folder.
ReplyDeletepls help me sir.
how to use ajax easy way in jquery.
ReplyDeleteTry this :
Deletehttp://www.html.net/tutorials/javascript/lesson21.php
http://www.w3schools.com/jquery/tryit.asp?filename=tryjquery_ajax_ajax
how can i store doc and pdf file, and view it in browser
ReplyDeletethankz in advance.
You can store .doc and .pdf file using same method as mention above post.
DeleteThe image has been stored in the folder. But the url is not storing in database !! why?
ReplyDeleteCheck your sql quires or database connection.
DeleteThis comment has been removed by the author.
ReplyDeleteCheck below link for your reference.
Deletehttp://in2.php.net/pdo
Thanks god bless u.
ReplyDeleteHi what do you name the tables in the database
ReplyDeleteIn above example table name is : picture
Deletehi jag, explain to me please and thanks for the notice..
ReplyDelete$uploadDir = 'images/';
$fileName = $_FILES['Photo']['name'];
$filePath = $uploadDir . $fileName;
the query is --> INSERT INTO user_information (image) VALUES ('$filePath',);
my question;
the datatype for image is blob..is what im doing called saving URL in database and image in directory??bcse i've noticed in ur query that ur not including the directory..do explain please and thank u so much
Datatype for Image field in above query is varchar for saving file name or path as a text.
DeleteIts optional to include or exclude the directory path.
hi jag, explain to me please and thanks for the notice..
ReplyDelete$uploadDir = 'images/';
$fileName = $_FILES['Photo']['name'];
$filePath = $uploadDir . $fileName;
the query is --> INSERT INTO user_information (image) VALUES ('$filePath',);
my question;
the datatype for image is blob..is what im doing called saving URL in database and image in directory??bcse i've noticed in ur query that ur not including the directory..do explain please and thank u so much
Thank You..
ReplyDeleteKeep it up
Good dy,
ReplyDeleteThanks for your code. It is working on my local machine which has a wamp server. However, it is not working on my real server.
Need to check your real server configuration.
DeleteHi, can you tell me if the following is applicable?
ReplyDeletewhen the user chooses an image from file input type, i want to rename it first (each product's image has the same name as the ID of the product) and then move it into a specific folder in order for me to retrieve it..
storing images in mysql and then retrieving them using php didnt with work me, so i had to put all of the products' images in a folder and named each product's image by its ID
Not geting your problem
Delete$filename = basename($_FILES['file']['name']);
Delete$extension = pathinfo($filename, PATHINFO_EXTENSION);
$Filenamee = "background.png";
move_uploaded_file($_FILES["filep"]["tmp_name"] , "$folder"."$Filenamee".'.'.$extension);
thanks...this saved my life
ReplyDeleteTHIS SAVED MY LIFE. OMG. THANK YOU SO MUCH :)
ReplyDeleteHi,Can you please tell me how to validate the file uploaded.
ReplyDeleteTry This :
Deletehttp://jagdeepmalhi.blogspot.in/2014/05/validation-uploaded-file-in-php.html
Thank you. very clear and easy to understand
ReplyDeletesir ,this not saving file path in database,this is only saving file name. and my images are not saving in file folder
ReplyDeleteCan you please guide me how to disply the images into pdf from database and stored folder.
ReplyDeleteYou can use same code, but you need to work on how generate .pdf files using php.
Deletehello can u please tel me how to do the same thing using code igniator??
ReplyDeleteThats the perfect for Me Thanks
ReplyDeleteHi,
ReplyDeleteThis is great however is it possible to give the file a unique name to prevent duplicates/overwriting?
Thanks
You could use something similar to this:
Delete';
echo mysqli_error($dbc);
if (!empty($_FILES)) {
$tempFile = $_FILES['file']['tmp_name']; // Generate a temporary file
$targetPath = dirname( __FILE__ ) . $ds. $storeFolder . $ds; // Genrate a temporary file during the upload
$targetFile = $targetPath. $name; // Set the location and the name for the uploaded file
move_uploaded_file($tempFile,$targetFile); // Move the temporarary file the location defined above and rename it.
$deleteFile = $targetPath.$old['avatar'];
if($old['avatar'] != '') {
if(!is_dir($deleteFile)) {
unlink($deleteFile);
}
}
}
?>
To avoid duplicate names simple solution use time stamp like:
DeleteExample : move_uploaded_file($_FILES["filep"]["tmp_name"] , "$folder".time().$_FILES["filep"]["name"]);
hi i want to show the imagepath in edit option where imagepath is already in db i have to show that path while i selecting the id for edit.....
ReplyDeletehi i want to show the imagepath in edit option where imagepath is already in db i have to show that path while i selecting the id for edit.....
ReplyDeleteGood...
ReplyDeletehi sir
ReplyDeleteNot able to save image or image path i created database test with table picture field url. still anything to change (changed localhost usernameand password)
please help me
hi i inserted successfully, but while retrieving the image it just showing img box instead of real image please help me to solve this problem am using real server.
ReplyDeleteGOOOOOD JOOOOOOB! This Works! :)
ReplyDeleteExcellent…Amazing…. I’m satisfied to find so many helpful information here within the put up,for latest php jobs in hyderabad. we want work out extra strategies in this regard, thanks for sharing.
ReplyDeleteddd
ReplyDelete