簡體   English   中英

將文件從android應用上傳到所需的文件夾

[英]uploading file from android app into desired folder

我想在下面特定的文件夾中使用PHP從Android應用程序上傳文件,這是我嘗試過的代碼。 請幫助我這段代碼有什么問題,請建議我一些簡單的解決方案,或者這種方法正確地從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;
  }

更改以下行:

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

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

tmp_name應該用於上傳文件,因為它具有臨時存儲文件的完整路徑。 其中,as名稱僅包含文件名,而沒有任何路徑信息。

如果這樣做確實可以工作move_uploaded_file($_FILES['pdf']['tmp_name'],$upload_path) ;

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM