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 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?