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

时间:2015-01-30 06:46:33

标签: php android file-upload xampp

我正在从android添加到PHP服务器上传图像。我做了一个" postFile()"来自android app。和一个PHP代码,我得到权限错误。 在MacOS上使用 XAMPP ,我应该更改哪些权限,或者代码是否存在问题。 请帮帮我。

  1. PHP代码
  2. -

    <?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()
    2. -

      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的
      2. -

        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.
        

3 个答案:

答案 0 :(得分:1)

要修复它,请运行recursive命令以确保Apache服务具有读/写权限。你应该给予以下许可。

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

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

答案 1 :(得分:1)

所以我解决了我的问题,将我的PHP代码更改为以下内容:

<?php
if($_FILES){
    $file = $_FILES['file'];
    $fileContents = file_get_contents($file["tmp_name"]);
    file_put_contents ("text.jpeg", $fileContents); 
}
?>

它会将图像保存到项目文件夹中。

答案 2 :(得分:0)

为了将数据从xampp上传到android,您需要允许该文件夹的权限,例如,您需要将所有图像设置为upload / dir,以便为该特定文件夹设置为777:

这就是你需要在MAC

下进行更新的方式

如果您使用的是ftp:

enter image description here