What are the ways to insert an image path into the MySQL database and save it in the folder and display the same image in a web page via PHP?
-
I want to insert image into mysql database from html form and then want that image to be stored in the folder in server. Then i want to display that image which is stored in that folder in a webpage.
-
Answer:
I'm using Java. How this can be done using JSP? Anyone who knows please answer
Sushil Karki at Quora Visit the source
Other answers
I assume you are aware of the basic File Upload techniques via HTML forms. $name = $_POST['filename']; $upfile = '/images/'.$name; You receive the file name via form. Set the path to the folder in which you have to save the File. In this case the root folder contains a subfolder images which has our file. Your database should have a table "image_info" ( choose your own name) which should have two columns image_id and image_path. Assign a unique id to the picture and store the exact path of the image in the image_path column.. Insert this entry in database and upload the file. Displaying the picture back on the web page.. Extract the path - store it in $path, <image src="<?php echo $path; ?>" > This will display the pic.
Abhishek Bhardwaj
You don't store image or the image path to the database. The database should only keep the ID of the image of UUID, http://en.wikipedia.org/wiki/Universally_unique_identifier. Then you use this information to build the URL to the image file, e.g. "http://static.foo.ltd/bar%7Buid%7D.png", in the latter example only the UUID is retrieved from the database.
Gajus Kuizinas
to get the image file through html form you should have the following input field in your html form: // a sample form contain name of the person and second for the the file. <form enctype="multipart/formdata"> <input type="text" name="first_name"/> <input type="file" name="screenshot"/> </form> now here is the php code to upload the file: <?php // here we define a constant named uploadpath which helps to move the uploaded image file to the specified folder. $define=('uploadpath','path_to_the_folder_for_uploaded_image'); $target= uploadpath.$image; //get the data from the html form. $person_name=$_POST['first_name']; $image=$_FILE['screenshot']['name']; $dbc=mysqli_connect('host_name','username','password','database_name') or die('error connecting to database'); //this is a sample query for a three column table in which first column store the current time and date,second column store say name of the person,third column will store the refrence or you can say name of the image through which it refers to the image in the folder where it is saved after upload permanently. $query= "insert into table_name values(NOW(),'','$person_name','$image')"; $result= mysqli_query($dbc,$query); //this function moves the uploaded file to the folder that we choose to upload the file because initially image is stored in the temporary folder in the server at the time of upload which will be vanished after some time so we need to move this file in to a permanent place. move_uploaded_file($_FILES['screenshot']['tmp_name'],$target); //to show the uploaded image u just need to echo it using the following statement echo'<img src="'.$target.'" />'; mysqli_close($dbc); ?>
Gautam Malik
Here is the answer for those of you looking like I did all over the web trying to find out how to do this task. Uploading a photo to a server with the file name stored in a mysql database and other form data you want in your Database. Please let me know if it helped. Firstly the form you need: <form method="post" action="addMember.php" enctype="multipart/form-data"> <p> Please Enter the Band Members Name. </p> <p> Band Member or Affiliates Name: </p> <input type="text" name="nameMember"/> <p> Please Enter the Band Members Position. Example:Drums. </p> <p> Band Position: </p> <input type="text" name="bandMember"/> <p> Please Upload a Photo of the Member in gif or jpeg format. The file name should be named after the Members name. If the same file name is uploaded twice it will be overwritten! Maxium size of File is 35kb. </p> <p> Photo: </p> <input type="hidden" name="size" value="350000"> <input type="file" name="photo"> <p> Please Enter any other information about the band member here. </p> <p> Other Member Information: </p> <textarea rows="10" cols="35" name="aboutMember"> </textarea> <p> Please Enter any other Bands the Member has been in.</p> <p> Other Bands: </p> <input type="text" name="otherBands" size=30 /> <br/> <br/> <input TYPE="submit" name="upload" title="Add data to the Database" value="Add Member"/> </form> Then this code processes you data from the form: <?php //This is the directory where images will be saved $target = "your directory"; $target = $target . basename( $_FILES['photo']['name']); //This gets all the other information from the form $name=$_POST['nameMember']; $bandMember=$_POST['bandMember']; $pic=($_FILES['photo']['name']); $about=$_POST['aboutMember']; $bands=$_POST['otherBands']; // Connects to your Database mysql_connect("yourhost", "username", "password") or die(mysql_error()) ; mysql_select_db("dbName") or die(mysql_error()) ; //Writes the information to the database mysql_query("INSERT INTO tableName (nameMember,bandMember,photo,aboutMember,otherBands) VALUES ('$name', '$bandMember', '$pic', '$about', '$bands')") ; //Writes the photo to the server if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) { //Tells you if its all ok echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory"; } else { //Gives and error if its not echo "Sorry, there was a problem uploading your file."; } ?> For more information Please follow blow link https://vikasmahajan.wordpress.com/2010/07/07/inserting-and-displaying-images-in-mysql-using-php/ Thanks Keep Asking
Kumar Navneet
<form action="#" method="post" enctype="multipart/form-data"> File: <input type="file" name="file"> <input type="submit" name="submit" value="Upload"> </form> <?php if(isset($_POST['submit'])) { mysql_connect('localhost','root',''); mysql_select_db('new'); $name=$_FILES['file']['name']; $type=$_FILES['file']['type']; if($type=='image/jpeg' || $type=='image/png' || $type=='image/gif' || $type=='image/pjpeg') { if(file_exists(dirname($_SERVER['DOCUMENT_ROOT']).'/oops/upload/image/'.$name)) { echo'file is already present'; } else { $up=move_uploaded_file($_FILES['file']['tmp_name'],dirname($_SERVER['DOCUMENT_ROOT']).'/oops/upload/image/'.$name); $q=mysql_query("insert into image values('','".$name."')"); if($up && $q) { echo'image uploaded and stored'; } elseif(!$up) { echo'image not uploaded'; } elseif(!$q) { echo'image not stored'; } } } else { echo'Invalid file type'; } } ?> The above code is used to upload images to database. Then you can retrieve it
Guru Parthi
The "upload.php" file contains the code for uploading a file: <?php $target_dir = "uploads/"; $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); $uploadOk = 1; $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION); // Check if image file is a actual image or fake image if(isset($_POST["submit"])) { $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]); if($check !== false) { echo "File is an image - " . $check["mime"] . "."; $uploadOk = 1; } else { echo "File is not an image."; $uploadOk = 0; } } ?> PHP script explained: $target_dir = "uploads/" - specifies the directory where the file is going to be placed $target_file specifies the path of the file to be uploaded $uploadOk=1 is not used yet (will be used later) $imageFileType holds the file extension of the file Next, check if the image file is an actual image or a fake image Now after the file has been uploaded you have to insert $target_dir into the database which includes the image path. To insert it into webpage you just have to set the href of <img> tag as the $target_dir. >
Shubham Patel
Here's one way, this is just the base class to upload the files, it has to be invoked. https://github.com/NoMan2000/owaspsecuritywithphp/blob/master/security/Models/FileUploader/FileUploader.php If you can't read that code, you probably shouldn't be doing this since that's an injection-safe approach to uploading files. If you don't care about security and error-handling, you can upload files in probably 20 lines of code. File-based injection attacks are one of the most severe attacks someone can do to a system since they can use that to compromise the database, the server, and any peripherals the server or database has access to. Change line 291 to your destination path in MySQL. In my case, the folder goes to "public/uploads".
Michael Soileau
The best answer so far is . A really experience programmer will never, I repeat never store an image path in a database because of security reasons. Want to know what I do? I save images or uploaded files in a directory which is not even in the web root (usually public_html). Create a naming convention of your files that you can deduce from the details stored in the database for the file like primary id, creation date and type of file (jpg, png or gif). Then serve the file using PHP image function (copy image from your directory which is outside the webroot - the reason being a world writable directory in the webroot is a _bad_ idea whereas script kiddies can't get to a directory outside the webroot in your home dir.
Shubhojoy Mitra
Here i am going to write a simple php script for image processing as you mentioned in your question. Following are some steps given for this type of image processing. Step 1: Make a html file containing following form for uploading image files: <form action="upload.php" method="post" enctype="multipart/form-data"> ChooseFile: <input type="file" name="file"> <input type="submit" name="submit" value="Upload"> </form> Step 2: Make directory in your server where you want to upload your images. Step 3: Make a table in your database having name images with a field name image . Step 4: Make a php file with name upload.php: <?php if(isset($_POST['submit'])) { mysql_connect('yourhost name','mysql username','your mysql password') or die("ERROR:Connection lost"); mysql_select_db('database name'); $filename=$_FILES['file']['name']; $filetype=$_FILES['file']['type']; if($filetype=='image/jpeg' or $filetype=='image/png' or $filetype=='image/gif') { move_uploaded_file($_FILES['file']['tmp_name'],'upload/'.$filename); $filepath="upload/".$filename; $sql="insert into images values('$filepath')"; mysql_query($sql); } } ?> Step 5: Displaying images into Html page stored in database insert code into html file where you want to display your images. <?php mysql_connect('yourhost name','mysql username','your mysql password') or die("ERROR:Connection lost"); mysql_select_db('database name'); $sql="select * from images"; $rdata=mysql_query($sql); while($res=mysql_fetch_array($rdata)) { $imgpath=$res['image']; echo "<img src='$imgpath' height='20' width='20'>"; } ?> hope it helps..... if you have any problem you can call me +918273739593 +919628225232
Mahendra Sharma
Related Q & A:
- How can I make a dynamic web page in PHP?Best solution by Yahoo! Answers
- How to check if a web page loads?Best solution by Server Fault
- In the simplest clearest language possible, can any one tell me step by step how to get a web page or domain?Best solution by Yahoo! Answers
- How to add an image to a Wikipedia page?Best solution by Yahoo! Answers
- How do I set up a web page?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.