2012-11-07 77 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

相關問題