[PHP] FTP Functions, upload file, delete file, etc.. - PHP / MySQL Tutorials
 
  Buy Ebooks, Video Games, DVDs, Electronics and more !
Home | Help Forums | Web Design Tutorials | Free Swishmax Downloads Make Money Online Blog . Free Image and Photo Sharing and Hosting

 

 

Company

Home

Nework Details

Tutorials

SWiSH 2

SWiSHMax

Flash MX

PHP & MySQL

HTML / JScript

Adobe Photoshop

Corel Draw

Gimp

Miscellaneous

Downloads

Templates

Plugins

Fonts

Wallpapers

Free Images

Scripts and Codes

Products

E-Books

SWiSH BB

SWiSH Templates

SWiSH-DB Newsletter
Subscribe to our newsletter : GO
  Partners / Affiliates

   Free image hosting
   Web Design Pakistan
   Swish Templates
   SWiSH Climax
   Talkfreelance Community
   Template Desire
   Lite boards
   Script Sector
   GimpTalk
   13dots
   Idea Designs
   PHP Cafe
   Sposatoettore
   Free swishmax templates
   Swishzone

Manage your site with Flax Article Content Management System
[PHP] FTP Functions, upload file, delete file, etc..
Category : PHP / MySQL | Level : Beginner | Language : English
Ask Question | Read Comments | Post tutorial | Previous | Next

Web www.swish-db.com

We can use php functions to do various ftp tasks on our server much like the tasks we do using an actual ftp program. Some of the functions include uploading files, downloading files and deleting files. These functions can sometimes save a user quite a bit of coding but can also cause some security risks.

In order to use these functions, we need to establish a connection to the server server. This is accomplished by using the ftp_connect() and ftp_login() functions. These are required at the beginning of your script or else we cannot do anything.

ftp_connect – Establishes a connection to the ftp server.
ftp_login – Logs a user into the ftp server.
ftp_close – Closes the connection with the ftp server.

Here is an example script using each of the above:

CODE

<?php

// set up the settings
$ftp_server = 'something.net';
$ftpuser = 'username';
$ftppass = 'pass';

// set up basic connection
$conn_id = ftp_connect($ftp_server);

// login with username and password
$login_result = ftp_login($conn_id, $ftpuser, $ftppass);

// close the connection
ftp_close($conn_id);


?>


This code doesn’t really do anything excepts establishes a connection with the server, logs the user in and then terminates the connection.

Here is a list of some very useful functions:

ftp_pwd – Returns current directory name
ftp_cdup – Changes to the parent directory
ftp_chdir – Changes directories on a FTP server
ftp_mkdir – Creates a directory
ftp_rmdir – Removes a directory
ftp_nlist – Lists the files within a given directory
ftp_rawlist – Returns a detailed list of files within a given directory
ftp_get – Downloads a file from the FTP server
ftp_put – Uploads a file to the FTP server
ftp_size – Returns the size of a given file
ftp_rename – Renames a file
ftp_delete – Deletes a file on the server
ftp_quit – Terminates the connection

To upload a file simply use:

CODE


<?php

// set up the settings
$ftp_server = 'something.net';
$ftpuser = 'username';
$ftppass = 'pass';

// delete the below variables if you decide to use a form.
$source_file = 'blah blah/blah.txt';
$destination_file = 'blah blah/blah.txt';

// set up basic connection
$conn_id = ftp_connect($ftp_server);

// login with username and password
$login_result = ftp_login($conn_id, $ftpuser, $ftppass);

ftp_put($conn_id, “$destination_file”, “$source_file”, FTP_BINARY); // the ftp function

// close the connection
ftp_close($conn_id);

?>



You can create a form with 2 input boxes named destination_file and source_file and have the submit button submit it to the above file which would allow you to create a quick and dirty upload form.

Below is an example of how you can delete a file:

CODE


<?php

// set up the settings
$file = 'somefile.txt';

$ftp_server = 'something.net';
$ftpuser = 'username';
$ftppass = 'pass';

// set up basic connection
$conn_id = ftp_connect($ftp_server);

// login with username and password
$login_result = ftp_login($conn_id, $ftpuser, $ftppass);

// try to delete $file
if (ftp_delete($conn_id, $file)) { // the ftp function
echo "$file deleted successful\n";
} else {
echo "could not delete $file\n";
}

// close the connection
ftp_close($conn_id);

?>



These examples should give you a basic idea of how to use the various ftp functions.

Please feel free to submit any errors...

Happy coding! smile.gif


© 2002-2005 Flaxweb Network | Article Manager | Gimp Tutorials Library | RAD C++ Library | Free C/C++ Sourcecode |
Sites of interest : Web Design Blog | Swish Templates | Swishmax Ebook | Photoshop Templates | Gimp Tutorials | Text Forum | Make Money Online