- Joomla! API
- Methods
- append
- Arguments
- Response
- Arguments
- Response
- delete
- Arguments
- Response
- exists
- Arguments
- Response
- getExt
- Arguments
- Response
- getName
- Arguments
- Response
- makeSafe
- Arguments
- Response
- Arguments
- Response
- Arguments
- Response
- stripExt
- Arguments
- Response
- upload
- Arguments
- How to use the filesystem package
- Материал из Joomla! Documentation
- Содержание
- Using the JFile:: class [ править ]
- Get the File Extension [ править ]
- Strip the File Extension [ править ]
- Clean the Filename [ править ]
- Copy a File [ править ]
- Delete a File [ править ]
- Upload a File [ править ]
- Example [ править ]
- Using the JFolder:: Class [ править ]
- Copy Folder [ править ]
- Create Folder [ править ]
- Move folder [ править ]
- Check If a Folder Exists [ править ]
- Read Files from a Folder [ править ]
- Read Folders from Filesystem [ править ]
- Make a Tree-Like List from a Folder Structure [ править ]
- Clean a Path String [ править ]
- Example [ править ]
- Joomla! API
- Methods
- __call
- Arguments
- Response
- __construct
- Arguments
- __get
- Arguments
- Response
- count
- Response
- decodeData
- Arguments
- Response
- Arguments
- exists
- Arguments
- Response
- Arguments
- Response
- getAlnum
- Arguments
- Response
- getArray
- Arguments
- Joomla! API
- Methods
- __call
- Arguments
- Response
- __construct
- Arguments
- __get
- Arguments
- Response
- count
- Response
- decodeData
- Arguments
- Response
- Arguments
- exists
- Arguments
- Response
- Arguments
- Response
- getAlnum
- Arguments
- Response
- getArray
- Arguments
- Response
- getArrayRecursive
- Arguments
- The Joomla! Forum™
- file_exists() doesnt’ work / include css file .php
- file_exists() doesnt’ work / include css file .php
- Re: include css file .php in main index
- Re: include css file .php in main index
- Re: file_exists() doesnt’ work / include css file .php
- Re: file_exists() doesnt’ work / include css file .php
Joomla! API
- Methods
- append
- copy
- delete
- exists
- getExt
- getName
- makeSafe
- move
- read
- stripExt
- upload
- write
- В» Protected
- В» Private
- Constants
A File handling class
Methods
append
Append contents to a file
Arguments
string The full file path
string The buffer to write
boolean Use streams
Response
boolean True on success
Arguments
string The path to the source file
string The path to the destination file
string An optional base path to prefix to the file names
boolean True to use streams
Response
boolean True on success
delete
Delete a file or array of files
Arguments
mixed The file name or an array of file names
Response
boolean True on success
exists
Wrapper for the standard file_exists function
Arguments
string File path
Response
boolean True if path is a file
getExt
Gets the extension of a file name
Arguments
string The file name
Response
string The file extension
getName
Returns the name, without any path.
4.0 — Use basename() instead.
Arguments
string File path
Response
makeSafe
Makes file name safe to use
Arguments
string The name of the file [not full path]
Response
string The sanitised string
Arguments
string The path to the source file
string The path to the destination file
string An optional base path to prefix to the file names
boolean True to use streams
Response
boolean True on success
Read the contents of a file
4.0 — Use the native file_get_contents() instead.
Arguments
string The full file path
boolean Use include path
integer Amount of file to read
integer Size of chunks to read
integer Offset of the file
Response
mixed Returns file contents or boolean False if failed
stripExt
Strips the last extension off of a file name
Arguments
string The file name
Response
string The file name without the extension
upload
Moves an uploaded file to a destination folder
Arguments
string The name of the php (temporary) uploaded file
string The path (including filename) to move the uploaded file to
boolean True to use streams
boolean Allow the upload of unsafe files
boolean Options to \JFilterInput::isSafeFile
How to use the filesystem package
Материал из Joomla! Documentation
Содержание
Using the JFile:: class [ править ]
There are 4 classes in the filesystem library.
- JFile:: (file.php)
- JFolder:: (folder.php)
- JPath:: (path.php)
- JArchive:: (archive.php)
This tutorial focuses on the JFile:: and JFolder:: classes.
The base for file handling is the JFile class, found in the /libraries/src/Filesystem/File.php file. Below you can see the most common options from this library file. At the end of this post you can find a very simple script for file upload.
Get the File Extension [ править ]
Pretty clear, just feed the function with a filename and it will return the extension of the file you selected.
Strip the File Extension [ править ]
This will return the filename without the extension.
Clean the Filename [ править ]
Use: It cleans out all odd characters from a filename and returns a safe filename.
Normally at the head of the file.
Copy a File [ править ]
It is basically a wrapper for the PHP copy() function, but also checks if the file you want to copy exists and the destination really is available. The great thing is that this function also makes use of the FTP-layer in J15 if necessary.
Delete a File [ править ]
It tries to delete the file, making sure it actually exists, but also checks permissions. If permissions are not set up properly, it tries to change them and delete the file. It also uses the FTP-layer when necessary.
Upload a File [ править ]
It is basically the wrapper for the PHP move_uploaded_file() function, but also checks availability and permissions on both source and destination path.
Example [ править ]
So how does that look in a script? Here’s a small code snippet of an upload script. Some of the functions are used. The script is fired from an upload form. That form has a file element, called file_upload (see below).
Note: If you didn’t add the part: enctype=»multipart/form-data» then you are unable to upload a file.
The upload code looks like this:
Using the JFolder:: Class [ править ]
The base for folder handling is the JFolder class, found in the /libraries/src/Filesystem/Folder.php file. Below I will discuss the most common options from this library file. At the end there is a simple example script.
Copy Folder [ править ]
This will copy a complete folder and all of its contents to another location on the server. It does permission and availability checking on both source and destination. By using $path you can enter a base path to prefix to the filename and by setting the $force parameter to true, you can force the overwriting of already existing files. If set in the configuration, the FTP-layer will be used.
Create Folder [ править ]
This is basically a wrapper for the PHP mkdir() function, but with error permissions and availability checking. This one also uses the FTP-layer when set in the configuration. $mode will set the default permission, once copied and defaults to 0755.
If any parent directory does not exist, it is created. If the directory to be created ($path) already exists, this function just returns a Boolean true.
Move folder [ править ]
Basically a wrapper for the PHP rename() function, but with permissions and availability checking. This one also uses the FTP-layer when set in the configuration.
Check If a Folder Exists [ править ]
Wrapper for the PHP is_dir() function. Returns true if the folder exists.
Read Files from a Folder [ править ]
A function to read files from a folder. When setting $recurse to true, subfolders will also be searched. $fullpath set to true returns the full path in the array. With $exclude, you can offer an array of extensions, not to include in the search for files. It returns an array with all filenames.
Read Folders from Filesystem [ править ]
The same as JFolder::files(), except this only returns an array with folder names.
Make a Tree-Like List from a Folder Structure [ править ]
It will read a folder, specified in $path and will return all folders in an array, suitable for tree display. You can specify the number of levels. The folder array looks like this:
Clean a Path String [ править ]
Similar to the JFile::makeSafe() function. It cleans all odd characters out of the string and returns a clean path.
Example [ править ]
How does this look in actual code? We are going to read the contents of a folder called images that holds a number of files. We want to create a subfolder, called jpg and filter all jpg files out and move them to the jpg subfolder. After that, we will move the complete subfolder to another place on the server.
Joomla! API
- Methods
- __call
- __construct
- __get
- count
- def
- exists
- get
- getAlnum
- getArray
- getBase64
- getBool
- getCmd
- getFloat
- getHtml
- getInputForRequestMethod
- getInt
- getMethod
- getPath
- getString
- getUint
- getUsername
- getWord
- serialize
- set
- unserialize
- В» Protected
- decodeData
- loadAllInputs
- В» Private
- Constants
Joomla! Input Files Class
This is an abstracted input class used to manage retrieving data from the application environment.
Methods
__call
Magic method to get filtered input data.
Arguments
string Name of the filter type prefixed with ‘get’.
array [0] The name of the variable [1] The default value.
Response
mixed The filtered input value.
__construct
Arguments
array Optional source data. If omitted, a copy of the server variable ‘_REQUEST’ is used.
array An optional associative array of configuration parameters: filter: An instance of Filter\Input. If omitted, a default filter is initialised.
__get
Magic method to get an input object
Arguments
mixed Name of the input object to retrieve.
Response
\Joomla\Input\Input The request input object
count
Get the number of variables.
Response
integer The number of variables in the input.
decodeData
Method to decode a data array.
Arguments
array The data array to decode.
Response
Define a value. The value will only be set if there’s no value for the name or if it is null.
Arguments
string Name of the value to define.
mixed Value to assign to the input.
exists
Check if a value name exists.
Arguments
string Value name
Response
Gets a value from the input data.
Arguments
string Name of the value to get.
mixed Default value to return if variable does not exist.
string Filter to apply to the value.
Response
mixed The filtered input value.
getAlnum
Get an alphanumeric string.
Arguments
Response
getArray
Gets an array of values from the request.
Arguments
array Associative array of keys and filter types to apply. If empty and datasource is null, all the input data will be returned but filtered using the default case in JFilterInput::clean.
Joomla! API
- Methods
- __call
- __construct
- __get
- count
- def
- exists
- get
- getAlnum
- getArray
- getBase64
- getBool
- getCmd
- getFloat
- getHtml
- getInputForRequestMethod
- getInt
- getMethod
- getPath
- getString
- getUint
- getUsername
- getWord
- serialize
- set
- unserialize
- В» Protected
- decodeData
- getArrayRecursive
- loadAllInputs
- В» Private
- Constants
Joomla! Input Files Class
This is an abstracted input class used to manage retrieving data from the application environment.
5.0 Use Joomla\Input\Files instead
Methods
__call
Magic method to get filtered input data.
Arguments
string Name of the filter type prefixed with ‘get’.
array [0] The name of the variable [1] The default value.
Response
mixed The filtered input value.
__construct
Arguments
array Optional source data. If omitted, a copy of the server variable ‘_REQUEST’ is used.
array An optional associative array of configuration parameters: filter: An instance of Filter\Input. If omitted, a default filter is initialised.
__get
Magic method to get an input object
Arguments
mixed Name of the input object to retrieve.
Response
\Joomla\Input\Input The request input object
count
Get the number of variables.
Response
integer The number of variables in the input.
decodeData
Method to decode a data array.
5.0 Use Joomla\Input\Files instead
Arguments
array The data array to decode.
Response
Define a value. The value will only be set if there’s no value for the name or if it is null.
Arguments
string Name of the value to define.
mixed Value to assign to the input.
exists
Check if a value name exists.
Arguments
string Value name
Response
Gets a value from the input data.
Arguments
string Name of the value to get.
mixed Default value to return if variable does not exist.
string Filter to apply to the value.
Response
mixed The filtered input value.
getAlnum
Get an alphanumeric string.
Arguments
Response
getArray
Gets an array of values from the request.
Arguments
array Associative array of keys and filter types to apply. If empty and datasource is null, all the input data will be returned but filtered using the default case in JFilterInput::clean.
mixed Array to retrieve data from, or null
Response
mixed The filtered input data.
getArrayRecursive
Gets an array of values from the request.
5.0 Use Joomla\Input\Input instead
Arguments
array Associative array of keys and filter types to apply. If empty and datasource is null, all the input data will be returned but filtered using the filter given by the parameter defaultFilter in JFilterInput::clean.
mixed Array to retrieve data from, or null.
string Default filter used in JFilterInput::clean if vars is empty and datasource is null. If ‘unknown’, the default case is used in JFilterInput::clean.
boolean Flag to indicate a recursive function call.
The Joomla! Forum™
file_exists() doesnt’ work / include css file .php
file_exists() doesnt’ work / include css file .php
Post by Irian » Wed Aug 19, 2009 10:50 pm
Re: include css file .php in main index
Post by ccauth » Thu Aug 20, 2009 4:43 am
Re: include css file .php in main index
Post by Irian » Fri Aug 21, 2009 1:08 pm
now I just included the file as if it was a .css file.
About the other question about why the file_exists() function doesnt work:
If I do it like this it works:
Re: file_exists() doesnt’ work / include css file .php
Post by betweenbrain » Tue Nov 24, 2009 2:01 am
I believe the problem has to do with relative addresses in context of current PHP dir. Take a look at http://www.php.net/manual/en/function.f . .php#93253.
I’m working on something similar and would be interested to see what you came up with. I’ll post some code when I get it working.
Re: file_exists() doesnt’ work / include css file .php
Post by kflorida78 » Tue May 25, 2010 4:23 pm
You can’t have absolute url’s when using this function.
For example, If you are checking if a file exists in a Joomla extension that will be displayed in your Administrator you have to use the relative path.
Let’s say you wrote a component that displays an image called «image1.jpg» located in your front-end images folder and you want to detect if the image exists in the Joomla Administrator section.
When you are looking at your component, lets say your url is: «http://www.mysite.com/administrator/ind . ycomponent»
So you need to get the relative path to your image from your current location. in this situation, it would be done like this:
The reason you use «DS» instead of «/» is so the file structure is written according to your platform. The above is the relative path from the admin component to the image. (this is the same as writing «../images/image1.jpg»)