如何在服务器文件夹中保存图像

时间:2016-03-30 11:15:09

标签: php android image server

这是我的图片上传的PHP代码。有一些文字。这个脚本用于Android应用程序代码。 图像ID,图像URL和文本正确保存在数据库中,但图像未保存在服务器文件夹中。为什么会这样?

<?php

    if($_SERVER['REQUEST_METHOD']=='POST'){

        $image = $_FILES['image'];
        $fullname = $_POST['fullname'];

        require_once('dbConnect.php');

        $sql ="SELECT id FROM uploadfinding ORDER BY id ASC";

        $res = mysqli_query($conn,$sql);

        $id = 0;

        while($row = mysqli_fetch_array($res)){
                $id = $row['id'];
        }

        $path = "uploads/$id.png";

        $actualpath = "My_url/$path";
    // query for db 
        $sql = "INSERT INTO uploadfinding (image,fullname) VALUES ('$actualpath','$fullname')";

        if(mysqli_query($conn,$sql)){

             file_put_contents($actualpath,base64_decode($image));
            echo "Successfully Uploaded";
            }

        mysqli_close($conn);
    }else{
        echo "Error";
    }

    ?>

2 个答案:

答案 0 :(得分:1)

这是这个问题的工作代码

<?php

     header("content-type:application/json");
    require_once('dbConnect.php');
    if($_SERVER['REQUEST_METHOD']=='POST'){

     if(isset($_FILES['image'])){

      $errors= array();

      $fullname = $_POST['fullname'];
      $location = $_POST['location'];

      $file_name = $_FILES['image']['name'];
      $file_size =$_FILES['image']['size'];
      $file_tmp =$_FILES['image']['tmp_name'];
      $file_type=$_FILES['image']['type'];
      $date=date('Y-m-dH:i:s');
     $path="folder_name/".$date.$file_name;
     $actualpath = "http:myUrl/uploadfinding/$path";
      if(empty($errors)==true){
           move_uploaded_file($file_tmp,$path);
          $sql = "INSERT INTO uploadfinding (image,fullname,location,) VALUES ('$actualpath','$fullname','$location',)";
          if(mysqli_query($conn,$sql)){

             echo "message";
          }
      }else{
         print_r($errors);
      }


   }
    }
    ?>

答案 1 :(得分:0)

确保您添加了&#39; enctype&#39; =&GT; &#39; multipart / form-data&#39; ,例如:

<form method="post" name="abc" enctype="multipart/form-data">