我正在开发一个Android应用程序,需要将用相机拍摄的图像上传到服务器。我使用了一个教程并导入了该项目 from this link, refer this for android code
我的PHP文件指示图像成功并返回URL但在服务器上找不到图像。我无法解决这个问题。
我的PHP文件代码是:
<?php
// Path to move uploaded files
$target_path = "Upload_image_ANDROID/";
// array for final json respone
$response = array();
// getting server ip address
$server_ip = '216.86.147.200';
// final file url that is being uploaded
$file_upload_url = 'http://' . $server_ip . '/' . 'sss_api' . '/' . $target_path;
if (isset($_FILES['image']['name'])) {
$target_path = $target_path . basename($_FILES['image']['name']);
// reading other post parameters
// $email = isset($_POST['email']) ? $_POST['email'] : '';
//$website = isset($_POST['website']) ? $_POST['website'] : '';
$response['file_name'] = basename($_FILES['image']['name']);
//$response['email'] = $email;
//$response['website'] = $website;
try {
// Throws exception incase file is not being moved
if (!move_uploaded_file($_FILES['image']['tmp_name'], $target_path)) {
// make error flag true
$response['error'] = true;
$response['message'] = 'Could not move the file!';
}
// File successfully uploaded
$response['message'] = 'File uploaded successfully!';
$response['error'] = false;
$response['file_path'] = $file_upload_url . basename($_FILES['image']['name']);
} catch (Exception $e) {
// Exception occurred. Make error flag true
$response['error'] = true;
$response['message'] = $e->getMessage();
}
} else {
// File parameter is missing
$response['error'] = true;
$response['message'] = 'Not received any file!F';
}
// Echo final json response to client
echo json_encode($response);
?>
答案 0 :(得分:0)
如果您按照教程结束并且您的Android应用程序在您的设备上正常运行,则由于权限,服务器端的问题更多,您的服务器不允许您保存文件。
尝试将位置设置为您的应用程序有权写入或更改 htaccess 的文件夹,您可以获取有关http://httpd.apache.org/docs/2.2/howto/htaccess.html的更多信息