警告ftp_put()文件名不能为空

时间:2019-04-20 13:49:51

标签: php file-upload

我需要使用php和ftp上传一些视频和音频文件。我正在使用php内置的ftp函数,但是ftp_put()函数存在一些问题。在测试代​​码时,它将继续给我一个与文件名有关的错误。我该如何解决。

当我尝试上传文件时,这是php控制台的输出:

Warning: ftp_put(): Filename cannot be empty in /Users/uc/Desktop/c/FtpManager.php on line 37

这是$_FILES数组转储: array(5) { ["name"]=> string(8) "em_1.mp4" ["type"]=> string(0) "" ["tmp_name"]=> string(0) "" ["error"]=> int(1) ["size"]=> int(0) }

我用于脚本原型的代码如下:

<?php 
  /* Upload video */
  public function uploadVideo(array $inputFile){
      if( ftp_chdir( $this->conn, "/cloud.mywebsite.com/" ) ){
        $upload = ftp_put( $this->conn, $inputFile['video_file']['name'], $inputFile['video_file']['name'], FTP_BINARY);
        if( $upload ){
          echo 'File uploaded!';
        }
      }
  }

if(isset($_POST['upload_video'])){
  echo $ftp->uploadVideo($_FILES['video_file']);
}

?>

<form enctype="multipart/form-data" method="POST" action="">
  <input type="file" name="video_file" />
  <input type="submit" name="upload_video">
</form>

2 个答案:

答案 0 :(得分:1)

错误的常见原因是超过table-x。至少,在尝试上传FTP之前,请检查文件maximum allowed filesize中的错误

$_FILES

答案 1 :(得分:0)

感谢您的帮助,在对代码进行一些调试之后,我找到了解决方案。这是代码的工作片段。我无法使用ftp_put()函数,因为未配置计算机上的嵌入式macOS php服务器。我已经从它切换到MAMP,并且已经解决了上传文件大小的问题。我还需要将ftp_pasv()模式设置为on,以避免上传的文件出现问题。

<?php 

class ftp{

public function ftpLogin(){
    if( ftp_login($this->conn, $this->user, $this->pwd) ){
      ftp_pasv($this->conn, true);
      echo "Connected to: $this->host !";
    }
}

public function uploadVideo(array $inputFile){
      if( ftp_chdir( $this->conn, "/mysite.subdomain.com/" ) ){
        $upload = ftp_put( $this->conn, $inputFile['name'], $inputFile['tmp_name'], FTP_BINARY);
        if( $upload ){
          echo 'File uploaded!';
        }
      }
}

}

?>