douliang2087 2017-04-10 22:42
浏览 68
已采纳

Php图像上传和命名失败

I have a small form to add categories. My table fields are the following:

Name Required

Description Not Required

Photo Not Required

It will create a category with all the information, it will even insert the image name in the database.

The problem I am having is it will not move the image to the uploads folder. Also it will rename the image as follows: If image name is avatar.jpg it will rename it 85789avatar.jpg in the database field.

I need it to rename the image as follows O1CCJDSXBOM2.jpg.

and the last issue is the image is not required and if you leave it blank it still puts 89439 numbers in the database field.

if (isset($_POST['submit'])) {

         $Name = $_POST['name'];
         $Description = $_POST['description']; 

         if (empty($Name)) {
             $errors[] = "Name Required.";
         }
         if (!empty($errors)) {
             echo validation_errors($errors[0]);

         } else {

         $file = rand(1000, 100000). $_FILES['photo']['name'];
         $file_loc = $_FILES['photo']['tmp_name'];
         $file_size = $_FILES['photo']['size'];
         $folder = "uploads/";

         if (($file_size > 2097152)) {         

             echo validation_errors("Your avatar exceeds file size");        

         } else {

        move_uploaded_file($file_loc, $folder, $file);

        $db = dbconnect();
        $stmt = $db->prepare("INSERT INTO discussion_categories(Name, Description, Photo) VALUES (?,?,?)");
        $stmt->bind_param('sss', $Name, $Description, $file);
        $stmt->execute();        
        $stmt->close(); 

        header("Location: managecategories.php");      
  • 写回答

1条回答 默认 最新

  • ds3016 2017-04-10 22:46
    关注

    it will not move the image to the uploads folder also it will rename the image as follows. if image name is avatar.jpg ti will rename it 85789avatar.jpg in the database field

    move_uploaded_file() takes two arguments, not three. Update this line:

    move_uploaded_file($file_loc, $folder, $file);
    

    To this (to append the filename to the folder):

    move_uploaded_file($file_loc, $folder . $file);
    

    the last issue is the image is not required and if you leave it blank it still puts 89439 numbers in the database field.

    Because move_uploaded_file() returns a boolean, the code could be updated to only insert a record if the file was successfully uploaded.

    if (move_uploaded_file($file_loc, $folder . $file)) {
        $db = dbconnect();
        $stmt = $db->prepare("INSERT INTO discussion_categories(Name, Description, Photo) VALUES (?,?,?)");
        $stmt->bind_param('sss', $Name, $Description, $file);
        $stmt->execute();        
        $stmt->close(); 
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题