From f6d9a7cff222f868312a6d4ae4e4050616acb9a7 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Thu, 8 Nov 2012 17:27:57 +0200 Subject: Polish docs for the File and Form helpers --- system/helpers/file_helper.php | 34 +- user_guide_src/source/helpers/file_helper.rst | 178 +++++++--- user_guide_src/source/helpers/form_helper.rst | 494 +++++++++++++++++--------- 3 files changed, 480 insertions(+), 226 deletions(-) diff --git a/system/helpers/file_helper.php b/system/helpers/file_helper.php index 6383007ba..aebb6e47a 100644 --- a/system/helpers/file_helper.php +++ b/system/helpers/file_helper.php @@ -66,12 +66,12 @@ if ( ! function_exists('write_file')) * Writes data to the file specified in the path. * Creates a new file if non-existent. * - * @param string path to file - * @param string file data - * @param int + * @param string $path File path + * @param string $data Data to write + * @param string $mode fopen() mode (default: 'wb') * @return bool */ - function write_file($path, $data, $mode = FOPEN_WRITE_CREATE_DESTRUCTIVE) + function write_file($path, $data, $mode = 'wb') { if ( ! $fp = @fopen($path, $mode)) { @@ -99,13 +99,13 @@ if ( ! function_exists('delete_files')) * If the second parameter is set to TRUE, any directories contained * within the supplied base directory will be nuked as well. * - * @param string path to file - * @param bool whether to delete any directories found in the path - * @param int - * @param bool whether to skip deleting .htaccess and index page files + * @param string $path File path + * @param bool $del_dir Whether to delete any directories found in the path + * @param bool $htdocs Whether to skip deleting .htaccess and index page files + * @param int $_level Current directory depth level (default: 0; internal use only) * @return bool */ - function delete_files($path, $del_dir = FALSE, $level = 0, $htdocs = FALSE) + function delete_files($path, $del_dir = FALSE, $htdocs = FALSE, $_level = 0) { // Trim the trailing slash $path = rtrim($path, '/\\'); @@ -121,7 +121,7 @@ if ( ! function_exists('delete_files')) { if (is_dir($path.DIRECTORY_SEPARATOR.$filename) && $filename[0] !== '.') { - delete_files($path.DIRECTORY_SEPARATOR.$filename, $del_dir, $level + 1, $htdocs); + delete_files($path.DIRECTORY_SEPARATOR.$filename, $del_dir, $htdocs, $_level + 1); } elseif ($htdocs !== TRUE OR ! preg_match('/^(\.htaccess|index\.(html|htm|php)|web\.config)$/i', $filename)) { @@ -131,7 +131,7 @@ if ( ! function_exists('delete_files')) } @closedir($current_dir); - if ($del_dir === TRUE && $level > 0) + if ($del_dir === TRUE && $_level > 0) { return @rmdir($path); } @@ -319,12 +319,12 @@ if ( ! function_exists('get_mime_by_extension')) * Note: this is NOT an accurate way of determining file mime types, and is here strictly as a convenience * It should NOT be trusted, and should certainly NOT be used for security * - * @param string path to file - * @return mixed + * @param string $filename File name + * @return string */ - function get_mime_by_extension($file) + function get_mime_by_extension($filename) { - $extension = strtolower(substr(strrchr($file, '.'), 1)); + $extension = strtolower(substr(strrchr($filename, '.'), 1)); static $mimes; @@ -359,7 +359,7 @@ if ( ! function_exists('symbolic_permissions')) * Takes a numeric value representing a file's permissions and returns * standard symbolic notation representing that value * - * @param int + * @param int $perms Permissions * @return string */ function symbolic_permissions($perms) @@ -426,7 +426,7 @@ if ( ! function_exists('octal_permissions')) * Takes a numeric value representing a file's permissions and returns * a three character string representing the file's octal permissions * - * @param int + * @param int $perms Permissions * @return string */ function octal_permissions($perms) diff --git a/user_guide_src/source/helpers/file_helper.rst b/user_guide_src/source/helpers/file_helper.rst index 60c5aa98c..194d4348f 100644 --- a/user_guide_src/source/helpers/file_helper.rst +++ b/user_guide_src/source/helpers/file_helper.rst @@ -9,20 +9,23 @@ The File Helper file contains functions that assist in working with files. Loading this Helper =================== -This helper is loaded using the following code - -:: +This helper is loaded using the following code:: $this->load->helper('file'); The following functions are available: -read_file('path') -================= +read_file() +=========== -Returns the data contained in the file specified in the path. Example +.. php:function:: read_file($file) -:: + :param string $file: File path + :returns: string or FALSE on failure + +Returns the data contained in the file specified in the path. + +Example:: $string = read_file('./path/to/file.php'); @@ -35,14 +38,24 @@ The path can be a relative or full server path. Returns FALSE (boolean) on failu .. note:: This function is DEPRECATED. Use the native ``file_get_contents()`` instead. -If your server is running an `open_basedir` restriction this function might not work if you are trying to access a file above the calling script. +.. important:: If your server is running an **open_basedir** restriction this + function might not work if you are trying to access a file above the + calling script. -write_file('path', $data) -========================= +write_file() +============ -Writes data to the file specified in the path. If the file does not exist the function will create it. Example +.. php:function:: write_file($path, $data, $mode = 'wb') -:: + :param string $path: File path + :param string $data: Data to write to file + :param string $mode: ``fopen()`` mode + :returns: bool + +Writes data to the file specified in the path. If the file does not exist then the +function will create it. + +Example:: $data = 'Some file data'; if ( ! write_file('./path/to/file.php', $data)) @@ -54,85 +67,150 @@ Writes data to the file specified in the path. If the file does not exist the fu echo 'File written!'; } -You can optionally set the write mode via the third parameter - -:: +You can optionally set the write mode via the third parameter:: write_file('./path/to/file.php', $data, 'r+'); -The default mode is wb. Please see the `PHP user guide `_ for mode options. +The default mode is 'wb'. Please see the `PHP user guide `_ +for mode options. -Note: In order for this function to write data to a file its file permissions must be set such that it is writable (666, 777, etc.). If the file does not already exist, the directory containing it must be writable. +.. note: In order for this function to write data to a file, its permissions must + be set such that it is writable (666, 777, etc.). If the file does not + already exist, the directory containing it must be writable. .. note:: The path is relative to your main site index.php file, NOT your controller or view files. CodeIgniter uses a front controller so paths are always relative to the main site index. -delete_files('path') -==================== +.. note:: This function acquires an exclusive lock on the file while writing to it. -Deletes ALL files contained in the supplied path. Example +delete_files() +============== -:: +.. php:function:: delete_files($path, $del_dir = FALSE, $htdocs = FALSE) + + :param string $path: Directory path + :param bool $del_dir: Whether to also delete directories + :param bool $htdocs: Whether to skip deleting .htaccess and index page files + :returns: bool + +Deletes ALL files contained in the supplied path. + +Example:: delete_files('./path/to/directory/'); -If the second parameter is set to true, any directories contained within the supplied root path will be deleted as well. Example +If the second parameter is set to TRUE, any directories contained within the supplied +root path will be deleted as well. -:: +Example:: delete_files('./path/to/directory/', TRUE); .. note:: The files must be writable or owned by the system in order to be deleted. -get_filenames('path/to/directory/') -=================================== +get_filenames() +=============== + +.. php:function:: get_filenames($source_dir, $include_path = FALSE) + + :param string $source_dir: Directory path + :param bool $include_path: Whether to include the path as part of the filenames + :returns: array -Takes a server path as input and returns an array containing the names of all files contained within it. The file path can optionally be added to the file names by setting the second parameter to TRUE. +Takes a server path as input and returns an array containing the names of all files +contained within it. The file path can optionally be added to the file names by setting +the second parameter to TRUE. -get_dir_file_info('path/to/directory/', $top_level_only = TRUE) -=============================================================== +Example:: -Reads the specified directory and builds an array containing the filenames, filesize, dates, and permissions. Sub-folders contained within the specified path are only read if forced by sending the second parameter, $top_level_only to FALSE, as this can be an intensive operation. + $controllers = get_filenames(APPPATH.'controllers/'); -get_file_info('path/to/file', $file_information) -================================================ +get_dir_file_info() +=================== + +.. php:function:: get_dir_file_info($source_dir, $top_level_only) + + :param string $source_dir: Directory path + :param bool $top_level_only: Whether to look only at the specified directory + (excluding sub-directories) + :returns: array + +Reads the specified directory and builds an array containing the filenames, filesize, +dates, and permissions. Sub-folders contained within the specified path are only read +if forced by sending the second parameter to FALSE, as this can be an intensive +operation. + +Example:: + + $models_info = get_dir_file_info(APPPATH.'models/'); + +get_file_info() +=============== + +.. php:function: get_file_info($file, $returned_values = array('name', 'server_path', 'size', 'date')) + + :param string $file: File path + :param array $returned_values: What type of info to return + :returns: array or FALSE on failure -Given a file and path, returns the name, path, size, date modified. Second parameter allows you to explicitly declare what information you want returned; options are: `name`, `server_path`, `size`, `date`, `readable`, `writable`, `executable`, `fileperms`. Returns FALSE if the file cannot be found. +Given a file and path, returns (optionally) the *name*, *path*, *size* and *date modified* +information attributes for a file. Second parameter allows you to explicitly declare what +information you want returned. -.. note:: The "writable" uses the PHP function is_writable() which is known - to have issues on the IIS webserver. Consider using fileperms instead, - which returns information from PHP's fileperms() function. +Valid ``$returned_values`` options are: `name`, `size`, `date`, `readable`, `writeable`, +`executable` and `fileperms`. -get_mime_by_extension('file') -============================= +.. note:: The *writable* attribute is checked via PHP's ``is_writeable()`` function, which + known to have issues on the IIS webserver. Consider using *fileperms* instead, + which returns information from PHP's ``fileperms()`` function. -Translates a file extension into a mime type based on config/mimes.php. Returns FALSE if it can't determine the type, or open the mime config file. +get_mime_by_extension() +======================= + +.. php:function:: get_mime_by_extension($filename) + + :param string $filename: File name + :returns: string or FALSE on failure + +Translates a filename extension into a MIME type based on *config/mimes.php*. +Returns FALSE if it can't determine the type, or read the MIME config file. :: - $file = "somefile.png"; - echo $file . ' is has a mime type of ' . get_mime_by_extension($file); + $file = 'somefile.png'; + echo $file.' is has a mime type of '.get_mime_by_extension($file); + +.. note:: This is not an accurate way of determining file MIME types, and + is here strictly for convenience. It should not be used for security + purposes. +symbolic_permissions() +====================== -.. note:: This is not an accurate way of determining file mime types, and - is here strictly as a convenience. It should not be used for security. +.. php:function:: symbolic_permissions($perms) -symbolic_permissions($perms) -============================ + :param int $perms: Permissions + :returns: string -Takes numeric permissions (such as is returned by `fileperms()` and returns standard symbolic notation of file permissions. +Takes numeric permissions (such as is returned by ``fileperms()``) and returns +standard symbolic notation of file permissions. :: echo symbolic_permissions(fileperms('./index.php')); // -rw-r--r-- -octal_permissions($perms) -========================= +octal_permissions() +=================== -Takes numeric permissions (such as is returned by fileperms() and returns a three character octal notation of file permissions. +.. php:function:: octal_permissions($perms) -:: + :param int $perms: Permissions + :returns: string - echo octal_permissions(fileperms('./index.php')); // 644 +Takes numeric permissions (such as is returned by ``fileperms()``) and returns +a three character octal notation of file permissions. + +:: + echo octal_permissions(fileperms('./index.php')); // 644 \ No newline at end of file diff --git a/user_guide_src/source/helpers/form_helper.rst b/user_guide_src/source/helpers/form_helper.rst index 02a758694..b2a9b6f0f 100644 --- a/user_guide_src/source/helpers/form_helper.rst +++ b/user_guide_src/source/helpers/form_helper.rst @@ -10,9 +10,7 @@ forms. Loading this Helper =================== -This helper is loaded using the following code - -:: +This helper is loaded using the following code:: $this->load->helper('form'); @@ -21,19 +19,27 @@ The following functions are available: form_open() =========== -Creates an opening form tag with a base URL **built from your config preferences**. It will optionally let you add form attributes and hidden input fields, and will always add the attribute accept-charset based on the charset value in your config file. +.. php:function:: form_open($action = '', $attributes = '', $hidden = array()) -The main benefit of using this tag rather than hard coding your own HTML is that it permits your site to be more portable in the event your URLs ever change. + :param string $action: Form action/target URI string + :param string $attributes: HTML attributes + :param array $hidden: An array of hidden fields' definitions + :returns: string -Here's a simple example +Creates an opening form tag with a base URL **built from your config preferences**. +It will optionally let you add form attributes and hidden input fields, and +will always add the `accept-charset` attribute based on the charset value in your +config file. -:: +The main benefit of using this tag rather than hard coding your own HTML is that +it permits your site to be more portable in the event your URLs ever change. + +Here's a simple example:: echo form_open('email/send'); -The above example would create a form that points to your base URL plus the "email/send" URI segments, like this - -:: +The above example would create a form that points to your base URL plus the +"email/send" URI segments, like this::
@@ -41,32 +47,25 @@ Adding Attributes ^^^^^^^^^^^^^^^^^ Attributes can be added by passing an associative array to the second -parameter, like this - -:: +parameter, like this:: $attributes = array('class' => 'email', 'id' => 'myform'); echo form_open('email/send', $attributes); -The above example would create a form similar to this - -:: +The above example would create a form similar to this:: Adding Hidden Input Fields ^^^^^^^^^^^^^^^^^^^^^^^^^^ -Hidden fields can be added by passing an associative array to the third parameter, like this - -:: +Hidden fields can be added by passing an associative array to the +third parameter, like this:: - $hidden = array('username' => 'Joe', 'member_id' => '234'); + $hidden = array('username' => 'Joe', 'member_id' => '234'); echo form_open('email/send', '', $hidden); -The above example would create a form similar to this - -:: +The above example would create a form similar to this:: @@ -75,29 +74,38 @@ The above example would create a form similar to this form_open_multipart() ===================== -This function is absolutely identical to the `form_open()` tag above -except that it adds a multipart attribute, which is necessary if you +.. php:function:: form_open_multipart($action = '', $attributes = array(), $hidden = array()) + + :param string $action: Form action/target URI string + :param string $attributes: HTML attributes + :param array $hidden: An array of hidden fields' definitions + :returns: string + +This function is absolutely identical to :php:func:`form_open()` above, +except that it adds a *multipart* attribute, which is necessary if you would like to use the form to upload files with. form_hidden() ============= -Lets you generate hidden input fields. You can either submit a -name/value string to create one field +.. php:function:: form_hidden($name, $value = '') -:: + :param string $name: Field name + :param string $value: Field value + :returns: string + +Lets you generate hidden input fields. You can either submit a +name/value string to create one field:: form_hidden('username', 'johndoe'); // Would produce: -Or you can submit an associative array to create multiple fields - -:: +... or you can submit an associative array to create multiple fields:: $data = array( - 'name'  => 'John Doe', - 'email' => 'john@example.com', - 'url'   => 'http://example.com' + 'name' => 'John Doe', + 'email' => 'john@example.com', + 'url' => 'http://example.com' ); echo form_hidden($data); @@ -109,35 +117,32 @@ Or you can submit an associative array to create multiple fields */ -Or pass an associative array to the value field. - -:: +You can also pass an associative array to the value field:: $data = array( - 'name'  => 'John Doe', - 'email' => 'john@example.com', - 'url'   => 'http://example.com' + 'name' => 'John Doe', + 'email' => 'john@example.com', + 'url' => 'http://example.com' ); echo form_hidden('my_array', $data); /* Would produce: + */ -If you want to create hidden input fields with extra attributes - -:: +If you want to create hidden input fields with extra attributes:: $data = array( - 'type'        => 'hidden', - 'name'        => 'email', - 'id'          => 'hiddenemail', - 'value'       => 'john@example.com', - 'class'       => 'hiddenemail' + 'type' => 'hidden', + 'name' => 'email', + 'id' => 'hiddenemail', + 'value' => 'john@example.com', + 'class' => 'hiddenemail' ); echo form_input($data); @@ -151,25 +156,28 @@ If you want to create hidden input fields with extra attributes form_input() ============ -Lets you generate a standard text input field. You can minimally pass -the field name and value in the first and second parameter +.. php:function:: form_input($data = '', $value = '', $extra = '') -:: + :param array $data: Field attributes data + :param string $value: Field value + :param string $extra: Extra attributes to be added to the tag *as is* + :returns: string + +Lets you generate a standard text input field. You can minimally pass +the field name and value in the first and second parameter:: echo form_input('username', 'johndoe'); Or you can pass an associative array containing any data you wish your -form to contain - -:: +form to contain:: $data = array( - 'name'        => 'username', - 'id'          => 'username', - 'value'       => 'johndoe', - 'maxlength'   => '100', - 'size'        => '50', - 'style'       => 'width:50%' + 'name' => 'username', + 'id' => 'username', + 'value' => 'johndoe', + 'maxlength' => '100', + 'size' => '50', + 'style' => 'width:50%' ); echo form_input($data); @@ -181,9 +189,7 @@ form to contain */ If you would like your form to contain some additional data, like -Javascript, you can pass it as a string in the third parameter - -:: +JavaScript, you can pass it as a string in the third parameter:: $js = 'onClick="some_function()"'; echo form_input('username', 'johndoe', $js); @@ -191,34 +197,70 @@ Javascript, you can pass it as a string in the third parameter form_password() =============== -This function is identical in all respects to the `form_input()` function above except that it uses the "password" input type. +.. php:function:: form_password($data = '', $value = '', $extra = '') + + :param array $data: Field attributes data + :param string $value: Field value + :param string $extra: Extra attributes to be added to the tag *as is* + :returns: string + +This function is identical in all respects to the :php:func:`form_input()` +function above except that it uses the "password" input type. form_upload() ============= -This function is identical in all respects to the `form_input()` function above except that it uses the "file" input type, allowing it to be used to upload files. +.. php:function:: form_upload($data = '', $value = '', $extra = '') + + :param array $data: Field attributes data + :param string $value: Field value + :param string $extra: Extra attributes to be added to the tag *as is* + :returns: string + +This function is identical in all respects to the :php:func:`form_input()` +function above except that it uses the "file" input type, allowing it to +be used to upload files. form_textarea() =============== -This function is identical in all respects to the `form_input()` function above except that it generates a "textarea" type. Note: Instead of the "maxlength" and "size" attributes in the above example, you will instead specify "rows" and "cols". +.. php:function:: form_textarea($data = '', $value = '', $extra = '') + + :param array $data: Field attributes data + :param string $value: Field value + :param string $extra: Extra attributes to be added to the tag *as is* + :returns: string + +This function is identical in all respects to the :php:func:`form_input()` +function above except that it generates a "textarea" type. + +.. note: Instead of the *maxlength* and *size* attributes in the above example, + you will instead specify *rows* and *cols*. form_dropdown() =============== +.. php:function:: form_dropdown($name = '', $options = array(), $selected = array(), $extra = '') + + :param string $name: Field name + :param array $options: An associative array of options to be listed + :param array $selected: List of fields to mark with the *selected* attribute + :param string $extra: Extra attributes to be added to the tag *as is* + :returns: string + Lets you create a standard drop-down field. The first parameter will contain the name of the field, the second parameter will contain an associative array of options, and the third parameter will contain the value you wish to be selected. You can also pass an array of multiple items through the third parameter, and CodeIgniter will create a -multiple select for you. Example +multiple select for you. -:: +Example:: $options = array( - 'small'  => 'Small Shirt', - 'med'    => 'Medium Shirt', - 'large'   => 'Large Shirt', + 'small' => 'Small Shirt', + 'med' => 'Medium Shirt', + 'large' => 'Large Shirt', 'xlarge' => 'Extra Large Shirt', ); @@ -251,33 +293,47 @@ multiple select for you. Example If you would like the opening @@ -346,21 +412,19 @@ array of attributes to the function :: $data = array( - 'name'        => 'newsletter', - 'id'          => 'newsletter', - 'value'       => 'accept', - 'checked'     => TRUE, - 'style'       => 'margin:10px', + 'name' => 'newsletter', + 'id'      => 'newsletter', + 'value'   => 'accept', + 'checked' => TRUE, + 'style'   => 'margin:10px' ); echo form_checkbox($data); // Would produce: -As with other functions, if you would like the tag to contain additional -data, like JavaScript, you can pass it as a string in the fourth -parameter - -:: +Also as with other functions, if you would like the tag to contain +additional data like JavaScript, you can pass it as a string in the +fourth parameter:: $js = 'onClick="some_function()"'; echo form_checkbox('newsletter', 'accept', TRUE, $js) @@ -368,29 +432,28 @@ parameter form_radio() ============ -This function is identical in all respects to the `form_checkbox()` -function above except that it uses the "radio" input type. - -form_submit() -============= - -Lets you generate a standard submit button. Simple example +.. php:function:: form_radio($data = '', $value = '', $checked = FALSE, $extra = '') -:: - - echo form_submit('mysubmit', 'Submit Post!'); - // Would produce: + :param array $data: Field attributes data + :param string $value: Field value + :param bool $checked: Whether to mark the radio button as being *checked* + :param string $extra: Extra attributes to be added to the tag *as is* + :returns: string -Similar to other functions, you can submit an associative array in the -first parameter if you prefer to set your own attributes. The third -parameter lets you add extra data to your form, like JavaScript. +This function is identical in all respects to the :php:func:`form_checkbox()` +function above except that it uses the "radio" input type. form_label() ============ -Lets you generate a