WAMP 403 forbidden error when using 'glob()' - php

I'm trying to make PHP grab the paths to all images in a folder (and then select one at random), but I keep getting a 403 - forbidden warning.
I tried using both glob() and scandir(), as well as trying the path directly in the browser.
Here is my code (using glob()) -
$directory = plugins_url('images/backgrounds', __FILE__);
if(is_dir($directory)) :
$backgrounds = glob($directory . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
endif;
I'm using WAMP and developing locally, so I can't see any reason why Apache wouldn't have access to the folder in question.
The $directory is correct, so it's not that it doesn't exist that is causing a problem (a 404 error is returned if I deliberatly alter the path to one I know doesn't exist).
I am preventing indexing via .htaccess but I have since removed this line and it made no difference.
Can anyone please suggest what I might check to try and fix this issue? Thanks.
Edit
I should add that my PHP and Apache logs seem to not be logging this event.

In your code, you have used - plugins_url which looks like a Wordpress function which will return a URL.
But you have to pass a file path to the glob function.
So you can use plugin_dir_path(), a wordpress function to get absolute path of plugin directory which can be used in glob function.

Related

How to assign correct path in codeigniter/php in index.php

I am exploring PHP, so pardon me if someone finds my question silly.
I have a PHP file called index.php in which i have three variable which are,
$system_path = '../../';
$application_folder = '../';
$view_folder = '';
I have installed xampp, which is running correctly now i need to run the index.php file, the moment i run php file it throws an error stating 'system folder path does not appear to be set correctly. Please open the following file and correct this: index.php'. Which is quite self explanatory, path which i gave is incorrect, but i don't know which path to use? I searched here as well OP says you must have system folder and application folder,sadly i can not see that as well and i have not removed or delete anything.
My path where i have php files : C:\xampp\htdocs\admin
If anyone can guide what will be my system_path, application_folder and view_folder that will be great.

Fail with nexted includes

I have a script that includes a file and that file has an include in it, like this:
In script:
include('includes/functions/homepage.php);
In homepage.php:
include('includes/functions/parent_functions.php');
I searched here and see it is a very common problem and the solution seems to be to use
include(dirname(__FILE__) . '/includes/functions/homepage.php');
I have the above and still get this error:
Warning: include(includes/functions/parent_functions.php): failed to open stream: No such file or directory
I've tried this on a site running php 5.5 and another using 7.2 - fails the same on both. If I print the path using the following it shows the correct full path.
echo dirname(__FILE__) . '/includes/functions/homepage.php';
As mentioned, this is a common question here but the fix isn't working in my case. Can anyone see why?
Well, you have an issue here. As the parent_functions.php and homepage.php are in the same directory you need not put an extra directory prefix. You can simply use
include('parent_functions.php');
instead of
include('includes/functions/parent_functions.php');

Unable to get correct path in PHP due to server settings

I am not an expert on servers and one of my friends wanted to use some of my PHP scripts I wrote, but has a problem with his server and I was unable to find how to fix this.
Basically the problem is in generating paths to files.
In my script, I use the following variables to get the path:
$path = "/template/"; // path to the template folder
$baseURL = $_SERVER['DOCUMENT_ROOT'].$path;
Then I include my pages for example like this:
include($baseURL."scripts/functions.php");
On my server, this works fine and when I tried to echo the baseURL parameter I get this:
/data/web/virtuals/104571/virtual/www/template/
However, on my friendĀ“s server, it just throws "PHP Fatal error: Call to undefined function" - because I checked and the functions.php is not being loaded,
When I tried echoing baseURL on my friendĀ“s server, I got nothing.
So I tried viewing phpinfo() and interestingly, I did't find the DOCUMENT_ROOT there at all and the paths are just weird, the PHP error shows:
PHP Fatal error: Call to undefined function convertT() in H:\webspace\hostings\kocher.es\hosting\www\meteotemplate\index.php on line 14
As I dont really understand servers so I dont know what this means, why it has that H:/ drive there and more importantly, how to fix this....
Here is the phpinfo() displayed:
http://kocher.es/meteotemplate/
If anyone knew how to fix this I would very much appreciate it, thanks.
try __DIR__
example:
$basePath = __DIR__;
$templatePath = $basePath."/template/"; // path to the template folder
include($templatePath."/scripts/functions.php");
__DIR__ will return file base directory and its different in different files which mean you must be careful from when will call it
You can try with dirname(), pathinfo() and realpath() to determine the base path instead using $_SERVER['DOCUMENT_ROOT']
Please add document root path in httpd.conf file of apache
Or try _SERVER["CONTEXT_DOCUMENT_ROOT"]

How come my code works separately but not when I merge it with my controller?

I wrote this code in a test.php file.
<?php
include ($_SERVER["DOCUMENT_ROOT"].'/MySite/protected/MyYouTube/google/src/Google/autoload.php');
require_once ($_SERVER["DOCUMENT_ROOT"].'/MySite/protected/MyYouTube/google/src/Google/Client.php');
require_once ($_SERVER["DOCUMENT_ROOT"].'/MySite/protected/MyYouTube/google/src/Google/Service/YouTube.php');
?>
If I go to this file like this: localhost/MySite/protected/MyYouTube/test.php it works, but if I copy the same code into my controller that is located under the same folder as test.php, I get this:
include(Google_Service.php): failed to open stream: No such file or
directory
There are no conflicts with the imports. In fact, the controller and view can be empty and still I get the same thing.
Apparently that happens when autoload.php is not actually loaded.
How is it possible that when the code is integrated into the website it throws this error?
This is what the path to my site looks like:
localhost/MySite/index.php/user/view It seems that the way I visit the file matters.
I tried several things. I tried importing the test.php into my view or my controller, and still I get the same error. I tried using Yii::app()->basePath and it gives the same problem.
The paths are correct, I have checked several times. How can I fix this?
The include path is actual server path, not site path, if you use / at the beginning, you are telling it to look in the server root.
If you know the absolute path in the server you can use it like /var/www/MySite or c:\mysiteif you don't know that then you use relative paths.
$_SERVER["DOCUMENT_ROOT"] works well with PHP 5.3, not sure if below check your version. Try removing the '/' before the MySite part, as the variable already does it for you so it might be printing like localhost//MySite, although I'm not sure if that should or shouldn't work. ]
Also the autoload php should be loaded with a require_once function, not with the include. Good luck!

PHP Image Upload trouble

I am currently working on a Joomla! website. I am using the Jumi extension (which allows custom scripts to be made and executed within Joomla! itself) to create a simple file upload tool. The problem is that I get the following error:
Warning: copy(C:/xampp/htdocs/images/1253889508.jpg) [function.copy]: failed to open stream: No such file or directory in C:\xampp\htdocs\Joomla\components\com_jumi\jumi.php(25) : eval()'d code on line 61
The offending code is as follows:
//we will give an unique name, for example the time in unix time format
$image_name=time().'.'.$extension;
//the new name will be containing the full path where will be stored (images folder)
$newname='C:/xampp/htdocs/images/'.$image_name;
//we verify if the image has been uploaded, and print error instead
$copied = copy($_FILES['image']['tmp_name'], $newname);
The full code is available here. Once I get this working, I will be modifying it to meet the needs of my website.
I'm not sure if the problem is a permissions issue with Jumi or if there is some other problem. The best I can tell is that for whatever reason, the temp file is not being created.
Thanks for any tips you may have!
Try this:
if(move_uploaded_file($_FILES['image']['tmp_name'], $newname)){
// move worked, carry on
}
And use relative paths instead of absolute ones.
does your C:/xampp/htdocs/images directory actually exists?
if not create it manually or with mkdir()
also try to use the constant DIRECTORY_SEPARATOR instead of hardcoding slashes
Also you should use the move_uploaded_file() for this and not the copy() function.
And never hardcode absolute paths into your scripts! instead get the root path and preferibly set it as a constant, this is mostly done with the dirname() function in the entry file, but joomla allready has a constant you can use for this.
Forward slashes in the $namename path rather than backslashes?

Categories