PHP - Simple Change Image Name When Upload

Submitted by: 
Language: 
Visitors have accessed this post 258 times.

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="<a href="https://sourcecodester.com">Sourcecodester</a>
  11. " rel="nofollow">https://sourcecodester.com">Sourcecodester</a>
  12. </a> </div>
  13. </nav>
  14. <div class="col-md-3"></div>
  15. <div class="col-md-6 well">
  16. <h3 class="text-primary">PHP - Simple Change Image Name When Upload</h3>
  17. <hr style="border-top:1px dotted #ccc;"/>
  18. <div class="col-md-3">
  19. <form method="POST" enctype="multipart/form-data">
  20. <h4>Upload here</h4>
  21. <input type="file" name="file" required="required"/>
  22. <br />
  23. <div class="form-group">
  24. <input type="text" class="form-control" name="filename" placeholder="Enter file name" required="required"/>
  25. </div>
  26. <center><button class="btn btn-primary" name="upload"><span class="glyphicon glyphicon-upload"></span> Upload</button></center>
  27. </form>
  28. </div>
  29. <div class="col-md-3"></div>
  30. <div class="col-md-6">
  31. <?php include'rename.php'?>
  32. </div>
  33. </div>
  34. </body>
  35. </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

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.