dwpu
dwpu

Reputation: 268

Warning ftp_put() filename cannot be empty

I need to upload some video and audio files using php and ftp. I'm using the php built-in ftp functions but I'm having some problems with the ftp_put() function. While I'm testing the code, it will continue to give me an error related to the file name. How I can fix this.

Here is the output of the php console when I try to upload the file:

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

And this is the $_FILES array dump: array(5) { ["name"]=> string(8) "em_1.mp4" ["type"]=> string(0) "" ["tmp_name"]=> string(0) "" ["error"]=> int(1) ["size"]=> int(0) }

The code I'm using to prototype the script is the following:

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

Upvotes: 0

Views: 272

Answers (2)

dwpu
dwpu

Reputation: 268

Thanks for your help and after a bit of debug of the code I've found a solution. Here is a working snippet of the code. I wasn't able to use the ftp_put() function because the embedded macOS php server on my machine isn't configured. I've switched from it to MAMP and I've solved the upload file size issue. I also needed to set the ftp_pasv() mode to on to avoid problem with the uploaded files.

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

}

?>

Upvotes: 0

Rakesh Jakhar
Rakesh Jakhar

Reputation: 6388

A common reason for the error is exceeding the maximum allowed filesize. At the very least, check the error in the $_FILES entry for your file before attempting the FTP upload

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

Upvotes: 1

Related Questions