本文介绍了如何在php上传之前重命名文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想动态重命名文件然后上传它。我从DB获取文件名。但是当我重命名文件时,它没有从DB获取名称值。它正在考虑我要添加的静态部分。这是我的代码

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:

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";
  }
?>

当我尝试使用add2.jpeg上传新文件时,但未添加$ opid值。

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

推荐答案

试试这个:

<?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";
 }
?>

这篇关于如何在php上传之前重命名文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 06:52