- How to upload image PHP and insert path in MySQL?
- 2 Answers 2
- Understanding the Database Storage Path in WordPress with PHP
- How to save file path in database and show path in detail page
- PHP/MYSQL- How to upload Image in Folder and Save
- Save file path in MySQL database when uploading file to server
- How to save an image to the database with a path file
- How to save user’s location in database
- Blog
- Let’s Create Database Using PhpMyadmin
- Write PHP script, to upload image and get stored into DB
- Let’s try to update to user data
- How to setup on your local system ?
- How to import sql file in Database
- admin
- You Might Also Like
- phpmyadmin command line| xampp mysql | mysql command line | mysql query in php
- How to calculate factorial in php | Factorial program in php | factorial recursion
How to upload image PHP and insert path in MySQL?
I’d like to include in my MySQL table a path to an image. The image path gets into the table by inserting the value of a «file» textfield (one of those Browse kind of deals). So the value that gets entered is something like: image001.jpg . But I can’t seem to use that value to put an image into a html page. if it goes into the table fine, why can’t I get it out? I upload an image but I don’t know where it’s gone. Because there’s no value entered in image field when I checked it through PhpMyadmin. Table schema
CREATE TABLE employee_details ( emp_image varchar(255), employee_name varchar(50), employee_address varchar(50), employee_designation varchar(50), employee_salary int(), );
$sql=" INSERT INTO employee_detail( emp_image, employee_name, employee_address, employee_contact, employee_designation, employee_salary ) VALUES( '$_POST[emp_image]', '$_POST[employee_name]', '$_POST[employee_address]', '$_POST[employee_contact]', '$_POST[employee_designation]', '$_POST[employee_salary]' )";
It’s different to just set a filename / path into your database in comparison with uploading your file to a specific path.You have to do both.Make sure your file was uploaded and then you should check that you have the uploaded image path in your tb table.Simple upload tutorial can be found here: w3schools.com/php/php_file_upload.asp
2 Answers 2
On your comment you ask how to upload and store the data to mysql. So here it is:
To get the file, you should have a script in your html like this:
Now, on POST, your PHP file should look like this but please take note that you have to check if the file exists on your POST:
if ($_FILES["file"]["error"] > 0) < echo "Error: " . $_FILES["file"]["error"] . "
"; > else < echo "Upload: " . $_FILES["file"]["name"] . "
"; echo "Type: " . $_FILES["file"]["type"] . "
"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB
"; echo "Stored in: " . $_FILES["file"]["tmp_name"]; >
Since the «Stored in:» part is just the temporary path, you should move to your ‘real’ image path using move_uploaded_file(). Let say the real/default path for your images is in:
You just have to move the file using this:
move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $image_dir. $_FILES['uploaded_file']['name']);
And your full path to the image would be
$image = $final_save_dir . $_FILES['uploaded_file']['name'];
There are several ways to store the path to your database:
1st: Is to store just the filename and concatenate the path of the image in PHP using $_SERVER[‘DOCUMENT_ROOT’] and your default image path like:
$sql="insert into employee_detail( emp_image, employee_name, employee_address, employee_contact, employee_designation, employee_salary) values( '$image', '$_POST[employee_name]', '$_POST[employee_address]', '$_POST[employee_contact]', '$_POST[employee_designation]','$_POST[employee_salary]')";
2nd: Is to store the full path like:
$sql="insert into employee_detail( emp_image, employee_name, employee_address, employee_contact, employee_designation, employee_salary) values( '".$_SERVER['DOCUMENT_ROOT']."\\images\\".$image."', '$_POST[employee_name]', '$_POST[employee_address]', '$_POST[employee_contact]', '$_POST[employee_designation]','$_POST[employee_salary]')";
What I recommend is this approach wherein you will input the partial path (without the root dir) so that later you don’t have a problem on deploying it:
$sql="insert into employee_detail( emp_image, employee_name, employee_address, employee_contact, employee_designation, employee_salary) values( 'images\\".$image."', '$_POST[employee_name]', '$_POST[employee_address]', '$_POST[employee_contact]', '$_POST[employee_designation]','$_POST[employee_salary]')";
And make sure that the images are successfully upload to that default image dir/path.
I also recommend that you use mysqli_* or PDO and use prepare() method /function to prevent sql injection.
Understanding the Database Storage Path in WordPress with PHP
Question: I need to store the paths of the uploaded images in a MySQL database. Regarding the button, you can follow BalusC’s suggestion here to create an HTML button that functions like a link. In your case, it would look something like this: Solution 2: By using this method, you can save the file name in your database. And retrieve your image or file like this: That’s all. Solution 1: You should attempt using the function for both images.
How to save file path in database and show path in detail page
In order to retrieve the cv_path instead of cv_id, the SELECT query needs to be modified.
Afterwards, you have the option to utilize PHP to assign it a different value, such as $cv_file = «www.test.nl/» . $row[‘cv_path’]; .
By utilizing echo $cv_file; , you will obtain www.test.nl/files/test.pdf .
The identical action would involve performing the SELECT query, but this time including it in the link.
A link will be created by both of them, directing you to http://www.test.nl/files/test.pdf.
You should already be familiar with these fundamental concepts, as they have been addressed numerous times on Stack. Thus, there is no need to ask such straightforward questions.
Regarding the button, if you prefer using pure HTML, you can follow BalusC’s suggestion mentioned here: how to create an html button that acts like a link ? «»».
In your specific situation, it would resemble something along these lines:
You have the ability to save the name of the file in your database.
$filetmp = $_FILES["cv"]["tmp_name"];
And get your image or file like
Php — How to save file path in database and show path in, I am busy with a small project where people can fill in a form which will save the values into a database. Now everything is working fine exept for file upload path. In my database I got three tables person, address and cv (with relations) see picture: Code sampleecho «
«;Feedback
PHP/MYSQL- How to upload Image in Folder and Save
This Tutorial is How to upload image in folder and save the image path and name in database using PHP /MYSQL. This Video will make you understand the program
Save file path in MySQL database when uploading file to server
Consider utilizing the move_uploaded_file function for both images. Set up two distinct directories on your server, then iterate through the files in each directory and apply the move_uploaded_file .
Additionally, establish two fields within your database to store both paths and transfer both components into them.
$path=’upload/’.$_FILES[‘file’][‘name’]; move_uploaded_file($_FILES[‘file’][‘tmp_name’] , $path); mysql_query(«insert into tablename (filepath) values(‘$path’)»);
I am looking for a way to store the paths of the uploaded images in a MySQL database.
Since you already have directories in place, it seems necessary to develop a database query for this.
$yourDir = "path to your directory"; $completePathtoYourFile = $yourDir . "/" . $imageName . ".jpg"; //do not append .jpg if already.
Discover the method for establishing a connection with a mysql database.
$insert = "INSERT INTO table_name (field1, field2) VALUES ('$completePathtoYourFile', . ) "; $result = mysqli_query($connectionVariable, $insert); if(!$result) < echo "Not Inserted.."; >else
As stated by others, it is necessary to transfer the uploaded file from temporary storage to a directory with the path specified as $yourDir .
PHP session.save_path when using database, I’ve just moved to storing my sessions in a database. My PHP code uses session_set_save_handler() to supply the required functions and all seems to be ok. But do I need to change my php.ini as well? For example, session.save_handler still has the value of «files». Also, do I still need to make …
How to save an image to the database with a path file
An alternative approach is to create a new variable that combines the path and the image variable, and subsequently utilize this new variable in your query.
$file_path = "assets/images/profile_pics/".$userPic;
if(!$errors) < $userPic = md5($_FILES["fileToUpload"]["name"]) . $date_time . " " . $file_name; move_uploaded_file($file_tmp,"assets/images/profile_pics/" . $userPic); $imag_path = "assets/images/profile_pics/" . $userPic; $stmt = $con->prepare("UPDATE users SET profile_pic = ? WHERE username = ?"); $stmt->bind_param('ss', $imag_path, $username); $stmt->execute(); $stmt->close(); >
Only the name of the new image is saved, not its file path.
How to store file path in database using php?, Another thing I recomend, is renaming the files using a hash. ( like sha1 ) then save it to the file system with a timestamp and hash time().hash().pdf Then save the user’s file name ( original name ) in the DB ( that way you can fake like it’s named that ). You wouldn’t believe the crap PPL put in filenames, all kind …
How to save user’s location in database
An illustration is provided below, demonstrating how to utilize navigator.geolocation and jQuery to transmit data to your backend via AJAX.
if (navigator.geolocation) < navigator.geolocation.getCurrentPosition(function(position) < $.ajax(< url: 'your_backend_page.php', data: < 'lat': position.coords.latitude, 'lng': position.coords.longitude >, type: 'POST', success: function (result) < // If your backend page sends something back alert(result); >>); // Use the 2 lines below to center your map on user location (you need a map instance at this point) userLoc = new google.maps.LatLng(position.coords.latitude, position.coords.longitude); map.panTo(userLoc); >); >
In your PHP page, you will receive the data as » $_POST[‘lat’] » and » $_POST[‘lng’] «, which can be utilized for inserting the data into your MySQL database.
Php — Get all the files from a directory(on a shared path), I need to save each of the file in a single folder in wordpress uploads folder, and insert the related data to each file in wp_posts and wp_postmeata table. There is a repository from where i have to get all the files and save them in uploads folder, for now i have to run this script and save all the files, later have to write a …
Blog
It’s very easy and simple. We need to Create Database for storing the user data. and write PHP script to get user data from the form and stored into database. We have provided all files and DB below of this blog.
Let’s Create Database Using PhpMyadmin
- Go to in phpmyadmin (localhost/phpmyadmin)
- Login into phpmyadmin (Default user: root ,and pass: (NULL))
- After redirected on phpmyadmin dashboard will look like this.
- Create table in this database here “users“
- Put numbers of field you want to create here “4″
Write PHP script, to upload image and get stored into DB
File name: index.php
Name :
Email :
Profile Picture:
FIle Name upload.php
$servername = "localhost"; $database = "fileupload"; $username = "root"; $password = ""; // Create connection $conn = new mysqli($servername, $username, $password,$database); // Check connection if ($conn->connect_error) < die("Connection failed: " . $conn->connect_error); > // write sql query for inserting data into users table. $sql = "INSERT INTO users (name, email, profile_pic) VALUES ('$name','$email', '$to_upload_path')"; if ($conn->query($sql) === TRUE) < header("Location:user_list.php?q=save"); >else < echo "Error: " . $sql . "
" . $conn->error; > $conn->close(); > ?>
- Here, we have created a directory “uploads” inside project root
- and uploaded images inside this and stores it’s path in Data users table (image name with path in this variable : $to_upload_path), so that we can get it on list page.
- Fetch all records from Database
- for Image we get path from database e.g. uploads/Adeep.jpg and locally we have uploads directory, where all images inside this. So we are able to show images.
Let’s try to update to user data
0) < // database connection, to get user data along with images $servername = "localhost"; $database = "fileupload"; $username = "root"; $password = ""; // Create connection $conn = new mysqli($servername, $username, $password,$database); // Check connection if ($conn->connect_error) < die("Connection failed: " . $conn->connect_error); > $userId = $_GET['id']; // write sql query for inserting data into users table. $sql = "SELECT * FROM users where "; $exe_query = $conn->query($sql); $result = $exe_query->fetch_assoc() ?> Name :
Email :
Profile Picture:
?>
File name : update.php (update_profile controller)
else < $to_upload_path = $_POST['profile_pic_update']; >> $servername = "localhost"; $database = "fileupload"; $username = "root"; $password = ""; // Create connection $conn = new mysqli($servername, $username, $password,$database); // Check connection if ($conn->connect_error) < die("Connection failed: " . $conn->connect_error); > // write sql query for inserting data into users table. $sql = "update users set name = '$name', email = '$email' , profile_pic ='$to_upload_path' where "; if ($conn->query($sql) === TRUE) < header("Location:user_list.php?q=update"); >else < echo "Error: " . $sql . "
" . $conn->error; > $conn->close(); > ?>
connect_error) < die("Connection failed: " . $conn->connect_error); > $userId = $_GET['id']; // write sql query for inserting data into users table. $sql = "delete from users where "; if ($conn->query($sql) === TRUE) < header("Location:user_list.php?q=deleted"); >else < echo "Error: " . $sql . "
" . $conn->error; > $conn->close(); > ?>
How to setup on your local system ?
- To Download the code click here
- Check carefully, DB user name and password
- Import Database in your local DB
- Your file structure will look like below.
How to import sql file in Database
- Login in phpmyadmin
- create database
- Click on import
- Browse the SQL file
- Click on Go
admin
I am a well-organized professional in Drupal Development and PHP web development with strong script handling knowledge or automation process with PHP. I awarded 2 times in year by my company for the best employee of the quarter. I never work to complete my working hours. I worked for only achievement.
You Might Also Like
phpmyadmin command line| xampp mysql | mysql command line | mysql query in php
July 10, 2020
How to calculate factorial in php | Factorial program in php | factorial recursion
August 1, 2020