使用 FTP 和 php 上传文件

upload a file using FTP and php

我无法使用 php 上传文件,因为我无法将从 html 文件获得的路径发送到函数 FTP_PUT,因为它只需要字符串 "test.txt"

如何将路径发送到此函数

PHP 文件

$file = $_POST["file"];

// upload file
if (ftp_put($ftp_conn, $file, $file, FTP_BINARY))
  {
  echo "Successfully uploaded $file.";
  }
else
  {
  echo "Error uploading $file.";
  }

// close connection
ftp_close($ftp_conn);

HTML 文件

<div class="container">
 
          <div class="row">
            <div class="col-lg-12">
               <form class="well" action="Upload.php" method="post" >
                  <div class="form-group">
                    <label for="file">Select a file to upload</label>
                    <input type="file" name="file" id="file">

                   <!-- <p class="help-block">Only jpg,jpeg,png and gif file with maximum size of 1 MB is allowed.</p> -->
                  </div>
                  <input type="submit" class="btn btn-lg btn-primary" value="Upload">
                </form>
            </div>
          </div>
    </div> 

将您的表单标签更改为:

<form class="well" action="Upload.php" method="post"  enctype="multipart/form-data">

如果您不包括

enctype="multipart/form-data"

不会上传任何内容!

使用$_FILES["file"]["tmp_name"]代替$_POST["file"]

编辑:

$file = $_FILES["file"]["tmp_name"];
$file_name = $_FILES["file"]["name"];

// upload file
if (ftp_put($ftp_conn, $file_name, $file, FTP_BINARY))

或者先移动上传的文件:

$target_path = "uploads/".basename($_FILES["file"]["name"]); 
move_uploaded_file($_FILES["file"]["tmp_name"], $target_path);

检查路径是否正确...我不知道您的文件结构所以我猜您需要完整路径,试试...

 if (ftp_put($ftp_conn, getcwd().$file, $file, FTP_BINARY)