What can cause a web server to not find an XML file? - php

I have a PHP script which reads from an xml file to generate a table. The first thing it does is check to make sure the file exists.
$path = getcwd();
if(file_exists($path.'\inc\php\kbs.xml')){
$kbs = simplexml_load_file($path.'\inc\php\kbs.xml');
} else {
echo "Error: No KB file found";
}
For some reason, intermittently, it doesn't find the file. I've tried removing the file_exists check all together (since I know the file does exist) but it still doesn't load the file at times. I can refresh the page and 7 times out of 10 it doesn't load, but sometimes it does.
I never ran into this problem during development, but once it went production (being used by maybe 200 users now) it started happening.
How do I go about troubleshooting this? (PHP 5.2.14 running on IIS)
EDIT: Error logs give me the following messages when it fails:
Notice: Undefined variable: kbs in <the path> on line 16
Notice: Trying to get property of non-object in <the path> on line 16
Warning: Invalid argument supplied for foreach() in <the path> on line 16
line 16 is the first time the variable $kbs is accessed. Obviously $kbs isn't set if the file isn't found.

Please use the absolute path, relative path make things a mess.

Is the location relative to PHP? Do permissions allow the web server to see it?

Related

PHP WHY Warning: filemtime(): stat failed for

Warning: filemtime(): stat failed for includes\img\defaultimg.jpg in
I read and researched but found nothing useful
in the php manual only says this:
If PHP's integer type is only 32 bits on your system, filemtime() will fail on files over 2GB with the warning "stat failed". All stat()-related commands will exhibit the same behavior.
but is just a jpg 900k
this is my code works fine in xammp, but not in production
$dirimg="includes/img/"; //this one also throws warning
$filename = "".$dirimg."".$resultsb['Img'].".jpg"; //the path
if(file_exists ($filename)){ // exists?
$timeimg=filemtime($filename);
$srci="".$dirimg."".$resultsb['Img'].".jpg?=".$timeimg."";
}else{$timeimg=filemtime("includes\img\defaultimg.jpg"); //another path, the file does exists
$srci="includes\img\defaultimg.jpg?=".$timeimg.""; } //
both throw the same warning in the respective path, the file exits btw
the image can be seen,
but i need to know
when it is modified so the browser refreshes it
It was the slashes hahaha , the server is in Linux
So, they have to be like this "includes/img/defaultimg.jpg?="
instead of "includes\img\defaultimg.jpg?="
LOL

Can't get $_SERVER to run properly

I'm trying to execute "opendir" to understand how it works & use it in a recursive page call (I get wrong directories using .. & .).
My folder is constructed as follows:
C:\xampp\htdocs\Ask_Alansky
C:\xampp\htdocs\ is what comes in the XAMPP package, & Ask_Alansky is the folder in which I'm working.
The issue is simply that I cannot get a opendir () to work & open the directory. I'm not sure if I'm misunderstanding the concept, but I expect the code to simply open the folder "Ask_Alansky" & show me the files.
My code is:
<?php
opendir($_SERVER['C:/xampp/htdocs/Ask_Alansky'] . '/index.php');
?>
I get:
Notice: Undefined index: C:/xampp/htdocs/Ask_Alansky in
C:\xampp\htdocs\Ask_Alansky\php_main\Testing.php on line 14
Warning: opendir(/index.php,/index.php): The system cannot find the file specified. (code: 2) in
C:\xampp\htdocs\Ask_Alansky\php_main\Testing.php on line 14
Warning: opendir(/index.php): failed to open dir: No such file or directory in
C:\xampp\htdocs\Ask_Alansky\php_main\Testing.php on line 14
What I'm doing wrong?
$_SERVER is an associative array that contains multiple server information. Therefore, there is not such key C:/xampp/htdocs/Ask_Alansky. If you wish to access the current directory, use getcwd() instead. It is also not a good idea to use absolute path to your file, this will cause a problem when you migrate your app to another server.

PHP 5.6, find an explanation for seemingly unexplainable errors

