数据库文件重命名并以JSON方式回显其结果

时间:2018-04-04 04:27:49

标签: php json

我想将图像上传到数据库并重命名,然后以JSON格式返回新的图像名称。这是我的代码:

     <?php

    if (isset($_POST['upload_Btn'])) {
        $ext = end((explode(".", $_FILES['gambar']['name'])));
        $target = "uploaddosen/". microtime() .".".$ext;


        if (move_uploaded_file($_FILES['gambar']['tmp_name'],$target)) {
            $msg = "Image uploaded successfully";
        }else{
            $msg = "Failed to upload image";
        }

      echo $msg;
  }

?>

1 个答案:

答案 0 :(得分:0)

你可以尝试这样的方式,

 <?php

if (isset($_POST['upload_Btn'])) {
    $ext = end((explode(".", $_FILES['gambar']['name'])));
    $file_name = microtime() .".".$ext;
    $target = "uploaddosen/". microtime() .".".$ext;


    if (move_uploaded_file($_FILES['gambar']['tmp_name'],$target)) {
        $msg = "Image uploaded successfully";
        // Write your db insert code here
        echo json_encode(array('msg' => $msg, 'image' => $file_name));
       exit();

    }else{
        $msg = "Failed to upload image";
    }

  echo $msg;

}

&GT;