ב"ה
מכיוון שבהרבה שרתים הפוקנציה unlink עושה בעיות ועם הפוקנציה chmod אי אפשר לתת הרשאת 777 והפונקציות FTP של PHP לא כלולות במנוע אז כתבתי את המחלקה הזו:
PHP קוד:
<?php
class ftp {
function connect($server,$username,$password,$port=21,$timeout=15)
{
if($this->connection=fsockopen($server,$port,$errno,$errstr,$timeout))
{
fgets($this->connection);
fwrite($this->connection,"USER ".$username."\r\n");
fgets($this->connection);
fwrite($this->connection,"PASS ".$password."\r\n");
if(substr(fgets($this->connection),0,3)=='230')
{
return true;
}
else
{
$this->error="Login incorrect";
return false;
}
}
else
{
$this->error=$errstr;
return false;
}
}
function deletef($filepath)
{
fwrite($this->connection,"DELE ".$filepath."\r\n");
$response=fgets($this->connection);
if(substr($response,0,3)=='230')
return true;
else
{
$this->error=$response;
return false;
}
}
function removedir($dirpath)
{
fwrite($this->connection,"RMD ".$dirpath."\r\n");
$response=fgets($this->connection);
if(substr($response,0,3)=='250')
return true;
else
{
$this->error=$response;
return false;
}
}
function chmod($path,$prem)
{
fwrite($this->connection,"SITE CHMOD ".$prem." ".$path."\r\n");
$response=fgets($this->connection);
if(substr($response,0,3)=='200')
return true;
else
{
$this->error=$response;
return false;
}
}
function disconnect()
{
return fclose($this->connection);
}
}
?>
תיעוד:
הפונקציה connect מקבלת את כתובת השרת,שם המשתמש והסיסמא ומחזירה true אם חיבור נוצר וfalse אם לא והמשתנה
מכיל את השגיאה.
דוגמא לחיבור:
PHP קוד:
$ftp=new ftp();
$ftp->connect("hosts.co.il","admin","XXXX");
הפונקציה deletef מוחקת קובץ. היא מקבלת את הנתיב של הקובץ בFTP.
דוגמא:

הנתיב לקובץ dir.php יהיה
קוד:
/public_html/dir.php
ומוחקים אותו ככה:
PHP קוד:
ftp->deletef("/public_html/dir.php");
אם הקובץ נמחק הפוקנציה תחזיר true, אם לא היא תחזיר false והמשתנה$ftp->error יכיל את השגיאה.
הפונקציה removedir מוחקת תקייה והיא מקבלת את נתיב התקייה (כמו בדוגמא של המחיקת קובץ) ומחזירה true אם התקייה נמחקת וfalse אם יש שגיאה והמשתנה $ftp->error יכיל את השגיאה.
דוגמא:
PHP קוד:
$ftp->removedir("/public_html/");
הפונציה chmod משנה את ההרשאה לקובץ/תקייה. היא מקבלת את נתיב הקובץ/תקייה ואת מספר ההרשאה שרוצים לתת לקובץ/תקייה.
דוגמא:
PHP קוד:
$ftp->chmod("/public_html/dir.php","777");
ולסיום
הפונקציה disconnect היא סוגרת את החיבור עם השרת FTP (לא חובה להשתמש בה)
דוגמא לשימוש:
PHP קוד:
$ftp->disconnect();
- - - - - - - - - - - - - - - - - - - - - - - - -
אני מקווה בעתיד להוסיף עוד פונקציות שימושיות. אם יש לכן רעיונות לשיפור והוספות אז בבקשה תכתבו.