简体   繁体   中英

how to rename file before upload in php

I want to rename file dynamically and then upload it. I am fetching name of file from DB. But when I rename file it's not taking name value coming from DB. It's considering static part I am adding to it. Here is my code

uploadId.php:

      $con=mysqli_connect("localhost","root","****","database");
      $result= mysqli_query($con,"SELECT op_id FROM Optrs_info WHERE op_id='1234'");
      $res= mysqli_fetch_array($result); 
      $opid= $res["op_id"];
      $id= $opid . "add2";
      $_SESSION['id']= $id;

And here I am uploading file with new name

<?php

  include("uploadId.php");
  $temp = explode(".", $_FILES["uploaded_file"]["name"]);
  $extension = end($temp);
  $path="/var/www/tcpdf/pictures/";

 $filename=basename($_FILES["uploaded_file"]["name"]);
 $filename =  $_SESSION['id'] . (string) strrchr($filename, '.');
 $filename =  $_SESSION['id'] . '.' . $extension;

  if(move_uploaded_file($_FILES["uploaded_file"]["tmp_name"],$path . $filename))
  {
      echo "Uploaded";
  }
?>

When I tried new file uploaded with add2.jpeg but $opid value is not getting added.

Try this:

<?php
include("uploadId.php");
$temp = explode(".", $_FILES["uploaded_file"]["name"]);
$extension = end($temp);
$path="/var/www/tcpdf/pictures/";

 $filename = basename($_FILES["uploaded_file"]["name"]);
 $filename = $_SESSION['id'] . strrchr($filename, '.') . $extension;

 if(move_uploaded_file($_FILES["uploaded_file"]["tmp_name"],$path . $filename))
 {
     echo "Uploaded";
 }
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM