PHP works on localhost, error on Google Cloud server - php

I have a PHP that runs fine on localhost but throws error on the cloud server. On the localhost, the script runs and also fetched the data from datastore.
This is the line thats causing the problem:
require __DIR__ . '/../../vendor/autoload.php';
// this is the line5 of datastore.php
I deploy it using this code:
gcloud app deploy --promote --stop-previous-version app.yaml
After deploying to the Google AppEngine, i get this:
Warning:
require(/base/data/home/apps/myproject/projectID/dialpad_research/api/../../vendor/autoload.php):
failed to open stream: No such file or directory in
/base/data/home/apps/myproject/projectID/dialpad_research/api/datastore.php
on line 5 Fatal error: require(): Failed opening required
'/base/data/home/apps/myproject/projectID/dialpad_research/api/../../vendor/autoload.php'
(include_path='.;/base/data/home/apps/myproject/projectID/;/base/alloc/tmpfs/dynamic_runtimes/php55_dynamic/ef537742f8701211/sdk')
in
/base/data/home/apps/myproject/projectID/dialpad_research/api/datastore.php
on line 5
This file is required inside another PHP file too.
But even if I try to access this file directly, I still get the same error.
Should I be adding the require code differently?

The problem was that the vendor folder was not getting uploaded.
.gcloudignore file was preventing it.
After correcting it, this error is gone.
Thanks everyone from trying.

Your require code should look like this. Your vendor folder should normally be at root directory level of your code on the same level as your app.yaml. In which case, your require should look like this:
require 'vendor/autoload.php';
Also, sanity check and make sure the file is actually there in the vendor folder.

Related

"File not found" When running on heroku, but it works on local

