How to store file name in database, with other info while uploading image to server using PHP?

To store the file name in a database along with other information while uploading an image to a server using PHP, you can follow these steps:

  1. Connect to your database using PHP.
  2. Create a form that allows the user to select and upload a file.
  3. Use the move_uploaded_file function to move the uploaded file to a desired location on your server.
  4. Use the $_FILES array to get the file name of the uploaded file.
  5. Use an SQL INSERT statement to insert the file name and other relevant information into the database.

Watch a course Learn object oriented PHP

Here is an example of how this could be done:

<?php
// Connect to the database
$db = mysqli_connect('hostname', 'username', 'password', 'database_name');

// Check if the form has been submitted
if (isset($_POST['submit'])) {
  // Get the file name and other information from the form
  $file_name = $_FILES['file']['name'];
  $file_type = $_FILES['file']['type'];
  $file_size = $_FILES['file']['size'];
  $other_info = $_POST['other_info'];

  // Move the uploaded file to a desired location
  move_uploaded_file($_FILES['file']['tmp_name'], "uploads/$file_name");

  // Insert the file name and other information into the database
  $sql = "INSERT INTO table_name (file_name, file_type, file_size, other_info) VALUES ('$file_name', '$file_type', '$file_size', '$other_info')";
  mysqli_query($db, $sql);
}

// Display the form
echo '
<form action="upload.php" method="post" enctype="multipart/form-data">
  Select file: <input type="file" name="file"><br>
  Other information: <input type="text" name="other_info"><br>
  <input type="submit" name="submit" value="Upload">
</form>
';

This code assumes that you have a database table with columns for file_name, file_type, file_size, and other_info. You can adjust the column names and the form fields as needed for your specific application.