使用FTP和php上传文件 [英] upload a file using FTP and php

查看:190
本文介绍了使用FTP和php上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



如何发送此函数的路径



PHP文件

  $ file = $ _POST [file]; 

//上传文件
if(ftp_put($ ftp_conn,$ file,$ file,FTP_BINARY))
{
echo成功上传$文件。
}
else
{
echo上传$ file时出错。
}

//关闭连接
ftp_close($ ftp_conn);

HTML文件



 < div class =container> 

< div class =row>
< div class =col-lg-12>
< form class =wellaction =Upload.phpmethod =post>
< div class =form-group>
< label for =file>选择要上传的文件< / label>
< input type =filename =fileid =file>

<! - < p class =help-block>只允许jpg,jpeg,png和gif文件,最大大小为1 MB。< / p> - >
< / div>
< input type =submitclass =btn btn-lg btn-primaryvalue =Upload>
< / form>
< / div>
< / div>
< / div>


解决方案使用 $ _ FILES [文件] [tmp_name] 而不是 $ _ POST [file]

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

//上传文件
if(ftp_put($ ftp_conn,$ file_name,$ file,FTP_BINARY))

或者先移动上传的文件:

  $ target_path =uploads /。基本名($ _ FILES [ 文件] [ 名称]); 
move_uploaded_file($ _ FILES [file] [tmp_name],$ target_path);


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> 

解决方案

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

这篇关于使用FTP和php上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