doufei0933
doufei0933
2010-08-17 13:09

如何在PHP中通过ftp上传文件?

已采纳

I have a form with html browse type and a submit button. I chose a file using browse button and submit the form. On form submission following code is called.

$conn_id="myid";
$conn_id = ftp_connect ( 'server' );
$ftp_user_name="username";
$ftp_user_pass="password";

// login with username and password
$login_result = ftp_login ( $conn_id , $ftp_user_name , $ftp_user_pass );

// check connection
if ((! $conn_id ) || (! $login_result )) {
    echo "FTP connection has failed!" ;
    exit;

} else {
    echo "Connected to for user $ftp_user_name" ;
} 

// upload the file
$upload = ftp_put( $conn_id, "images/signatures/" . $fileName , $_FILES['tmp_name'] , FTP_BINARY );

// check upload status
if(!$upload){
    echo "FTP upload has failed!" ;
} else {
    echo "Successfully Uploaded." ;
}

But it produce the following warning:

Warning: ftp_put(): Filename cannot be empty in /var/www/echdp/_ProviderSignature.php on line 70 FTP upload has failed!

But when I hard code the source path in above code then it upload the file on server:

$upload = ftp_put( $conn_id, "images/signatures/myfile.txt" , "/var/www/images/hello.txt" , FTP_BINARY );
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

4条回答

  • doujuegai8830 doujuegai8830 11年前

    Example of html code with enctype:

    <form action="upload_file.php" method="post" enctype="multipart/form-data">
    <label for="file">Filename:</label>
    <input type="file" name="myfile" id="file" /> 
    <br />
    <input type="submit" name="submit" value="Submit" />
    </form>
    

    Do you have enctype in Form tag? Without the attribute your file won't be send.

    For debugging of $_FILES array. Try

    var_dump($_FILES);
    

    to see if the data are correct.

    Your variable:

    $_FILES['tmp_name'] 
    

    is used wrongly because $_FILES contain files not a one file. Therefore you should write:

    $_FILES['your-name-in-html-form']['tmp_name'] // your-name-in-html-form = myfile in the example above
    

    Have a look on the example: http://www.zymic.com/tutorials/php/creating-a-file-upload-form-with-php/

    点赞 评论 复制链接分享
  • dongzai5181 dongzai5181 11年前
    $_FILES['tmp_name'] 
    

    isn't name of your file. It is stored in:

    $_FILES['inputname']['tmp_name']
    

    where inputname is name of your field. Also check your form for enctype ="multipart/form-data".

    Be aware of simply putting on your server everything that user can send - if there is access to uploaded files by http (like http://yoursite/images/signatures/uploadedfile.xxx) someone can put .php files on your server and execute it !

    点赞 评论 复制链接分享
  • dongtuo1482 dongtuo1482 11年前

    Obviously $_FILES['tmp_name'] does not contain the filename. Try print_r($_FILES) to see what it contains. Also, use multipart/form-data as your form enctype.

    点赞 评论 复制链接分享
  • douqianzha6213 douqianzha6213 6年前

    Try this:

    $file = 'somefile.txt';
    $remote_file = '/public_html/dir/dir2/dir3/somefile.txt';
    $ftp_server = "yourdomain.in";
    $ftp_user_name = "ftpusername";
    $ftp_user_pass = "ftppassword";
    
    // set up basic connection
    $conn_id = ftp_connect($ftp_server);
    
    // login with username and password
    $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
    
    // upload a file
    if (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)) {
     echo "successfully uploaded $file
    ";
    } else {
     echo "There was a problem while uploading $file
    ";
    }
    
    // close the connection
    ftp_close($conn_id);
    

    Your file will be uploaded in the /public_html/dir/dir2/dir3/ directory of your domain.
    Caution: Never upload an index file when you are testing this feature.
    Source: PHP.net

    点赞 评论 复制链接分享

相关推荐