【问题标题】:How to change image file name before uploading to database in php如何在php中上传到数据库之前更改图像文件名
【发布时间】:2015-06-23 13:39:32
【问题描述】:

如何在插入数据库之前重命名图像文件?

  • 数据库名称:项目
  • 表格名称:图片
  • 字段:id (int)、文件 (varchar) [图片网址存储在这里]、名称 (varchar) [图片描述]

HTML 代码放在这里

<form method="POST" action="signup_process.php" enctype="multipart/form-data">
 Image:  <input type="file" name="user_image">
<input tyoe="submit" value="submit">
 </form>

signup_process.php

 <?php
    include 'dbcon.php';
    $filename = $_FILES[user_image][name];
    $source = $_FILES[customer_photo][tmp_name];
    $destination = "photos/$filename";
    move_uploaded_file($source, $destination);

//sql statement bla bla below to upload file in image table

    ?>

此脚本将文件上传到我的服务器的照片目录中,但我想要的是我想将该名称更改为我的图像表的相应 id。 例如sunny.jpg --> 5.jpg

【问题讨论】:

  • 脚本太长了……我需要修改我的代码。
  • 没有人需要更改您的代码。您的问题已在该链接中得到解答。

标签: php html mysql


【解决方案1】:

如果您想在将文件移动到最终位置之前更改文件名,只需更改以下行:

$destination = "photos/$filename";

到这里:

$path_parts = pathinfo($_FILES["user_image"]["name"]);
$extension = $path_parts['extension'];
$destination = "photos/<the name you want>." . $extension;

【讨论】:

    【解决方案2】:
    Please Check below code ,is working for me.
    
    $source = $_FILES[customer_photo][tmp_name];
    $temp = explode(".",$_FILES["user_image"]["name"]);
    $newfilename = rand(1,99999) . '.' .end($temp); //you can change here random id replace the your id
    $destination = "photos/$newfilename";    
    move_uploaded_file($source, $destination);
    

    【讨论】:

      猜你喜欢
      • 2023-04-02
      • 1970-01-01
      • 2019-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多