note 87794 added to function.ftp-chmod

From: Date: Mon, 22 Dec 2008 05:26:20 +0000
Subject: note 87794 added to function.ftp-chmod
Groups: php.notes 
Request: Send a blank email to php-notes+get-148446@lists.php.net to get a copy of this message
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(). ---- Server IP: 64.71.164.2 Probable Submitter: 68.35.91.151 ---- Manual Page -- http://www.php.net/manual/en/function.ftp-chmod.php Edit -- https://master.php.net/note/edit/87794 Del: integrated -- https://master.php.net/note/delete/87794/integrated Del: useless -- https://master.php.net/note/delete/87794/useless Del: bad code -- https://master.php.net/note/delete/87794/bad+code Del: spam -- https://master.php.net/note/delete/87794/spam Del: non-english -- https://master.php.net/note/delete/87794/non-english Del: in docs -- https://master.php.net/note/delete/87794/in+docs Del: other reasons-- https://master.php.net/note/delete/87794 Reject -- https://master.php.net/note/reject/87794 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#148446) next »