使用ftp_put上传文件

时间:2014-05-06 06:56:45

标签: php ftp

我已经解决了下面的原始问题。如果我需要为文件上传器插入进度条,最好的方法是什么?使用ftp_put方法,在文件成功上传之前,不允许从服务器获取响应。

谢谢!


我正在尝试使用 ftp_put 上传文件,出于某种原因,当我通过localhost运行代码时,它给出了以下错误:

  

警告:ftp_put():无法打开该文件:第25行的C:\ xampp \ htdocs \ ftp \ upload_file.php目录

<form action="upload_file.php" enctype="multipart/form-data" method="post">
<input name="uploadedfile" type="file" />
<input name="submit" type="submit" value="Upload File" />
</form>

PHP代码:

<?php
$ftp_server = "";
$ftp_user_name = "";
$ftp_user_pass = "";
$destination_file = "/public_html/testing/";
$source_file = $_FILES['uploadedfile']['tmp_name']; 

// set up basic connection
$conn_id = ftp_connect($ftp_server);
ftp_pasv($conn_id, true); 

// 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!";
    echo "Attempted to connect to $ftp_server for user $ftp_user_name"; 
    exit; 
} else {
    echo "Connected to $ftp_server, for user $ftp_user_name";
}

// upload the file
$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY); 

// check upload status
if (!$upload) { 
    echo "FTP upload has failed!";
} else {
    echo "Uploaded $source_file to $ftp_server as $destination_file";
}
// close the FTP stream 
ftp_close($conn_id);
?>

谢谢。

2 个答案:

答案 0 :(得分:1)

请测试目的地的chdir或查看this on php.net

    Got this cryptic error

Warning:  ftp_put() [function.ftp-put]: 'STOR' not understood in
C:\wamp\www\kevtest\ftp_todays.php on line 48

Found the prob, you can't put a path to the destination file
(even though I can do that in the dos ftp client...?)

e.g. - this doesn't work
ftp_put($conn, '/www/site/file.html','c:/wamp/www/site/file.html',FTP_BINARY);

you have to put

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

答案 1 :(得分:0)

        <?php

        if(isset($_POST['submit']))
            {
                ini_set('max_execution_time', 60000);

        $file = $_FILES['video']['tmp_name'];
        $destination_file = "/public_html/".time().basename($_FILES['video']['name']);
        // connect and login to FTP server
        $ftp_server = ".com";
        $ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
        $login = ftp_login($ftp_conn, 'name', 'pass');

        // upload file
        if (ftp_put($ftp_conn, $destination_file, $file, FTP_BINARY))
          { echo "Successfully uploaded ".$_FILES['video']['name']." "; }
        else
          { echo "Error uploading $file.";  }

        // close connection
        ftp_close($ftp_conn);
        }
        ?>
        <!DOCTYPE html>
        <html lang="en">
        <head>
        <body>
        <div class="container">
        <form name="frm" id="frm" method="post" enctype="multipart/form-data">
        <div class="input-group">
        <input type="file" name="video" required="required" />
        </div>
        <div class="input-group">
        <input type="submit" name="submit" value="Submit" onClick="move()"/>
        </div>
        </form>
        </div>

        </body>

        </html>