someone could tell me if there is a reason why I get these PHP errors, which should not exist.
PHP Warning: include (xyz.php): failed to open stream: No such file or directory in /mypath/myfile.php on line 351
When the file clearly exists!
PHP Fatal error: Can not access MyClass::$myproperty in /mypath/MyClass.php on line 250
When the declaration exists:
class MyClass
{
...
public static $myproperty = array();
...
I would like to emphasize that these errors are not always reported! But only once in a while. When I make a mistake writing a part of the code, the PHP fatal error should always occur, not 5 times a day from 12:22 to 12:27 and nevermore. I'm talking about a site from thousands of visits a day, I'm also talking about a .php file that is included on a regular basis in every request of any page.
I can't get any error in my pc (php 5.6 also) when I run and test the web application.
Is it possible some server problem?
Thanks.

copy(): Filename cannot be empty error (only occasionally)

I have a strange php issue.
I have a custom front-end post submission form for WordPress. It most of the time but the other times it seemingly at random doesn't work.
My PHP error log spits out the following:
PHP Warning: copy(): Filename cannot be empty in /home/y567889/public_html/wp-content/themes/colormag/inc/front_deck.php on line 1071
PHP Warning: file_get_contents(https://ygoprodeck.com/pics/Trinity World Chalice-deck-14461.png): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found
in /home/y567889/public_html/wp-content/themes/colormag/inc/front_deck.php on line 1025
PHP Warning: unlink(/home/y567889/public_html/pics/Trinity World Chalice-deck-14461.png): No such file or directory in /home/y567889/public_html/wp-content/themes/colormag/inc/front_deck.php on line 172
So in theory, a user could submit 10 posts. Sometimes 2 of those posts would generate with errors (missing featured image etc etc) with the above error in the php log.
What I have tried:
Changed copy() to move_uploaded_file() with no joy.
Changed permissions on the "pics" folder to 775.
It occurs to one of my users in particular. It happens him with every post he attempts to upload. Generating a new WP account fixes this for him (although it still fails occasionally as per usual).
Because the error starts at line 1017 I'll show what the code is for that:
function moveYdktoDeck($ydkId,$postid){
$ydkUrl = wp_get_attachment_url( $ydkId );
$newfile = $_SERVER['DOCUMENT_ROOT'].'/UploadedDecks/3/'.$postid.'.ydk';
copy($ydkUrl, $newfile)
}
Function wp_get_attachment_url can return false. Check that before you use it in copy function.

copy() function returns "permission denied"

The code below used to work just fine, but since upgrading my system (from Mandriva 2010 & KDE 4 to Fedora 23 & KDE 5.14) a lot of things have broken, including this. I've managed to fix most of the other problems, but this specific issue is still eluding me.
I suspect the code itself isn't the problem, but I'll post it here so you have all the elements:
$infile = stripslashes($infile);
$filepath = "../site/images/thumbs/$dlettr/";
$outfile = $filepath . $id . '-ori.' . $fext;
copy($infile, $outfile);
$gis = getimagesize($outfile);
$width = $gis[0];
$height = $gis[1];
$outfile2 = str_replace("-ori.$fext", '-0.jpg', $outfile);
$im = new Imagick("$outfile");
$im->setImageFormat("jpg");
$im->writeImage("$outfile2");
unlink($outfile);
The original $infile is the URL of an image that is passed through a form.
$dlettr is a letter that is determined earlier in the code.
$fext is the file extension, also determined earlier.
$id is a numerical associated with the page the code is running on, and thus a unique identifier for the file.
What the code does is simply download an image from the given URL, save it in the appropriate path, and associate it with the current page it is being processed from.
Since upgrading, I now get the following when I try to run the code, regardless of the URL:
Warning: copy(../site/images/thumbs/G/27879-ori.jpg): failed to open stream: Permission denied in /var/www/html/site/index.php on line 1055
Warning: getimagesize(../site/images/thumbs/G/27879-ori.jpg): failed to open stream: No such file or directory in /var/www/html/site/index.php on line 1057
Fatal error: Uncaught exception 'ImagickException' with message 'unable to open image `../site/images/thumbs/G/27879-ori.jpg': No such file or directory # error/blob.c/OpenBlob/2701' in /var/www/html/site/index.php:1064 Stack trace: #0 /var/www/html/site/index.php(1064): Imagick->__construct('../site/images...') #1 {main} thrown in /var/www/html/site/index.php on line 1064
Yes, the ImageMagick plugin is installed ;-)
At first I thought it might be a SELinux issue, as many of my other problems have been linked to that, but I'm not seeing any errors in the audit log, so that's not it.
So I thought of a permissions issue, because of the "Permission denied", so I tweaked those to death but to no avail. They currently are set to 777 for both folders and files, and STILL it won't save the picture!
As for ownership, files and folders are owned by user:user, with apache being a member of my user's group (so it should be able to access the folders and save an image within).
One little detail I should specify, that I think might be the heart of the problem (though it worked fine in my previous system) is that I have limited space on the /var partition, and since I expect the image library to grow quite a bit, I am actually storing the pictures in a different, larger partition, and symlinked the folder into /var/www/html/site:
$ ls -l /var/www/html/site/
lrwxrwxrwx. 1 user user 17 Oct 9 04:51 images -> /mnt/site/images
I tried to google the errors I get, but I'm only seeing problems that were resolved by chmod, which obviously is not doing anything here... Likewise, there are a few similar questions on stackoverflow, but none that really match my exact situation.
Side note: My Apache user is named 'apache', and there apparently is no php user in Fedora 23 (the service is started along with apache, so I guess apache is also the php user...)

Categories