Change Image Name When Upload in PHP

June 22, 2020
PHP
change image name in upload using php

In this tutorial, we will create a Simple Change Image Name When Upload using PHP. This code can rename the upload images when the user enters a value in the textbox and upload the file to the database server. The code use explodes () a PHP built-in function that disassembles any string base on the given parameter to concatenate the value with the POST data. This a user-friendly program. Feel free to modify and use it for your system.

We will be using PHP as a scripting language that interprets in the webserver such as xamp, wamp, etc. It is widely used by modern website application to handle and protect user confidential information.

Getting Started:

First, you have to download & install XAMPP or any local server that runs PHP scripts. Here’s the link for the XAMPP server https://www.apachefriends.org/index.html.

And, this is the link for the bootstrap that I used for the layout design https://getbootstrap.com/.

Creating Database

Open your database web server then create a database name in it db_rename; after that, click Import, then locate the database file inside the folder of the application then click ok.

change image name in upload using php

Creating the database connection

Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it conn.php.

<?php
    $conn = new mysqli('localhost', 'root', '', 'db_rename');
 
    if(!$conn){
        die("Error: Failed to connect to database");
    }
?>

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as index.php.

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
        <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
    </head>
<body>
    <nav class="navbar navbar-default">
        <div class="container-fluid">
            <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
        </div>
    </nav>
    <div class="col-md-3"></div>
    <div class="col-md-6 well">
        <h3 class="text-primary">PHP - Simple Change Image Name When Upload</h3>
        <hr style="border-top:1px dotted #ccc;"/>
        <div class="col-md-3">
            <form method="POST" enctype="multipart/form-data">
                <h4>Upload here</h4>
                <input type="file" name="file" required="required"/>
                <br />
                <div class="form-group">
                    <input type="text" class="form-control" name="filename" placeholder="Enter file name" required="required"/>
                </div>
                <center><button class="btn btn-primary" name="upload"><span class="glyphicon glyphicon-upload"></span> Upload</button></center>
            </form>
        </div>
        <div class="col-md-3"></div>
        <div class="col-md-6">
            <?php include'rename.php'?>
        </div>
    </div>
</body>
</html>

Creating Main Function

This code contains the main function of the application. This code rename the uploaded image when the button is clicked. To do that just copy and write this block of codes inside the text editor, then save it as rename.php.

<?php
    require_once 'conn.php';
 
    if(ISSET($_POST['upload'])){
        $allowed_ext = array("jpg", "png", "jpeg", "gif");
        $ext = explode(".", $_FILES['file']['name']);
        $end = end($ext);
        $file_name=$_POST['filename'];
 
        if(in_array($end, $allowed_ext)){
            if($_FILES['file']['size'] < 1048576){
                $name = $file_name.".".$end;
                $path = "upload/".$name;
 
                if(move_uploaded_file($_FILES['file']['tmp_name'], $path)){
                    $conn->query("INSERT INTO `image` VALUES('', '$name', '$path')");
                }
 
                echo "<center><img src='$path' width='200' height='200'/></center>";
                echo"<center><h3>".$name."</h3></center>";
 
            }else{
                echo "File too large to upload";
            }
        }else{
            echo "Invalid file format";
        }
    }
 
 
?>

There you have it we successfully created Simple Change Image Name When Upload using PHP. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

https://www.campcodes.com/

This is a free education portal. You can use every source code in your project without asking permission to the author. Share our website to everyone to make our community of programmers grow more.

    Leave a Reply

    Your email address will not be published. Required fields are marked *