ftp_put将文件上传到服务器问题

时间:2014-10-10 01:42:37

标签: php

我有一个HTML表单,旨在允许文件上传到服务器。我正在编写一个FTP客户端,到目前为止进展顺利,只是文件不会上传到服务器。我的表格如下:

<form action='upload.php' id='upload'>
    <input type='file' name='file' />
    <input name='file_name' placeholder='File Name' />
    <input type='submit' value='upload' />
</form>

这是我的php:

<?php
    $ftp_connection = ftp_connect($_COOKIE['domain']);
    if(@ftp_login($ftp_connection, $_COOKIE['username'], $_COOKIE['password'])) {
        ftp_put($ftp_connection, $_REQUEST['file_name'], $_REQUEST['file']);
    }
    ftp_close($ftp_connection);
?>

另请注意,所有这些cookie都可以正常工作,因为我使用它们来登录FTP GUI。

1 个答案:

答案 0 :(得分:2)

您的表单缺少enctype="multipart/form-data",这在上传文件时是必需的。

还需要POST方法。

将您的<form...修改为:

<form action='upload.php' id='upload' enctype='multipart/form-data' method='post'>

<form>在省略时默认为GET。

另请看:

该页面的示例:

<?php
$ftp_server="";
$ftp_user_name="";
$ftp_user_pass="";
$file = "";//tobe uploaded
$remote_file = "";

// 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\n";
    exit;
} else {
    echo "There was a problem while uploading $file\n";
    exit;
    }
// close the connection
ftp_close($conn_id);
?>

该页面的示例:

<?php
ftp_chdir($conn, '/www/site/');
ftp_put($conn,'file.html', 'c:/wamp/www/site/file.html', FTP_BINARY );
?>

<?PHP
        $destination_path = "src/bin/";

//where you want to throw the file on the webserver (relative to your login dir)

    $destination_file = $destination_path."img.jpg";

//This will create a full path with the file on the end for you to  use, I like splitting the variables like this in case I need to use on on their own or if I'm dynamically creating new folders.

        $file = $myFile['tmp_name'];

//Converts the array into a new string containing the path name on the server where your file is.

    $upload = ftp_put($conn_id, $destination_file, $file, FTP_BINARY);// upload the file
    if (!$upload) {// check upload status
        echo "FTP upload of $destination_file has failed!";
    } else {
        echo "Uploaded $file to $conn_id as $destination_file";
    }
?>
相关问题