PHP Doku:: Ändert die Zugriffsrechte einer Datei über FTP - function.ftp-chmod.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzSonstige DiensteFTPFTP-Funktionenftp_chmod

Ein Service von Reinhard Neidl - Webprogrammierung.

FTP-Funktionen

<<ftp_chdir

ftp_close>>

ftp_chmod

(PHP 5)

ftp_chmodÄndert die Zugriffsrechte einer Datei über FTP

Beschreibung

int ftp_chmod ( resource $ftp_stream , int $mode , string $filename )

Ändert die Zugriffsrechte der spezifierten Datei zu mode.

Parameter-Liste

ftp_stream

Der Verbindungshandler der FTP-Verbindung.

mode

Die neuen Zugriffsrechte als octal-Wert.

filename

Der Name der Datei auf dem FTP-Server.

Rückgabewerte

Gibt die neuen Zugriffsrechte oder im Fehlerfall FALSE zurück.

Beispiele

Beispiel #1 ftp_chmod()-Beispiel

<?php
$dateiname 
'public_html/index.php';

// einfacher Verbindungsaufbau
$conn_id ftp_connect($ftp_server);

// Login mit Benutzername und Passwort
$login_result ftp_login($conn_id$ftp_username$ftp_passwort);

// Zugriffsrechte von $dateiname auf 644 ändern
if (ftp_chmod($conn_id0644$dateiname) !== false) {
 echo 
"Zugriffsrechte der Datei $dateiname auf 644 geändert\n";
} else {
 echo 
"Änderung der Zugriffsrechte fehlgeschlagen\n";
}

// Verbindung schließen
ftp_close($conn_id);
?>

Siehe auch

  • chmod() - Ändert die Zugriffsrechte einer Datei


8 BenutzerBeiträge:
- Beiträge aktualisieren...
DanN
17.11.2009 8:17
Just wanted to contribute a quick note for those who are still experiencing issues with changing the permissions via FTP.

If you are having trouble with PHP recognizing the mode as an integer, you can take the previous poster's method:

$mode = octdec ( str_pad ( $mode, 4, '0', STR_PAD_LEFT ) );

And add the following snippet right after:

$mode = (int) $mode;

This will force PHP to recognize the mode as an integer when you do:

ftp_chmod ( $conn_id, $mode, $path );

These together never seem to fail for me.
Maarten Wolzak
23.09.2009 0:15
Using the excellent octdec and decoct functions you can make this easy:

<?php
$mode
= "644";
$mode = octdec( str_pad($mode,4,'0',STR_PAD_LEFT) );
ftp_chmod($ftp_stream, $mode, $file);
?>
rperea at vsourceweb dot com
22.12.2008 6:26
It took me a while to figure out how to use this function in my situation because I needed the $mode to be passed to this function as a variable that was read from a database. Since the database returns the value as an integer without a leading zero, I could not get the operation to work because adding a leading zero in PHP turns the value into a string.

For example, this does not work in my situation:

<?php
// Assume that this is the value returned from the database.
$mode = 644;

// Now try to chmod using this value.
ftp_chmod($conn_id, $mode, 'test.txt');

// The file now has permissions of 204 and not 644
?>

Adding a leading zero doesn't work either:

<?php
// Assume that this is the value returned from the database.
$mode = 644;

// Now try to chmod using this value.
ftp_chmod($conn_id, '0'.$mode, 'test.txt');

// The file now has permissions of 204 and not 644
?>

I tried many ways to get it to work even converting it from oct to dec using octdec and then back to decoct and nothing worked. This is the only way I was able to get it to work, with an eval statement.

<?php
// Assume that this is the value returned from the database.
$mode = 644;

// Turn the mode into a string
$np = '0'.$mode;

// Now run chmod with the eval'd string parsed as an integer.
ftp_chmod($conn_id, eval("return({$np});"), 'test.txt');

// The file now has permissions of 644
?>

Of course, you will have to make sure that the value of $mode only contains 3 digits. Always do checking on your values before handing it off to eval().
cspiegl at kwask dot de
21.04.2008 21:30
Ok,
so if 2 people say that my way is wrong and the other is right, i will take mine back.
I posted it cause for me just the way i used it worked (i don't know why)

AND: i would not say something like: "I would try before post", in my opinion that is realy unfriendly, cause i tryed!
tiefenbrunner at obdev dot at
17.12.2007 14:19
The "mode" parameter of the PHP5 ftp_chmod function is an integer value that is supposed to be given as an octal number, like the argument for the "chmod" command line tool.

Thus the sprintf must use the %o formatting character, so that the passed integer value is really represented as an octal number to the CHMOD site command for the FTP server.

So, IMHO, rabin's version is correct (it definitely worked for me).
Josh at jcr dot com
13.09.2007 19:13
rabin's code works just fine as a replacement for ftp_chmod().
I would try that before trying cspiegl's solution for pre-php 5 installations.
cspiegl at KwasK dot de
31.08.2007 22:36
Hi,
The commet with the "ftp_site" is not correct!
You must use it like this:

<?php
if (!function_exists('ftp_chmod')) {
    function
ftp_chmod($ftp_stream, $mode, $filename)
    {
        return
ftp_site($ftp_stream, sprintf('CHMOD %u %s', $mode, $filename));
    }
}
?>
rabin at rab dot in
23.05.2006 12:21
As mentioned in the note below, the function posted by "hardy add mapscene dot com" works incorrectly if used with an octal mode, the way the php5 function is used.

This function works exactly like the the php5 one:

<?php
if (!function_exists('ftp_chmod')) {
    function
ftp_chmod($ftp_stream, $mode, $filename)
    {
        return
ftp_site($ftp_stream, sprintf('CHMOD %o %s', $mode, $filename));
    }
}
?>



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)