2012-11-07 48 views
0

现在挣扎了几个小时,所以有时间问一个问题。保存一个图像到服务器后http后 - android to php

我使用apachehttpclient jar包从android发送一个multipart http post到php服务器。

一旦信息被发布到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);

任何非常感谢。

+0

这是模拟代码还是您正在使用的确切代码?如果是后者,你已经在底部的条件语句中注释了结尾括号。然而,这个问题似乎太微不足道了,所以如果我错误地看待事情,请随时忽略我。致歉,请致电 – Beardy

+0

。是的,这只是模拟代码。我已经修复了这个编辑 – EHarpham

回答

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; 
} 
?> 
+0

非常感谢 – EHarpham

相关问题