gpt4 book ai didi

php - 将图像从 android 上传到 PHP 服务器

转载 作者:行者123 更新时间:2023-11-29 20:54:12 24 4
gpt4 key购买 nike

我正在从 android add 上传一张图片到 PHP 服务器。我从 android 应用程序制作了一个“postFile()”。还有一个 PHP 代码,我收到权限错误。在 MacOS 上使用 XAMPP,我应该更改什么权限,或者代码有问题。请帮帮我。

  1. PHP代码

-

<?php
$target_dir = "images/";
$target_file = $target_dir . basename($_FILES["file"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image

$check = getimagesize($_FILES["file"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}

// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["file"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["file"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
  1. postFile()

-

private void postFile(){
try{
String postReceiverUrl = "http://10.0.2.2/testing/getimage.php";
Log.v(TAG, "postURL: " + postReceiverUrl);

// new HttpClient
HttpClient httpClient = new DefaultHttpClient();

// post header
HttpPost httpPost = new HttpPost(postReceiverUrl);

File file = new File(realPath);
file.getAbsolutePath();
FileBody fileBody = new FileBody(file);

MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("file", fileBody);
httpPost.setEntity(reqEntity);

// execute HTTP post request
HttpResponse response = httpClient.execute(httpPost);
HttpEntity resEntity = response.getEntity();

if (resEntity != null) {

String responseStr = EntityUtils.toString(resEntity).trim();
Log.v(TAG, "Response: " + responseStr);
}

} catch (NullPointerException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
  1. LOGCAT

-

01-29 18:46:17.925: V/MainActivity.java(32696): postURL: http://10.0.2.2/testing/getimage.php
01-29 18:46:18.623: V/MainActivity.java(32696): Response: File is an image - image/jpeg.<br />
01-29 18:46:18.623: V/MainActivity.java(32696): <b>Warning</b>: move_uploaded_file(images/maxresdefault.jpg): failed to open stream: Permission denied in <b>/Applications/XAMPP/xamppfiles/htdocs/testing/getimage.php</b> on line <b>38</b><br />
01-29 18:46:18.623: V/MainActivity.java(32696): <br />
01-29 18:46:18.623: V/MainActivity.java(32696): <b>Warning</b>: move_uploaded_file(): Unable to move '/Applications/XAMPP/xamppfiles/temp/phpb7Xmt1' to 'images/maxresdefault.jpg' in <b>/Applications/XAMPP/xamppfiles/htdocs/testing/getimage.php</b> on line <b>38</b><br />
01-29 18:46:18.623: V/MainActivity.java(32696): Sorry, there was an error uploading your file.

最佳答案

要修复它,请运行递归命令以确保 Apache 服务具有读/写权限。您应该像下面这样授予权限。

sudo chmod -R 777 /Site_Root_Directory/<rest_path>/

这里的路径可能不同。您只需要注意实际的站点目录路径。

关于php - 将图像从 android 上传到 PHP 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28230597/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com