2013-03-07 147 views
-1

我嘗試了幾種上傳FTP服務器上的文件的方法,但是我失敗了。在android應用程序上傳文件到FTP服務器

在這裏我粘貼我的服務器代碼(php)以接受來自設備的文件,請提供與該服務器代碼對應的android代碼。

<?php 
error_reporting(0); 
include("dbconfig.php"); 
$msg = ''; 
$status = 1; 
$overwrite = 1; 
/*echo "<pre>"; 
print_r($_POST); 
echo "</pre>";*/ 

/*echo "<pre>"; 
print_r($_FILES); 
echo "</pre>";*/ 
if(trim($_POST['userid'])!='') 
{ 
    $userid = trim($_POST['userid']); 
    $overwrite = trim($_POST['overwrite']); 
    if($overwrite == 0) 
    { 
     $selfilesqry = mysql_query("SELECT filepath FROM userfiles WHERE userid = 1 AND status=1"); 
     while($row = mysql_fetch_array($selfilesqry)) 
     { 
      $selfiles[] = $row['filepath']; 
     } 

    } 
/* echo "<pre>"; 
    print_r($selfiles); 
    echo "</pre>";*/ 
    $host = '97.***.****'; 
    $usr = '*****************8'; 
    $pwd = '**********************'; 

    $paths = '/mobbisys/cloudbin/data'; 
    //$paths = '/php_projects/mahesh/ftpupload/data'; 

    // connect to FTP server (port 21) 
    $conn_id = ftp_connect($host, 21) or die ("Cannot connect to host"); 

    // send access parameters 
    ftp_login($conn_id, $usr, $pwd) or die("Cannot login"); 

    for($i=0; $i<count($_FILES['txtfile']['name']); $i++) 
    { 
     if($_FILES['txtfile']['name'][$i]!='') 
     { 
      $type = explode('/',$_FILES['txtfile']['type'][$i]); 
      $filetype = $type[0];   
      if($filetype == 'audio') 
      { 
       $path = $paths."/audio"; 
      } 
      else if($filetype == 'video') 
      { 
       $path = $paths."/video"; 
      } 
      else if($filetype == 'image') 
      { 
       $path = $paths."/image"; 
      } 
      else 
      { 
       $path = $paths."/misc"; 
      } 
      //echo "<br />".$path; 
      $name = $_FILES['txtfile']['name'][$i]; 
      $filep = $_FILES['txtfile']['tmp_name'][$i];  
      // turn on passive mode transfers (some servers need this) 
      // ftp_pasv ($conn_id, true); 
      $filepath = strtolower($path.'/'.$name); 
      if($overwrite == 1) 
      { 
       // perform file upload 
       $upload = ftp_put($conn_id, $path.'/'.$name, $filep, FTP_BINARY); 

       // check upload status: 
       if(!$upload) 
       { 
        $msg .= '<br />Cannot upload - '.$_FILES['txtfile']['name'][$i]; 
       } 
       else 
       { 
        $msg .= '<br />Upload complete - '.$_FILES['txtfile']['name'][$i]; 
        $sqlAdd = "INSERT INTO userfiles(userid, filetype, filepath, createddate, modifieddate, status) VALUES(".$userid.",'".$filetype."','".$filepath."',NOW(),NOW(),'".$status."') "; 
        $resAdd = mysql_query($sqlAdd);    
       } 
      } 
      else 
      { 
       if(in_array($filepath, $selfiles)) 
       { 
        $msg .= '<br />Cannot upload - '.$_FILES['txtfile']['name'][$i].' already exist.'; 
       } 
       else 
       {   
        // perform file upload 
        $upload = ftp_put($conn_id, $path.'/'.$name, $filep, FTP_BINARY); 

        // check upload status: 
        if(!$upload) 
        { 
         $msg .= '<br />Cannot upload - '.$_FILES['txtfile']['name'][$i]; 
        } 
        else 
        { 
         $msg .= '<br />Upload complete - '.$_FILES['txtfile']['name'][$i]; 
         $sqlAdd = "INSERT INTO userfiles(userid, filetype, filepath, createddate, modifieddate, status) VALUES(".$userid.",'".$filetype."','".$filepath."',NOW(),NOW(),'".$status."') "; 
         $resAdd = mysql_query($sqlAdd);    
        } 
       } 
      } 
     } 
    } 
    // close the FTP stream 
    ftp_close($conn_id); 
} 
else 
{ 
    $msg = 'Please provide userid'; 
} 
echo $msg; 
?> 

回答

0

你必須包括move_uploaded_file功能,當你想插入

語法:move_uploaded_file(字符串$文件名,字符串$目的地)

move_uploaded_file($_FILES['uploadedfile']['tmp_name'], "./upload/".$_FILES["uploadedfile"]["name"]);

http://php.net/manual/en/function.move-uploaded-file.php

0

這是錯誤的代碼。爲什麼不通過ftp從Android設備上傳文件而沒有服務器端腳本? see here