将文件从Android应用程序上传到所需的文件夹

时间:2017-06-09 06:00:30

标签: php mysql

我想在下面的特定文件夹中使用PHP从Android应用程序上传文件是我尝试过的代码。 请求帮助我这个代码有什么问题,请为我建议一些简单的解决方案,或者这个方法是从serever上的Android应用程序上传文件的权利

 $response = array();

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

        //checking the required parameters from the request
        if(isset($_POST['exp']) && isset($_POST['employee_id']) && isset($_FILES['pdf']['name'])  ){

                //connecting to the database
            $con = mysqli_connect(DB_SERVER,DB_USER,DB_PASSWORD,DB_DATABASE) or die('Unable to Connect...');

            $resume_name = $_POST['exp'];
            $employee_id = $_POST['employee_id'];
            $file_data = $_FILES['pdf']['name'];

            $upload_path = 'Images/Employee_Profile_Picture/'.$employee_id.'/Resume/';

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

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

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

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

         try{

            if(file_exists($upload_path))
            {
               $existing_file = glob($upload_path."/*.*");
               $empty_file = implode(" ",$existing_file);

              move_uploaded_file($_FILES['pdf']['name'],$upload_path) ;

    $sql = "UPDATE employee_registration SET resume_name ='$resume_name', resume_path='$file_url' where employee_id ='$employee_id'";

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

                    //filling response array with values

                    $response['Success'] = "File Uploaded Successfully...!";
                    echo json_encode($response);

                }
                else
                {
                  $response['Error'] = "File Uploading Error...!";
                  echo json_encode($response);
                }

            }
                else
                {
                      mkdir('Images/Employee_Profile_Picture/'.$employee_id.'/Resume');
                      move_uploaded_file($_FILES['pdf']['name'],$upload_path) ;

        $sql = "UPDATE employee_registration SET resume_name ='$resume_name', resume_path='$file_url' where employee_id ='$employee_id'";

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

                             //filling response array with values

                                 $response['Success'] = "File Uploaded Successfully...!";
                                 echo json_encode($response);

                            }
                        else
                            {
                                $response['Error'] = "File Uploading Error...!";
                                echo json_encode($response);
                            }

                }
            }catch(Exception $e){
                $response['error']=true;
                $response['message']=$e->getMessage();
            }
    }

    }


      //here is my method getFileName

function getFileName($employee_id)
 {

   //mysql query to fetch data 
   $sql = mysql_query("SELECT resume_path from employee_registration where 
     employee_id = '$employee_id'") or die(mysql_error());
   while ($row = mysql_fetch_array($sql, MYSQL_ASSOC)) 
   {
     $response=$row['resume_path'];
   }
    $resume_name = explode("/", $response);
   echo $resume_name[4]; 
   return $resume_name;
  }

2 个答案:

答案 0 :(得分:2)

更改以下行:

move_uploaded_file($_FILES['pdf']['name'],$upload_path) ;

move_uploaded_file($_FILES['pdf']['tmp_name'],$upload_path) ;
应该使用

tmp_name来上传文件,因为它具有临时存储文件的完整路径。其中名称仅包含没有任何路径信息的文件名。

答案 1 :(得分:0)

如果这项工作正常move_uploaded_file($_FILES['pdf']['tmp_name'],$upload_path) ;

请使用此file_put_contents($ upload_path,$ _ FILES ['pdf'] ['tmp_name']);