Loading of files on FTP
Any manager of a site wants, that visitors on his{its} site would be more, and the traffic which they eat, they open free-of-charge ftp were small. For this purpose accounts which allow to load and download free-of-charge files of the small size (as a rule, no more than 10 MB). In this case there is a problem of automatic loading of files on ftp. In given clause{article} I shall show to you as with the help of small function PHP maximum to facilitate to you the given problem{task}.
Let's create the small form of loading of a file:
<form action=upl.php method=post enctype = "multipart/form-data" name=Form>
<input type=file name=filename>
<input type=submit value ='Zagruzit` '>
</form>
Pay attention, that upl.php, this name of a file where the data which should contain function of processing and loading of a file are passed. Also at the description of the form it is necessary to specify a line
enctype = "multipart/form-data"
Which allows to load any data. If at you the file is not loaded on ftp, first of all check up, whether you have added this property.
Code of a file upl.php:
<? php
function upload ($filename)
{
$login = "vash_login";
$pass = "vash_parol`";
$host = "imja_ftp";
$path = "put`_k_papke";
$tmp = $ _FILES [$filename] ['tmp_name'];
$aname = $ _FILES [$filename] ['name'];
$connect = ftp_connect ($host);
if (! $connect) exit ();
$result = ftp_login ($connect, $login, $pass);
if ($result == false) exit ();
if (ftp_chdir ($connect, $path)) ftp_put ($connect, $aname, $tmp, FTP_BINARY);
else exit ();
ftp_quit ($connect);
unlink ($tmp);
};
if (isset ($filename)) upload ('filename')
?>
$login and $pass - a login and the password of yours ftp an account
$host - a name of yours ftp (for example, ftp.microsoft.com)
$path - an internal way to a folder where it is necessary to load a file (rights for the given folder necessarily should stand on recording). For example/alex/download/upload/.
$tmp = $ _FILES [$filename] ['tmp_name']
We determine a time name of a loaded file. Here it is necessary to explain a principle of loading of a file. After you have pressed button " Loading ", the file has been placed in a time directory on your server (usually a folder tmp). Only after that we can work with the given file and send it{him} where it is necessary. Contents of a folder tmp are usual is destroyed automatically but what to be reinsured, at the end of all operations it is necessary to remove a file itself
unlink ($tmp)
Now it is necessary to incorporate with ftp:
$connect = ftp_connect ($host);
if (! $connect) exit ();
$result = ftp_login ($connect, $login, $pass);
if ($result == false) exit ();
At once we check, if connection is absent ($connect=false) or the incorrect data of a login and-or the password ($result=false) that are specified stop performance of function.
Further by means of function ftp_chdir we pass in a folder $path and we load a file with the help of function ftp_put:
if (ftp_chdir ($connect, $path)) ftp_put ($connect, $aname, $tmp,
FTP_BINARY);
else exit ()
The file is loaded, now it is necessary to be disconnected from ftp and to remove a temporary file:
ftp_quit ($connect);
unlink ($tmp)
Now we carry out the reference{manipulation} to function:
if (isset ($filename)) upload ('filename')
If the user has chosen any file ($filename not empty) the given file on ftp is loaded.

|