【问题标题】:Saving a image to server after http post - android to php在 http 发布后将图像保存到服务器 - android 到 php
【发布时间】:2012-11-07 22:04:20
【问题描述】:

为此苦苦挣扎了几个小时,所以有时间问一个问题。

我正在使用 apachehttpclient jar 包从 android 向 php 服务器发送多部分 http 帖子。

一旦信息发布到 php,我会尝试将图像保存到文件夹中以便我可以使用它:

<?php

require("fpdf.php");

//Receive the data from android
$name = $_POST['email'];
$data = $_POST['data'];

//Receive the file
$file = $_FILES['image'];

//$newpath = "/";
//file_put_contents($newpath, $file);

if(move_uploaded_file($file, $newpath)) {

echo json_encode(
        array(
            'result'=>$data,
            'msg'=>'Report added successfully.'
            )
        );
}else {
//something else 
};

?>

我已经尝试过move_upload_file 如上以及file_put_contents($newpath, $file);

非常感谢任何帮助。

【问题讨论】:

  • 这是模拟代码还是您正在使用的确切代码?如果是后者,您已经在底部的条件语句中注释掉了右括号。然而,这似乎太微不足道了,所以如果我看错了,请随意忽略我。
  • 道歉。是的,这只是模拟代码。我已经在编辑中修复了这个问题

标签: php android image file post


【解决方案1】:

这是我用于将照片从 ios 应用程序上传到服务器的 php 脚本。

<?php
$uploadDir = './images/';      //Uploading to directory specified
$file = basename($_FILES['userfile']['name']);
$uploadFile = $file;
$randomNumber = rand(0, 99999999999); 
$newName = $uploadDir . $randomNumber . $uploadFile;


if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {
    echo "Temp file uploaded. \r\n";
} else {
    echo "Temp file not uploaded. \r\n";
}

if ($_FILES['userfile']['size']> 100000000) {
    exit("Your file is too large."); 
}

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $newName)) {
    $maxsize = ini_get('upload_max_filesize');
    echo "http://www.mydomain.com/images/{$file}" . "\r\n" . $_FILES['userfile']['size'] . "\r\n" . $_FILES['userfile']['type'] ;
    echo $randomNumber;
}
?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-16
    • 2020-11-25
    • 1970-01-01
    • 2011-06-05
    • 2014-06-19
    相关资源
    最近更新 更多