Fatal error: Cannot instantiate non-existent class: directoryiterator - php

Need some help with this error. Fresh wordpress 2.9 install...
Fatal error: Cannot instantiate non-existent class: directoryiterator in /home1/test/public_html/test2/wp-content/themes/mytheme/functions.php on line 471
function get_dirs($path = '.') {
$dirs = array();
foreach (new DirectoryIterator($path) as $file) { //THIS IS LINE 471
if ($file->isDir() && !$file->isDot()) {
$dirs[] = $file->getFilename();
}
}
return $dirs;
}

The DirectoryIterator class is part of the Standard PHP Library (SPL), which is installed by default in PHP 5. Maybe you are using PHP 4?
EDIT
To get information on your PHP installation, use phpinfo(). Create a .php file with this single line and open it in your browser:
<?php phpinfo() ?>

Related

A PHP Error: Only variables should be passed by reference in Codeigniter

I'm stuck in this error. Can someone help me out? I am using Codeigniter 3.1.10
Here's a snippet of my code in system/core/Common.php
function &load_class($class, $directory = 'libraries', $param = NULL)
{
static $_classes = array();
// Does the class exist? If so, we're done...
if (isset($_classes[$class]))
{
return $_classes[$class];
}
$name = FALSE;
// Look for the class first in the local application/libraries folder
// then in the native system/libraries folder
foreach (array(APPPATH, BASEPATH) as $path)
{
if (file_exists($path.$directory.'/'.$class.'.php'))
{
$name = 'CI_'.$class;
if (class_exists($name, FALSE) === FALSE)
{
require_once($path.$directory.'/'.$class.'.php');
}
break;
}
}
I keep on receiving this error message:
Notice: Only variables should be passed by reference in
/customers/5/d/b/tsoft.se/httpd.www/kanban/system/codeigniter/Common.php
on line 148
A PHP Error was encountered Severity: Notice
Message: Only variables should be passed by reference
Filename: codeigniter/Common.php
Line Number: 148
**Note : Line 148 is this return $_classes[$class];
As stated in a comment on this github issue, since you are using CodeIgniter version 3.1.10, you need to replace your system folder.
The file system/codeigniter/Common.php has been moved to system/core/Common.php on your version.

How can I access my class properties from a namespace with php version 5.5.12?

I'm using WAMPSERVER 2.5 and PHP version 5.5.12 on Windows 8 PC. I created a namespace which worked ok when I was running PHP version 5.2.12. After upgrading to php version 5.5.12 I'm getting error message about undefined variables which I think means that the namespace is not being used. Here's what my code looks like:
In my UploadFile.php file I have this:
<?php
namespace myNamespace;
class UploadFile
{
protected $avatarUrl;
public function getUrl()
{
return $this->avatarUrl;
}
protected function moveFile($file)
{
$filename = isset($this->newName) ? $this->newName : $file['name'];
echo $file[$key];
$success = move_uploaded_file($file['tmp_name'], $this->destination . $filename);
if ($success) {
$url='http://westcoastchill.com/dc-esports/images/' . $filename;
$this->avatarUrl=$url;
...
}
.....
?>
Then here's the html form that uses the class in the namespace where I get the error messages that states that the variable 'newUrl' is undefined and the index 'displaymax' is undifined.
<?php
require_once 'uploads/src/myNamespace/UploadFile.php';//<------names here
if (!isset($_SESSION['maxfiles'])) {
$_SESSION['maxfiles'] = ini_get('max_file_uploads');
$_SESSION['postmax'] = UploadFile::convertToBytes(ini_get('post_max_size'));
$_SESSION['displaymax'] = UploadFile::convertFromBytes($_SESSION['postmax']); //<------ undifined index
}
$max = 50 * 1024;
$result = array();
if (isset($_POST['upload'])) {
$destination = __DIR__ . '/uploads/uploaded/';
try {
$upload = new UploadFile($destination);
$upload->setMaxSize($max);
$upload->allowAllTypes();
$upload->upload();
$result = $upload->getMessages();
$newUrl=$upload->getUrl(); //<----------- here's the undifined newUrl;
} catch (Exception $e) {
$result[] = $e->getMessage();
}
}
$error = error_get_last();
$oldUrl=$newUrl;
...
?>
}
How can I access my class from a namespace with php version 5.5.12?
Thanks for any help with this!
UPDATE: Sorry I had getUrl() outside of superclass but in my actual project is in the correct place. So I tried:
\myNamespace\UploadFile::convertToBytes(ini_get('post_max_size'));...
but I still get the same error. I also tried adding use:
myNamespace\UploadFile and \myNamespace\UploadFile...
still getting the same error message. The thing is my code worked with the using statement before I updated PHP so I'm curious why just an update would change things.
UploadFile class is within the namespace myNamespace, so to reference the class from outside of myNamespace, you would need to use \myNamespace\UploadFile.
If you are already in the global namespace, you don't need the leading slash, but I think it's good practice to always use the leading slash, since the leading slash refers to the global namespace.
Ex:
$_SESSION['postmax'] = \myNamespace\UploadFile::convertToBytes(ini_get('post_max_size'));
and
$upload = new \myNamespace\UploadFile($destination);
The issue was the configuration of the php.ini file. After researching more I tried turning off 'display_errors=Off in the php.ini file and the code the code ran using the namespace and everything. Conclusion: the php.ini file of the new installation different settings. I'll explore the settings more but my problem was solved by turning off error displaying. Obviously this is not a desired option so I'll toggle on and off as I go forward and read up on other parameters in the file that may help me.

php fatal error "undefined function" for a function in an elgg plugin library

I am trying to write a plugin for elgg that's similar to the bookmarks plugin. I found out php doesn't do "innerHTML," so I added some code to my plugin's library to accomplish that. But I am getting this error:
PHP Fatal error: Call to undefined function DOMinnerHTML() in
/var/www/html/mod/mypluginname/actions/mypluginname/save_address.php
on line 71, referer:
http://www.example.com/mypluginname/add_address/49
The calling code:
mytitle = $doc->getElementById('myTitle');
if($mytitle){
$title = DOMinnerHTML($mytitle);
}
Line 71 of /var/www/html/mod/mypluginname/actions/mypluginname/save_address.php is:
$title=DOMinnerHTML($mytitle);
The function is in /var/www/html/mod/mypluginname/lib/mypluginname.php:
function DOMinnerHTML(DOMNode $element)
{
$innerHTML = "";
$children = $element->childNodes;
foreach ($children as $child)
{
$innerHTML .= $element->ownerDocument->saveHTML($child);
}
return $innerHTML;
}
Any idea why this function isn't considered "defined?"
The bookmarks plugin is using the library loading function elgg_load_library. This allows you to reference a library by name instead of absolute path, which makes things slightly more flexible.
If you want to use that in your plugin, you'll need to use elgg_register_library in start.php.
You have to include or require mypluginname.php ?

spl_autoload_register in Zend

I'm currently trying to use the Twilio PHP library that uses spl_autoload_register to include its classes.
function Services_Twilio_autoload($className) {
if (substr($className, 0, 15) != 'Services_Twilio') {
return false;
}
$file = str_replace('_', '/', $className);
$file = str_replace('Services/', '', $file);
return include dirname(__FILE__) . "/Twilio.php";
}
spl_autoload_register('Services_Twilio_autoload');
I throw in this code:
require_once('Library/Services/Twilio.php');
$client = new Services_Twilio($this->sid, $this->token);
And then I get this error when running it:
Fatal error: Cannot redeclare Services_Twilio_autoload() (previously declared in ...\Twilio\Library\Services\Twilio.php:9) in ... \Twilio\Library\Services\Twilio.php on line 16
This code runs off Zend, and already has a bootstrap with _initAutoload(). I'm not sure where or how I should implement the autoload for this library as I'm not very familiar with it.
I think I have reproduced the problem.
To correct it, I just add require_once('Services/Twilio.php'); in the bootstrap like this:
require_once('Services/Twilio.php');
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
....
In my case, I put Services directory in library directory (where is Zend directory).
And in a controller, I can call Services_Twilio like you :
$client = new Services_Twilio($this->sid, $this->token);
I hope it will help you. :)

What is the right way to include plugins files in plugin system

I've created a plugins system, and I've created everything in that system except, how can I inclusion plugins files to execute it.
I'm tried to create a method, Which is doing include plugins files to execute it.
-- Firstly -- :
The method that get all plugins files, and that begin with index word which indicates the main file of plugin (i.g. index-pluginName.php), and add the path and file name to an array.
public function getPluginFiles($plugin_folder) {
$dir = opendir($plugin_folder);
while ($files = readdir($dir)) {
if ($files == '.' || $files == '..')
continue;
if (is_dir($plugin_folder.'/'.$files))
$this->getPluginFiles($plugin_folder.'/'.$files);
if (preg_match('/^[index]+/i', $files)) {
$this->plugins_path[$plugin_folder.'/'.$files] = $files;
}
}
closedir($dir);
}
-- secondly -- :
The method that include all the main file of plugins to execute, and this method get the path and name of plugin file from the array that created earlier .
public function includePlugFiles() {
$this->getPluginFiles($this->plugin_folder);
foreach ($this->plugins_path as $dir=>$file) {
include_once (dirname($dir)."/".$file);
}
}
Also see an example of code that exists in plugin file:
function test() {
echo " This is first plugin <br/>";
}
$plugin->addHook('top', test); // parameters(top=position, test=callback)
Now, when I create an instance of the object to be this form .
$plugin = new plugin;
$plugin->includePlugFiles();
But after all this, shows error message
Fatal error: Call to a member function addHook() on a non-object in .... projects\plugins\index-test.php on line 7
This is the code of line 7:
$plugin->addHook('top', test); // parameters(top=position, test=callback)
I know the problem occur because, the object will not be created.
and the problem is can't create the object in every main plugin file.
It's probably not the cleanest solution, but instead of trying to reference the $plugin symbol (which is outside the scope of the plugin file), you could also do this:
$this->addHook('top', test);
Alternatively, you could explicitly create the reference inside the includePlugFiles() method:
public function includePlugFiles()
{
$plugin = $this;
$this->getPluginFiles($this->plugin_folder);
foreach ($this->plugins_path as $dir=>$file) {
include_once (dirname($dir)."/".$file);
}
}

Categories