PHP - Simple Change Image Name When Upload

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 enter a value in the textbox and upload the file to the database server. The code use explode() a php built-in function that disassemble any string base on the given parameter in order to concatenate the value with the POST data. This a user-friendly program feel free to modify and use it to your system. We will be using PHP as a scripting language that interpret in the webserver such as xamp, wamp, etc. It is widely use 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 run PHP scripts. Here's the link for 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. tut1

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.
  1. <?php
  2. $conn = new mysqli('localhost', 'root', '', 'db_rename');
  3.  
  4. if(!$conn){
  5. die("Error: Failed to connect to database");
  6. }
  7. ?>

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.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
  6. </head>
  7. <body>
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  11. </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">PHP - Simple Change Image Name When Upload</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <div class="col-md-3">
  18. <form method="POST" enctype="multipart/form-data">
  19. <h4>Upload here</h4>
  20. <input type="file" name="file" required="required"/>
  21. <br />
  22. <div class="form-group">
  23. <input type="text" class="form-control" name="filename" placeholder="Enter file name" required="required"/>
  24. </div>
  25. <center><button class="btn btn-primary" name="upload"><span class="glyphicon glyphicon-upload"></span> Upload</button></center>
  26. </form>
  27. </div>
  28. <div class="col-md-3"></div>
  29. <div class="col-md-6">
  30. <?php include'rename.php'?>
  31. </div>
  32. </div>
  33. </body>
  34. </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.
  1. <?php
  2. require_once 'conn.php';
  3.  
  4. if(ISSET($_POST['upload'])){
  5. $allowed_ext = array("jpg", "png", "jpeg", "gif");
  6. $ext = explode(".", $_FILES['file']['name']);
  7. $end = end($ext);
  8. $file_name=$_POST['filename'];
  9.  
  10. if(in_array($end, $allowed_ext)){
  11. if($_FILES['file']['size'] < 1048576){
  12. $name = $file_name.".".$end;
  13. $path = "upload/".$name;
  14.  
  15. if(move_uploaded_file($_FILES['file']['tmp_name'], $path)){
  16. $conn->query("INSERT INTO `image` VALUES('', '$name', '$path')");
  17. }
  18.  
  19. echo "<center><img src='$path' width='200' height='200'/></center>";
  20. echo"<center><h3>".$name."</h3></center>";
  21.  
  22. }else{
  23. echo "File too large to upload";
  24. }
  25. }else{
  26. echo "Invalid file format";
  27. }
  28. }
  29.  
  30.  
  31. ?>
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!

Add new comment