Categories : PHP

In this article i explained all about how you can interact with ftp in php.

file_name is the name of the file to download

ftp_server is the ftp server address

ftp_user_name is the ftp username

ftp_user_pass is the ftp password

ftp_port is the ftp port

ftp_timeout is the ftp timeout

ftp_passive is the ftp passive mode

ftp_ssl is the ftp ssl mode

ftp_ssl is the ftp ssl mode

ftp_ssl_verify is the ftp ssl verify mode

php ftp download function by: FTP credentials

returns the file contents

parameters of ftp_download function are: file_server file_user_name file_user_pass file_name ftp_local_file

<?php


function ftp_download($ftp_server, $ftp_user_name, $ftp_user_pass, $ftp_file, $ftp_local_file) {
    $conn_id = ftp_connect($ftp_server);
    $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
    if ((!$conn_id) || (!$login_result)) {
        return false;
    } else {
        ftp_pasv($conn_id, true);
        $download = ftp_get($conn_id, $ftp_local_file, $ftp_file, FTP_BINARY);
        ftp_close($conn_id);
        return $download;
    }
}

php ftp upload function by: FTP credentials

  • php ftp upload function by: FTP credentials
  • returns true if upload was successful
  • parameters: ftp server, ftp user name, ftp user password, ftp file, local file
<?php


function ftp_upload($ftp_server, $ftp_user_name, $ftp_user_pass, $ftp_file, $ftp_local_file) {
    $conn_id = ftp_connect($ftp_server);
    $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
    if ((!$conn_id) || (!$login_result)) {
        return false;
    } else {
        ftp_pasv($conn_id, true);
        $upload = ftp_put($conn_id, $ftp_file, $ftp_local_file, FTP_BINARY);
        ftp_close($conn_id);
        return $upload;
    }
}

php ftp file create function by: FTP credentials

  • php ftp file create function by: FTP credentials
  • returns true if file created, false if not
  • parameters: ftp_server, ftp_user_name, ftp_user_pass, ftp_file
<?php



function ftp_create_file($ftp_server, $ftp_user_name, $ftp_user_pass, $ftp_file) {
    $conn_id = ftp_connect($ftp_server);
    $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
    if ((!$conn_id) || (!$login_result)) {
        return false;
    } else {
        ftp_pasv($conn_id, true);
        $upload = ftp_put($conn_id, $ftp_file, '', FTP_BINARY);
        ftp_close($conn_id);
        return $upload;
    }
}

php ftp file delete function by: FTP credentials

  • php ftp file delete function by: FTP credentials
  • returns true if file deleted, false if not
  • parameters: ftp server, ftp user name, ftp user password, ftp file
<?php
function ftp_delete($ftp_server, $ftp_user_name, $ftp_user_pass, $ftp_file) {
    $conn_id = ftp_connect($ftp_server);
    $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
    if ((!$conn_id) || (!$login_result)) {
        return false;
    } else {
        ftp_pasv($conn_id, true);
        $delete = ftp_delete($conn_id, $ftp_file);
        ftp_close($conn_id);
        return $delete;
    }
}

php ftp file list function by: FTP credentials

  • php ftp file list function by: FTP credentials
  • returns an array of files
  • parameters of ftp_list function are: ftp_server, ftp_user_name, ftp_user_pass, ftp_file
function ftp_list($ftp_server, $ftp_user_name, $ftp_user_pass, $ftp_file) {
    $conn_id = ftp_connect($ftp_server);
    $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
    if ((!$conn_id) || (!$login_result)) {
        return false;
    } else {
        ftp_pasv($conn_id, true);
        $list = ftp_nlist($conn_id, $ftp_file);
        ftp_close($conn_id);
        return $list;
    }
}

php ftp file size function by: FTP credentials

  • php ftp file size function by: FTP credentials
  • returns size in bytes
  • parameters of ftp_size function are: ftp_server, ftp_user_name, ftp_user_pass, ftp_file
function ftp_file_size($ftp_server, $ftp_user_name, $ftp_user_pass, $ftp_file) {
    $conn_id = ftp_connect($ftp_server);
    $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
    if ((!$conn_id) || (!$login_result)) {
        return false;
    } else {
        ftp_pasv($conn_id, true);
        $size = ftp_size($conn_id, $ftp_file);
        ftp_close($conn_id);
        return $size;
    }
}

php ftp file exists function by: FTP credentials

  • php ftp file exists function by: FTP credentials
  • returns true if file exists
  • parameters of ftp_file_exists function are: ftp_server, ftp_user_name, ftp_user_pass, ftp_file
function ftp_file_exists($ftp_server, $ftp_user_name, $ftp_user_pass, $ftp_file) {
    $conn_id = ftp_connect($ftp_server);
    $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
    if ((!$conn_id) || (!$login_result)) {
        return false;
    } else {
        ftp_pasv($conn_id, true);
        $exists = ftp_size($conn_id, $ftp_file);
        ftp_close($conn_id);
        if ($exists == -1) {
            return false;
        } else {
            return true;
        }
    }
}

php ftp file rename function by: FTP credentials

  • php ftp file rename function by: FTP credentials
  • reutrn true if success
  • parameters: ftp server, ftp user name, ftp user password, ftp file, new ftp file
function ftp_rename($ftp_server, $ftp_user_name, $ftp_user_pass, $ftp_file, $new_ftp_file) {
    $conn_id = ftp_connect($ftp_server);
    $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
    if ((!$conn_id) || (!$login_result)) {
        return false;
    } else {
        ftp_pasv($conn_id, true);
        $rename = ftp_rename($conn_id, $ftp_file, $new_ftp_file);
        ftp_close($conn_id);
        return $rename;
    }
}

Leave a Reply

Your email address will not be published.