【问题标题】:Warning ftp_put() filename cannot be empty警告 ftp_put() 文件名不能为空
【发布时间】:2019-04-20 13:49:50
【问题描述】:

我需要使用 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>

【问题讨论】:

  • ["error"]=&gt; int(1) -- 这意味着您的上传有错误。值1是UPLOAD_ERR_INI_SIZE错误,即means“上传的文件超出了php.ini中的upload_max_filesize指令。”
  • @rickdenhaan 我正在使用 macOS 的 embed php 服务器。我没有设置 php ini,但我用来测试的视频只​​有 3MB 大。我遇到的主要问题是ftp_put() 函数

标签: php file-upload


【解决方案1】:

错误的一个常见原因是超出了maximum allowed filesize。至少,在尝试 FTP 上传之前检查文件的 $_FILES 条目中的错误

if ($_FILES['video_file']['error'] != UPLOAD_ERR_OK) {
  // handle the error instead of uploading e.g. give a message to user
}

【讨论】:

  • @dwpu 请分享 print_r($_FILES) 的输出
  • 检查问题,我已经发布了。该数组只有文件名信息和错误。
  • @dwpu 因为我可以看到索引错误的值为 1,这意味着您上传文件时出错。请检查您的 ini 中的 post_max_size
  • 我已经从 macOS 的 embed php 服务器切换到 MAMP,并且文件数组现在填充了有关我用于代码测试的视频的所有信息。问题仅在于ftp_put()。这是$_FILES数组内容:array(5) { ["name"]=&gt; string(8) "em_1.mp4" ["type"]=&gt; string(9) "video/mp4" ["tmp_name"]=&gt; string(36) "/Applications/MAMP/tmp/php/php8tx1cg" ["error"]=&gt; int(0) ["size"]=&gt; int(3625754) }
  • 奇怪的问题是关于代码中不存在的文件变量,但控制台记录了这两个错误:PHP Notice: Undefined variable: filePHP Warning: ftp_put(): Filename cannot be empty
【解决方案2】:

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

<?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!';
        }
      }
}

}

?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-19
    • 1970-01-01
    • 2012-05-24
    • 2014-01-31
    • 2013-06-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多