dongqiao1151 2013-10-11 13:59
浏览 44
已采纳

Android MySql使用php创建并上传图像到服务器

I need some help here to put image on an "AD" that user creates. I'm doing it with JSON, Android, PHP and MySql

add_ad.php

<?php

//load and connect to MySQL database stuff
require("config.inc.php");

if (!empty($_POST)) {
    //initial query
    $query = "INSERT INTO ads ( ad_title, ad_price, ad_category, ad_description, ad_user, ad_contact ) VALUES ( :title, :price, :category, :description, :email, :mobile ) ";

    //Update query
    $query_params = array(
        ':title' => $_POST['ad_title'],
        ':price' => $_POST['ad_price'],
        ':category' => $_POST['ad_category'],
        ':description' => $_POST['ad_description'],
        ':email' => $_POST['ad_user'],
        ':mobile' => $_POST['ad_contact']
    );

    //execute query
    try {
        $stmt   = $db->prepare($query);
        $result = $stmt->execute($query_params);
    }
    catch (PDOException $ex) {
        // For testing, you could use a die and message. 
        //die("Failed to run query: " . $ex->getMessage());

        //or just use this use this one:
        $response["success"] = 0;
        $response["message"] = "Erro base de dados. Impossível adicionar o anúncio.";
        die(json_encode($response));
    }

    $response["success"] = 1;
    $response["message"] = "Anúncio criado com sucesso! Aguarde aprovação.";
    echo json_encode($response);

} ?>

I don't know how to transform this to receive the image from android and save it on the server/database, PLEASE ALSO tell me the type of the field on database.

This is very important, after that I need to send image from my android to this .php and save it on that user.

Thanks, please be aware that I'm new on android, so be brave :)

  • 写回答

1条回答 默认 最新

  • duanqiu3800 2013-10-11 14:22
    关注

    I'm not sure how Android sends the image to this php script, but normally (by web) it's by using the $_FILES server variable. You can read more about file uploads and how to use it here: http://www.w3schools.com/php/php_file_upload.asp.

    $allowedExts = array("gif", "jpeg", "jpg", "png");
    $temp = explode(".", $_FILES["file"]["name"]);
    $extension = end($temp);
    
    if (($_FILES["file"]["size"] < 20000) && in_array($extension, $allowedExts)) {
    
       if ($_FILES["file"]["error"] > 0) {
           echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
       } else {
    
          if (file_exists("upload/" . $_FILES["file"]["name"])) {
             echo $_FILES["file"]["name"] . " already exists. ";
          } else {
             move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]);
             // IMAGE UPLOAD SUCCESSFULL, UPDATE DATABASE ROW
    
    
          }
        }
      } else {
         echo "Invalid file";
      }
    

    After the image is uploaded to the server, you save the filename in your database. If your filename won't be longer than 255 characters you should use a VARCHAR(255) field for this in your database.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)