Opencart module include custom php file issue? - php

I'm trying to create an OpenCart 1.5.1.3 module. The issue I'm having is with including a custom php script.
When I do this in the controller:
include('module/simple_html_dom.php');
or
include('simple_html_dom.php');
I'm presented with the following error:
Notice: Error: Could not load language module/simple_html_dom! in D:\xampp\htdocs\store\system\library\language.php on line 26
I suspect there's a simple explanation, but just can't work it out.

The reason is that you're still in theory calling from the index.php file, so need to either use a relative path from that, or use a defined variable, which you can find in the config.php file that have all the relevant paths OpenCart uses

I've found a way to include the file I wanted. I'm not sure it's the proper way of doing this but I used the following code - with OpenCart having such little documentation it's not easy to debug and/or develop for it.
require_once(DIR_SYSTEM . 'simple_html_dom.php');
DIR_SYSTEM is OpenCart's /system folder, so I placed my simple_html_dom.php file in there and voila, it worked.

I have the same problem in opencart. i have to install file i have something like
<?php include 'i_header.tpl';?>
I replace it with
<?php include (DIR_TEMPLATE. '/module/---your module name ---/i_header.tpl');?>
and it works. You can chose the correct path name which under the config file.
ROOT/config.php
ROOT/admin/config.php
I hope this helps.

Related

Get the server path /home directory from php wamp windows

I have the following hierarchy
c:/wamp64/www/site/folder/folder2/file.php
in file php I want to call to a file that is located here:
c:/wamp64/www/site/folder3/folder4/file2.php
How can I do that? ../../. works only in some cases, if I call to the file from different locations it's not working.
I need to get always the root path, how do I get it?
Use it this way, First define a path SITE_PATH as you know which file to call which is here folder3/folder4/file2.php. if you are using MVC kind of framework then define SITE_PATH as very top most of file, so that in future if you change the base location of you project then you can change it in one variable then it will good to go.
<?php
define("SITE_PATH","c:/wamp64/www/site/");//defined a common path.
$fileToCall="folder3/folder4/file2.php";//file to use
require_once SITE_PATH.$fileToCall;;

require_once(PHPExcel/Classes/PHPExcel.php): failed to open stream

I am trying to include PHPExcel to a Silverstripe 3 site to export excel sheets. Right now I am just trying to test, but I get this error when trying to do it:
[Warning] require_once(/sitename/mysite/AddOns/PHPExcel/Classes/PHPExcel.php): failed to open stream: No such file or directory
Thing is I know this file exists since I copied it over myself and have rechecked the path over and over. So I decided "well check if the file exists" using this code:
if(!file_exists(Director::baseURL().'mysite/AddOns/PHPExcel/Classes/PHPExcel.php')) {
echo 'sdf';exit;
}
The path is correct (that is where it is saved) according to the error, but- file does not exist. I am also requiring the file in the same way, with no luck
require_once Director::baseURL().'mysite/AddOns/PHPExcel/Classes/PHPExcel.php';
I have tried everything-checking file permissions, referencing parent folders using ../../, calling it directly like AddOns/PHPExcel, moving it to this new AddOns folder (first tried placing the PHPExcel classes on the root and discovered that Silverstripe doesn't read it then :) )
I know I am doing something wrong but for the life of me I cannot see what. Please help
Thanks
BASE_PATH is the best way to access the web root folder.
require_once(BASE_PATH . '/AddOns/PHPExcel/Classes/PHPExcel.php');
Also this is only an issue if you are not using composer, to solve this issue in the correct way you should use composer.
You should consider using composer to include the PHPExcel class, this will avoid the need to manually require the class and will help you with dependency management.
composer require phpoffice/phpexcel
As pointed out Director::baseURL() will return the URL rather than the filepath.
Instead require relative to the file web root like so:
require_once(BASE_PATH . '/AddOns/PHPExcel/Classes/PHPExcel.php');
As pointed out by both Dan and Barry in the other answers, it's preferable to use composer for dependency management.

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 include fail to open path of included files

I'm trying to include file from another php framework but doing so it's giving me an error failing open the stream for the files which are included inside the file I'm trying to include.
Any idea on how to include it properly so that the files included inside my included file are able to be processed?
What Framework are you using?
I have little experience with templates but I don't think you should be adding PHP code to a template file.
Click Here for another question on how to add PHP code to a template file.
However as far as your path goes, try using:
include("../../other/index.php");
Your include only looks only one directory up.
include ("../../other/index.php");
would be two directories up so into the "admin" directory then the "other" directory
This has been asked before here

Including file in cakephp

I'm getting mad. I can't understand what's the problem.
Obviously i'm trying a require_once() but it doesn't work. As you can see, the file should be in the right place. Anyone have a clue why it doesn't work?
If you have a file in your vendors folder (i.e., vendors/filename.php)
You can do:
App::import('Vendor', 'filename');
Here's some more examples of including vendors
You can include files in different ways:
http://book.cakephp.org/2.0/en/core-utility-libraries/app.html
In your case you're using Lucene as a vendor, so the correct include would be:
App::import('Vendor', 'lucene/Search/Lucene');
to load /app/Vendor/lucene/Search/Lucene.php
Just place your path appropiately. For instance
require_once('/cake/importedfolder/imported_file.php');
In as much as your .htaccess is set to have permission and mod_rewriting is enabled.
To narrate better: you can place the file or folder containing the file you want to import in the 'webroot' folder
For instance we want to require a connection.php file (just to explain tho).
All you have to do is place the file in 'webroot'
And then require it. : thus 'require_once(/cake/connection.php');
Let me know if this helps.....

Categories