John Davidson

php - How to give the uploaded image file a unique filename

0 comments
Message:


i am running a localhost project. i'm uploading image and some text into localhost phpmysql server. the image file save into images folder. when i upload a image called 1.png its save on the folder as a same name. like this - images/1.png.


what i want - is it possible to change automatically the image file name to a unique one? Like this - images/1.png into images/sgaggjhdzfndzfnd1.png


<?php 
session_start();

if (!isset($_SESSION['username'])) {
$_SESSION['msg'] = "You must log in first";
header('location: login.php');
}

if (isset($_GET['logout'])) {
session_destroy();
unset($_SESSION['username']);
header("location: login.php");
}


// Create database connection
include_once('db.php');

// Initialize message variable
$msg = "";

// If upload button is clicked ...
if (isset($_POST['upload'])) {
// Get image name
$image = $_FILES['image']['name'];
// Get text
$image_text = mysqli_real_escape_string($db, $_POST['image_text']);

$text01 = mysqli_real_escape_string($db, $_POST['text01']);

$text02 = mysqli_real_escape_string($db, $_POST['text02']);

// image file directory
$target = "images/".basename($image);

$sql = "INSERT INTO images (image, image_text, text01, text02) VALUES ('$image', '$image_text', '$text01', '$text02')";
// execute query
mysqli_query($db, $sql);

if (move_uploaded_file($_FILES['image']['tmp_name'], $target)) {
$msg = "Image uploaded successfully";
}else{
$msg = "Failed to upload image";
}
}
$result = mysqli_query($db, "SELECT * FROM images ORDER BY id DESC");
?>


My Answer:
To give the uploaded image file a unique filename, you can use the `uniqid()` function in PHP to generate a unique identifier and append it to the original filename before saving the file. Here's how you can modify your code to achieve this:

php
// If upload button is clicked ...
if (isset($_POST['upload'])) {
// Get image name
$image = $_FILES['image']['name'];
// Generate a unique identifier
$unique_id = uniqid();
// Get file extension
$image_extension = pathinfo($image, PATHINFO_EXTENSION);
// Create a unique filename
$new_image_name = $unique_id . '.' . $image_extension;
// Get text
$image_text = mysqli_real_escape_string($db, $_POST['image_text']);

$text01 = mysqli_real_escape_string($db, $_POST['text01']);

$text02 = mysqli_real_escape_string($db, $_POST['text02']);

// image file directory
$target = "images/" . $new_image_name;

$sql = "INSERT INTO images (image, image_text, text01, text02) VALUES ('$new_image_name', '$image_text', '$text01', '$text02')";
// execute query
mysqli_query($db, $sql);

if (move_uploaded_file($_FILES['image']['tmp_name'], $target)) {
$msg = "Image uploaded successfully";
} else {
$msg = "Failed to upload image";
}
}


In this code snippet, we generate a unique identifier using `uniqid()` and append it to the original filename before saving the file. This ensures that each uploaded image will have a unique filename in the `images` folder.

Rate this post

5 of 5 based on 7367 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us