在Android应用程序中将文件上传到FTP服务器

时间:2013-03-07 07:41:09

标签: android ftp-client

我尝试了几种在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;
?> 

2 个答案:

答案 0 :(得分:0)

如果要插入

,则必须包含 move_uploaded_file 功能

语法:move_uploaded_file(string $ filename,string $ destination)

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

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

答案 1 :(得分:0)

这是糟糕的代码。为什么不在没有服务器端脚本的情况下通过ftp从android设备上传文件? see here