Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • PHP Tutorial
  • PHP Exercises
  • PHP Array
  • PHP String
  • PHP Calendar
  • PHP Filesystem
  • PHP Math
  • PHP Programs
  • PHP Array Programs
  • PHP String Programs
  • PHP Interview Questions
  • PHP GMP
  • PHP IntlChar
  • PHP Image Processing
  • PHP DsSet
  • PHP DsMap
  • PHP Formatter
  • Web Technology
Open In App
Next Article:
PHP | ftp_exec() function
Next article icon

PHP | ftp_connect() function

Last Updated : 07 Aug, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

The ftp_connect() function is an inbuilt function in PHP which is used to create a new connection to the specified FTP server or Host. When connection is successful then only other FTP functions can be run against the server.

Syntax: 

ftp_connect( $ftp_host, $ftp_port, $timeout );

Parameter: This function accepts three parameters as mentioned above and described below:  

  • $ftp_host: It is required parameter and used to specify the host name or the ftp server to connect to. It can be domain name or IP address and these address must not prefixed with “ftp://” or must not have any slashes at the end of that url.
  • $ftp_port: It is optional parameter. It specifies the port number to connect to. If it is not provided then the default port number for FTP is used. The default ftp port number is 21.
  • $timeout: It is optional parameter. It specifies the timeout for all subsequent network operation. If this parameter is not provided then the default parameter is being used, which is 90 seconds.

Note: Timeout can be queried or changed anytime using ftp_get_option() and ftp_set_option() accordingly.

Return Value: It returns FTP stream on success or False on failure.

Note:  

  • This function is available for PHP 4.0.0 and newer version.
  • The following examples cannot be run on online IDE. So try to run in some PHP hosting server or localhost with proper ftp server name.

Below programs illustrate the ftp_connect() function in PHP:

Example 1:  

PHP

<?php
 
// Connect to FTP server
$ftp_server = "localhost";
 
// Establish ftp connection
$ftp_connection = ftp_connect($ftp_server)
    or die("Could not connect to $ftp_server");
  
if($ftp_connection) {
    echo "Successfully connected to the ftp server!";
     
    // Closing  connection
    ftp_close($ftp_connection);
}
 
?>
                      
                       

Output: 

Successfully connected to the ftp server!

Example 2: Connect to ftp server using port 21.  

PHP

<?php
 
// Connect to FTP server
$ftp_server = "localhost";
 
// Establish ftp connection
$ftp_connection = ftp_connect($ftp_server, 21)
    or die("Could not connect to $ftp_server");
 
// Port number 21 is used as second parameter
// in the function ftp_connect()
if( $ftp_connection ) {
    echo "Successfully connected to the ftp server!";
     
    // Closing  connection
    ftp_close( $ftp_connection );
}
 
?>
                      
                       

Output: 

Successfully connected to the ftp server!

Reference: https://www.php.net/manual/en/function.ftp-connect.php
 



Next Article
PHP | ftp_exec() function

G

gekcho
Improve
Article Tags :
  • PHP
  • Web Technologies
  • PHP- FTP
  • PHP-function

Similar Reads

  • PHP | ftp_ssl_connect() Function
    The ftp_ssl_connect() function is an inbuilt function in PHP which opens a secure SSL-FTP connection. FTP functions can be run against the server while the connection is open. It opens an explicit SSL-FTP connection to the host specified in parameter. It will succeed even if the server's certificate
    2 min read
  • PHP | ftp_exec() function
    The ftp_exec() function is an inbuilt function in PHP that is used to execute a command on the FTP server. Syntax: ftp_exec( $ftp_connection, $command ) Parameters: This function accepts two parameters as mentioned above and described below: $ftp_connection: It is required parameter. It specifies th
    2 min read
  • PHP | ftp_alloc() function
    The ftp_alloc() function is an inbuilt function in PHP which is used to allocate space for file to be uploaded in FTP server.Syntax: ftp_alloc( $ftp_connection, $filesize, $result ); Parameter: This function accepts three parameters as mentioned above and described below: $ftp_connection: It is requ
    3 min read
  • PHP | ftp_delete() function
    The ftp_delete() function is an inbuilt function in PHP which is used to delete a file on the FTP server. Syntax: ftp_delete( $ftp_connection, $file ) Parameters: This function accepts two parameters as mentioned above and described below: $ftp_connection: It is required parameter. It specifies the
    2 min read
  • PHP | ftp_chdir() function
    The ftp_chdir() function is an inbuilt function in PHP which is used to change the current directory on the FTP server. Syntax: ftp_chdir( $ftp_connection, $directory ) Parameter: This function accepts two parameters as mentioned above and described below: $ftp_connection: It is required parameter.
    2 min read
  • PHP mysqli_connect() Function
    The mysqli_connect() function in PHP is a fundamental tool for establishing a connection to a MySQL database. This function is crucial for PHP applications that need to interact with MySQL databases, enabling them to execute queries, retrieve data, and perform various database operations. In this ar
    3 min read
  • PHP finfo_close() Function
    The finfo_close() function is an inbuilt function in PHP that is used to close the file instance that is opened by the finfo_open() function. Syntax: finfo_close($finfo): boolParameters: This function accepts only one parameter which is described below. $finfo: The file info resource returned by fin
    2 min read
  • PHP fclose() Function
    The fclose() function in PHP is an inbuilt function that is used to close a file that is pointed by an open file pointer. The fclose() function returns true on success and false on failure. It takes the file as an argument that has to be closed and closes that file. Syntax: bool fclose( $file )Param
    2 min read
  • PHP | connection_status() Function
    The connection_status() function is an inbuilt function in PHP which returns the current connection status. Syntax: int connection_status( void ) Parameters: This function doesn't accept any parameters. Return Value: This function returns the connection status bitfield. The possible values of return
    2 min read
  • PHP | chroot( ) Function
    The chroot() function in PHP is an inbuilt function which is used to change the root directory of the current process to directory. The chroot() function changes the current working directory to "/". The chroot() function is only available to GNU and BSD systems, and only when the user is using the
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences