Facing issue while performing file reading operations in php - php

I am new to php . Currently as part of Migration activity we copied the existing php application to a new infrastructure where we are facing a strange issue.
Problem Statement:
From the php application,not able to read any file and it is not throwing any errors also. I used the following libraries and got the same behavior:
file_get_contents (This is the existing mechanism and I should use this one in the new infrastructure also)
Below two, checked to triage the issue:
readfile
fopen (tried with fopen just to see whether able to open like fopen($file, "r") or die( ),but this is also going to die condition
Issue Triaging
Made the below sample.php and make it available in my current server configuration. This is available by accessing through url (for e.g. https://myapplicationdomain/myapplication/sample.php). This php component is reading an input.txt which is in the same directory (actually this is the simulation of our problem)
<?php
ini_set('display_errors', 1); // show errors
error_reporting(-1);
//set your own error handler before the call
set_error_handler(function ($err_severity, $err_msg, $err_file, $err_line, array $err_context)
{
throw new ErrorException( $err_msg, 0, $err_severity, $err_file, $err_line );
}, E_WARNING);
echo "fopen url is";
var_dump(ini_get('allow_url_fopen'));
try{
$file ='input.txt';
if(is_readable($file)) {
echo "$file is readable \n"
} else {
echo "$file is not readable \n";
}
if(file_exists($file))
{
$homepage = file_get_contents($file);
var_dump($homepage);
echo "content is $homepage \n";
}else{
echo "file is not present \n";
}
}catch (Exception $e) {echo 'Error Caught $e';}
?>
Apache server is running using apache user and this user is having permission in the files and parent directories,
Following are the observations we got during triaging this:
When accessing through url for e.g. https://myapplicationdomain/myapplication/sample.php (i.e. through apache server).
a. functions is_readable & file_exists are returning true.
b. file_get_contents is returning blank and when I did var_dump, it is returning 'Null'
c. Confirmed the value of allow_url_fopen and it is returning 1 which is true
d. No errors are getting logged in this case.
e. Changed the permission of the 'input.txt' to see the permission behaviour. In that case,proper error got displayed in is_readable it self.So through this way ruled out the possibility of permission issue and also confirmed the error handler implementation is working fine.
f. Changed the location of input.txt to outside folders . ie. home/apache_user/ or root directory. Still file read operations are failing.
Able to run the same php code independently. i.e php sample.php .In this case,it is working as expected and all file reading operations are working fine.
3.Checked all the below php.ini configurations (removed all the values in disabled functions for testing)
a. open_basedir => no value
b. disabled_functions => no value
c. allow_url_fopen => true
Checked all the posts ,but not getting any idea how to proceed further. Following are version we are using: PHP 7.3.5 , CodeIgnitor framework and also configured php-fpm.

As per my understanding php library which apache was referring had some issues. Since it is a migration project and it is working fine in the current environment,copied the existing php folder (which was inside apache) and fpm binaries and configurations from the working environment to the new environment.Still not got any idea on exact root cause (as only read scenario was failing),but this got worked

Related

Joomla add article via php script Application Instantiation Error

i created a script in order to insert some articles via a php script. This script is working on my local machine (xampp), but when i deploy the whole joomla project to my web server i get the following error message:
Error displaying the error page: Application Instantiation Error: Application Instantiation Error
By adding some echo calls, i was able to find the line which causes the error:
$app = JFactory::getApplication('site');
Now i am wondering how to fix this behaviour and make my function also run on the web server.
Below i will provide my system informations and the beginning of my php function until the line, which causes the error message:
Systeminformations
joomla version: 3.6.5 Stable
db version: 5.6.34-79.1-log
php version: 5.6.30-he.0
web server: Apache
PHP snippet
<?php
echo "STARTING; ";
// get db connection
include('../includes/mysql.inc.php');
// get all sql querys
include('./autoNewsQuerys.inc.php');
/**
* Prepare joomla framework to insert article correctly
*/
if (!defined('_JEXEC')) {
define('_JEXEC', 1);
define('JPATH_BASE','/is/htdocs/wp1088688_4E1H7PYJFK/www');
require_once(JPATH_BASE . '/includes/defines.php');
require_once(JPATH_BASE . '/includes/framework.php');
defined('DS') or define('DS', DIRECTORY_SEPARATOR);
}
echo "searching app;";
$app = JFactory::getApplication('site');
echo "Found app;";
Full browser output
STARTING; searching app;Error displaying the error page: Application Instantiation Error: Failed to start the session because headers have already been sent by "/is/htdocs/wp1088688_4E1H7PYJFK/www/1500AutoNews/autoNews.php" at line 3.
So as you can see the error is caused by the JFactory call. 'Found app' is never printed out.
Just copy the Joomla index.php file to another file and then replace the $app->execute code with your code.
Note: You don't need to include any MySQL library - since you are loading the Joomla environment then you have access to the JDatabase class, which is a database abstraction layer.
Another note: this file autoNewsQuerys.inc.php likely contains some issues (maybe you are trying to instantiate the app there, but the Joomla environment hasn't loaded yet). You should move it to the end.
if it was working on localhost and it is not working on Web server then the biggest issue is with defining the base path. Give a hardcoded base path of your joomla installation. I suggest you to check your base path using a php script. Create a php file and name it path.php. Put it in the Joomla main folder. Content of the file
<?php
echo "The Base path is ".getcwd();
?>
Once you get the path just change the Base_Path. For ex:
define('JPATH_BASE', '\var\www\joomla');

Bad flag in substitute command: 'i'.sql

Running a database migrate in PHP (via Phing), I'm getting the following FAILED BUILD error:
Unable to open /site/thewebsite.co.uk/data/migrate-web_site-sed: 1: "s/[^a-z0-9]/-/gi": bad flag in substitute command: 'i'.sql for reading:
as well as several warnings along similar lines:
[PHP Error] fopen(/site/thewebsite.co.uk/data/migrate-web_site-sed 1: "s/[^a-z0-9]/-/gi": bad flag in substitute command: 'i'.sql): failed to open stream: No such file or directory [line 224 of /site/thewebsite.co.uk/vendor/phing/phing/classes/phing/tasks/ext/dbdeploy/DbDeployTask.php]
Does anyone have ideas about where to start looking for a resolution and/or the cause of the problem? The site files were cloned from a previously working repository and to my knowledge the DbDeployTask.php file has not been changed by any of my co-workers since the last working copy was cloned. I've had a look through the relevant .php file and I can't see any obvious issues. The section of the code referenced looks pretty standard:
protected function createOutputFile($file, $undo = false)
{
$fileHandle = fopen($file, "w+");
$sql = $this->generateSql($undo);
fwrite($fileHandle, $sql);
}
With createOutputFile called from within the deploy function:
protected function deploy()
{
// create deploy outputfile
$this->createOutputFile($this->outputFile, false);
// create undo deploy outputfile
$this->createOutputFile($this->undoOutputFile, true);
}
Where would people advise looking for the source of this issue, so our Apache server is able to deploy the development website correctly on this Macintosh?

Owncloud webdav access from php

I'm trying to access my owncloud webdav interface and get the list of files in the root directory from PEAR WebDAV Client. I'm trying the following:
include("HTTP/WebDAV/Client.php");
$client = new HTTP_WebDAV_Client_Stream();
$user="admin";
$pass = "q1w2e3r4";
$dir = "webdavs://".$user.":".$pass."#127.0.0.1:10081/owncloud/remote.php/webdav/";
$stuff = $client->dir_opendir($dir, array());
var_dump($stuff);
var_dump($client->dirfiles);
The code print false, false, and when I've tried to debug it, I saw Connection refused error in Socket.php, when it tried to call the function
fsockopen("127.0.0.1", 10081, $err, $errstr);
It's even more strange since I can access this server and list the directories from cadaver though. Also I can access the test DAV server with my code by replacing my $dir variable:
$dir = "webdavs://".$user.":".$pass."#127.0.0.1:10081/owncloud/remote.php/webdav/";
It's an ugly hack, but if you add something similar to:
var_dump($err->getMessage());
to /usr/share/php/HTTP/Request.php on line 739, you should be able to see the most relevant error message. (If that is where Request.php got installed to.)
Unfortunately, there doesn't appear to be a mechanism in place in the underlying HTTP_Request package for raising that specific error message back to the HTTP_WebDAV_Client_Stream code.

Multiple requests conflict time to read or copy a file

Details:
PHP Version: 5.3.0
Apache 2.2.37
Windows XP SP3
I am creating a system backup error (php error and custom error) in TXT, not user data but the dynamically generated page.
If you have already generated the LOG other user does not need to generate as ever existed.
I simulated 10 concurrent connections/requests and sometimes this error occurs:
No such file
Code:
<?php
$p = 'errs/'.$arqErr;
$o = 'temp/'.$arqErr;
if(file_exists($o) && is_readable($o) && is_writable($o)){
if(!copy($o,$p)){
echo 'Error copy "',$p,'" to "',$o,'"';
}
if(file_exists($o) && is_readable($o) && is_writable($o)){
unlink($o);
}
}
?>
sometimes the error occurs:
Warning: Unable to open 'temp/6039dd66559c9431004109202d279557.php' for reading: No such file or directory
and sometimes the error occurs:
Warning: Unlink failed (No such file or directory)
Is it a bug of PHP_5.3.0?
Note: In Windows Seven with PHP5.3.4 this failure does not occur.
The problem seems to be having multiple concurrent users trying to access the same files, it doesn't seem to be a PHP related problem.
Try to think of a different logging scheme, maybe using the error_log() function you can have PHP manage concurrency access issues for you.
<?php
error_log('Log message', 3, "md5(address of the page).LOG.txt");
?>

PHP - writing file - Permission denied for one script, but not another in the same directory

One of my PHP scripts writes data to a file. It is executing without issues. However my test code which calls the same methods in the same way and (for the purpose of trying to clear up this problem) is located in the same directory is reporting a:
failed to open stream: Permission denied
error.
My correctly executing script is simply:
try {
$printer = new PDFPrinter();
$FDFData = $printer->assembleFDFData(5);
$fdf_file = $printer->writeFDFFile($FDFData);
$printer->downloadForm($fdf_file);
}catch(Exception $e) {
echo $e->getMessage();
}
and my test code is:
function setUp() {
$this->printer = new PDFPrinter();
$this->FDFData = $this->printer->assembleFDFData(5);
}
function testWrite() {
try {
$this->printer->writeFDFFile($this->FDFData);
$this-assertTrue(true);
} catch(Exception $e) {
$this->assertTrue(false);
}
}
I know the testWrite test code is bad, but I just want to get it to stop throwing that exception first.
The script throws the exception when executing the writeFDFFile($this->FDFData); method as it tries to open the file to write. This is the code which fails:
$fp=fopen($fdf_file,'w')
I don't understand how 2 files in the same directory with the same permissions can work differently when executing the same code.
What could I be doing wrong?
Many thanks
How do you run standard and test code? Maybe you run standard code through apache (open in browser), but test code through directly php (run from IDE). And maybe apache have permissions, but php is not. And maybe working directory is difference ($fdf_file contains absolute path?).
Note also that "failed to open stream: Permission denied" is warning (not fatal error) and when you run standart code this warning will not stop execution. But PHPUnit convert warnings to exceptions and this warning will stop execution when you run code under PHPUnit.

Categories