I want to include a file that is in a parent folder in my php code using:
include('../defines.php');
However when I deploy to heroku from CLI and run my php file it gives me this error.
PHP Warning: include(): Failed opening '../defines.php' for inclusion (include_path='.:/app/.heroku/php/lib/php') in /app/workers/test.php on line 2
If I run locally from my installed Wamp 3.1.4 server it run fines without issues.
I can make it work if I remove the `../? from the path. But that seems wrong.
Another way I've found on github is doing
include __DIR__ . '/../defines.php';
But i think doing it that way would be using absolute, not relative path.
Is there something i'm missing?
<?php
include('../defines.php');
?>
Folder tree:
www/
defines.php
workers/
test.php
It's depending how you classified your folder.
Maybe you can try this :
require("/../yourFolder.php");

Cannot include php files from root using subdomain

I have recently made an API now for that api i have made a subdomain however it broke my php file includes
include $_SERVER['DOCUMENT_ROOT'].'/core/init.php';
now my subdomain i am using is api.website.com and i need to include something from website.com when i try to include its giving the root of the subdomain,
Summary
When i try to include init.php i get
Warning: include(/home/website/public_html/API/core/init.php): failed to open stream: No such file or directory in /home/website/public_html/api/apex/getusers.php on line 2
When i actually need
Warning: include(/home/website/public_html/core/init.php): failed to open stream: No such file or directory in /home/website/public_html/api/apex/getusers.php on line 2
I apologize for my grammar if need be.
Changing your include path to
include ("../../core/init.php");
will fix your problem. However, this is not a recommended approach.
You should think about using a dependency manager like composer. ( https://getcomposer.org/ ). Then your included files will always be under a directory like
/home/website/public_html/vendor/core
and there will be less of a chance of your application breaking if the files in /home/website/public_html/core change.
This should work.
include("../core/init.php");
Use following code
include_once $_SERVER['DOCUMENT_ROOT'] . '/../core/init.php';
visit https://developer.hyvor.com/php/including-files-in-root-with-subdomain.php to learn more.

Symlink - require_once doesn't seem to work?

I store my WordPress theme in my Dropbox to use it easily on many machines. Unfortunately require_once()/include_once() doesn't seem to work for me.
Is store original theme at G:\Dropbox\Dropbox\Wordpress\Themes\Blabla\
The symlink is placed at C:\xampp\htdocs\blabla\wp-content\themes\Blabla
When I wanted to require/include any file, eg. C:\xampp\htdocs\blabla\wp-load.php I used to do require_once('../../wp-load.php');
But with symlink all I got is:
Warning: require_once(../../../wp-load.php): failed to
open stream: No such file or directory in
G:\Dropbox\Dropbox\Wordpress\Themes\Blabla\foo\bar.php on line
2 Fatal error: require_once(): Failed
opening required '../../../wp-load.php'
(include_path='.;C:\xampp\php\PEAR') in
G:\Dropbox\Dropbox\Wordpress\Themes\Blabla\foo\bar.php on line
2
Looks like require_once looks for the file within the Dropbox, not symlink context? Is it possible to fix it somehow? I can't use absolute path as I develop on different machines / different OSes and those vary... Any ideas? How does require_once work when it's symlinked? Does it look for required file in both places (original-context & symlink-context)?
How about putting the original file into you Wordpress folder and symlinking to dropbox?
While installing this, you should turn off dropbox on all machines where you did not move the folder yet.

Using PEAR XMLRPC2 inside wordpress plugin

I am trying to run the PEAR XMLRPC inside a plugin function to do some validation. It is all working fine in a standalone app that in a folder on my server, but as soon as I put all the files into my plugin folder, the:
require_once 'XML/RPC2/Client.php';
doesn't work. The Client.php file return an error:
Warning: require_once(XML/RPC2/Exception.php) [<a href='function.require-once'>function.require-once</a>]: failed to open stream:
So i assume it has to do with relative vs non-relative files, but I can't seem to figure out why it works outside of wordpress just fine.
If I start changing all the
require_once 'XML/RPC2...
to be the absolute path on the server the errors start going away, but I feel like I shouldn't have to change the source of PEAR and XMLRPC to do what i need. Any Suggestions?
You could update the PHP include path:
set_include_path(get_include_path() . PATH_SEPARATOR . $pathToPearLibs);
require_once($pathToPearLibs . 'XML/RPC2/Client.php');
This will give PHP one more (correct) place to look after it attempts to load from the wrong place.

Web application not finding file in what appears to be the correct path

I tried to reorganise my facebook application files into better folders and now when I try to load my application on facebook it throws the following error:
Warning: require_once(../scripts/mysql_connect.php) [function.require-once]: failed to open stream: No such file or directory in C:\xampp\htdocs\<root folder>\Model\Database.php on line 3
Fatal error: require_once() [function.require]: Failed opening required '../scripts/mysql_connect.php' (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\<root folder>\Model\Database.php on line 3
The second error is a result of the failure of the first one I think. The weird thing is that my program works perfectly fine when I'm accessing through the localhost url in the browser but when I run it through facebook it throws this error.
In my root folder I have a "Model" folder where the Database.php file is kept and a "scripts" folder where the mysql_connect.php file is kept. I have tried clearing my cache on the browser and using private browsing but it is still throwing this error.
Does anyone know what could be wrong?
EDIT: Sorry this does not seem to be working in localhost either now! One thing I think is weird is that it says "failed to open stream : NO SUCH FILE OR DIRECTORY IN" and then lists the database.php path. Why is it looking in there?
I use the line require_once('../scripts/mysql_connect.php'); at the top of my Database.php file to include it.
It seems that on the server there is a different working directory. Something like the following owuld be a quick fix
dirname(__FILE__) . '/../scripts/mysql_connect.php'
Ok I've fixed this, but I'm still unsure why it was a problem. In my root folder I have two folders: scripts, and Model. I have a database.php file in my Model folder which has the following line of code in:
require_once('../scripts/mysql_connect.php');
For some reason this did not work, but by replacing the line with:
require_once('/scripts/mysql_connect.php');
This started working again! Just thought I would post the solution. Does anyone know why the other way failed? I thought the ".." just makes it go up one level in the folder hierarchy i.e. to the root folder in this case, and then follow the path which would have taken it to the file...
EDIT: This appears to only have solved it for localhost and not through Facebook...
Do you have permission to edit your php.ini file? One solution would be to find the line with the include path (you'll notice it has C:\xampp\php\PEAR in there already). Include the full path to the "scripts" folder you're referencing, then you can change the php code to read:
require_once('mysql_connect.php');
PHP will be able to find this file since it's in the include path. Just make sure that if you start adding several include paths that you shouldn't have a file named mysql_connect.php in two or more of the different include paths because you'll most likely get errors. Hope this helps. It definitely makes for some cleaner code.

Categories