upload a file using FTP and php

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-02 19:57:45

问题


I can't Upload the file using php because i can't send the path i got from the html file to the function FTP_PUT because it only takes string "test.txt"

How Can i send the Path to this function

PHP FILE

$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 FILE

    

<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> 

回答1:


Use $_FILES["file"]["tmp_name"] instead of $_POST["file"]

edit:

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

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

or move the uploaded file first:

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



回答2:


Change your form tag to :

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

If you don't include

enctype="multipart/form-data"

Nothing will get uploaded!




回答3:


Check that the path is correct... I don't know your file structure so I'm guessing you need the full path, try...

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


来源:https://stackoverflow.com/questions/34465646/upload-a-file-using-ftp-and-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!