2017-04-19 150 views
0

我想使用PHP文件将文件从Android应用程序上传到Web服务器。服务器中的文件路径

此PHP文件:

<?php 

    //importing dbDetails file 
    require_once 'dbDetails.php'; 

    //this is our upload folder 
    $upload_path = 'usuarios/'; 

    //Getting the server ip 
    $server_ip = gethostbyname(gethostname()); 

    //creating the upload url 
    $upload_url = 'http://'.$server_ip.'/danyra/administrar/application/admin/'.$upload_path; 

    //response array 
    $response = array(); 


    if($_SERVER['REQUEST_METHOD']=='POST'){ 

     //checking the required parameters from the request 
     if(isset($_POST['name']) and isset($_FILES['image']['name'])){ 

      //connecting to the database 
      $con = mysqli_connect(HOST,USER,PASS,DB) or die('Unable to Connect...'); 

      //getting name from the request 
      $name = $_POST['name']; 

      //getting file info from the request 
      $fileinfo = pathinfo($_FILES['image']['name']); 

      //getting the file extension 
      $extension = $fileinfo['extension']; 

      //file url to store in the database 
      $file_url = $upload_url . getFileName() . '.' . $extension; 

      //file path to upload in the server 
      $file_path = $upload_path . getFileName() . '.'. $extension; 

      //trying to save the file in the directory 
      try{ 
       //saving the file 
    line 45 -->  move_uploaded_file($_FILES['image']['tmp_name'],$file_path); 
       $sql = "INSERT INTO `images` (`id`, `url`, `name`) VALUES (NULL, '$file_url', '$name');"; 

       //adding the path and name to database 
       if(mysqli_query($con,$sql)){ 

        //filling response array with values 
        $response['error'] = false; 
        $response['url'] = $file_url; 
        $response['name'] = $name; 
       } 
      //if some error occurred 
      }catch(Exception $e){ 
       $response['error']=true; 
       $response['message']=$e->getMessage(); 
      }  
      //displaying the response 
      echo json_encode($response); 

      //closing the connection 
      mysqli_close($con); 
     }else{ 
      $response['error']=true; 
      $response['message']='Please choose a file'; 
     } 
    } 

    /* 
     We are generating the file name 
     so this method will return a file name for the image to be upload 
    */ 
    function getFileName(){ 
     $con = mysqli_connect(HOST,USER,PASS,DB) or die('Unable to Connect...'); 
     $sql = "SELECT max(id) as id FROM images"; 
     $result = mysqli_fetch_array(mysqli_query($con,$sql)); 

     mysqli_close($con); 
     if($result['id']==null) 
      return 1; 
     else 
      return ++$result['id']; 
    } 

此文件是从一个教程服用。 这是情景:

的PHP文件是: http://myserver.com/danyra/android_login_api/upload.php

,我想存储上传的图片的文件夹在:

http://myserver.com/danyra/administrar/application/admin/usuarios

我使用邮递员检查脚本,我总是收到此错误:

Warning: move_uploaded_file(usuarios/18.png): failed to open stream: No such file or directory in /home2/kokls/public_html/myserver.com/danyra/android_login_api/upload.php on line 45 

Warning: move_uploaded_file(): Unable to move '/tmp/phpMciKUa' to 'usuarios/18.png' in /home2/kokls/public_html/myserver.com/danyra/android_login_api/upload.php on line 45 
    {"error":false,"url":"http:\/\/XXX.XXX.246.130\/danyra\/administrar\/application\/admin\/usuarios\/18.png","name":"fsd"} 

我尝试了很多改变路径的选项,但没有成功。

任何帮助,欢迎。

+0

检查目录权限和路径 – Roljhon

+0

@Roljhon,目录权限是755,和路径是正确的 – mvasco

+0

你试过'$ _ SERVER [“DOCUMENT_ROOT”]',而不是服务器的ip ? – Roljhon

回答

0

我认为你当前的路径不正确。你应该使用相对路径。 尝试这条道路

$upload_path = $_SERVER['DOCUMENT_ROOT'].'/danyra/administrar/application/admin/usuarios/'; 
+0

谢谢,它的作品 – mvasco

+0

@mvasco欢迎:) – Roljhon