Jump to content

file_exists() not finding file that DOES exist


stevieontario

Recommended Posts

Morning Freaks,

 

Wierd little problem -- I have set up xampp sandboxes on two different computers. On one, the following script finds an xml file inside a folder and var_dump() outputs info about the file. On the other, nothing.

<?php
include "Functions1.php";
include "Functions2.php";

$filemoniker = '/home/ubuntu/public_html/myfolder/xmlfile.xml';
if (file_exists($filemoniker))
{echo "The file $filemoniker exists";} else {echo "The file $filemoniker does not exist";}
 foreach(glob('/home/ubuntu/public_html/myfolder/xmlfile.xml') as $filename) 
 {
 	 	$xmlname = basename($filename);
	echo "<pre>"; 
 	echo $xmlname;
 	echo "</pre>";
 $xml_file = simplexml_load_file($filename) or die("no data loaded");
 $rr = object2array($xml_file);
  print "<pre>";
  var_dump($rr);
 print "</pre>";
}
?>

As you can see, I wrote in a test script on lines 5-7. On one sandbox -- php version 5.4.7 -- that script echoes "The file [$filemoniker] exists."

 

In the second sandbox -- php v. 5.5.9 -- it echoes "The file [$filemoniker] does not exist."

 

I have checked the paths and the folder myfolder, and the file does exist. Needless to say, the rest of the script from foreach down returns nothing but a blank screen.

 

Any ideas?

Link to comment
Share on other sites

I wondered about that, and tried on the basis of searches for fixes to similar problems to use

eio_chmod('/home/ubuntu/public_html/myfolder/', 0777);

which returned "Fatal error: Call to undefined function eio_chmod()".

 

Tried

chmod('/home/ubuntu/public_html/myfolder/', 0777);

which returned "The file [$filemoniker] does not exist."

Link to comment
Share on other sites

I meant the file permission for the xml file itself, not for your directory. 

 

Judging by your last comment

 

 

When I run the script without the xmlfile name -- i.e. just the path -- then the 5.5.9 sandbox returns "The file [$filemoniker] exists."

PHP can access the directory just fine. The problem lies with PHP unable to read the xml file itself which is causing file_exists to return false and so your get the xml file does not exist message.

Link to comment
Share on other sites

well, I copied the file name (first from the Ubuntu folder and then from the Linux terminal, just to be sure) and pasted it into the code.

 

Neither works. It recognizes the path, but doesn't see the files inside.

 

Should have mentioned before, in case it matters: the sandbox in which the script runs fine is installed in Ubuntu 11.10; the wonky one is in Ubuntu 12.04.

Link to comment
Share on other sites

trq, you are right there seems to be no point for the foreach. That part of the script is a historical vestige of my original problem. The folder myfolder actually holds thirty or forty of xml files.

 

Works fine in Ubuntu 11.10 with PHP 5.4.7. But on my Ubuntu 12.04 system with PHP 5.5.9 the foreach was not yielding any of the info I wanted.

 

So I backtracked and tried file_exists() just to see if PHP was recognizing anything in that folder. I copied/pasted one of the files into the code just to be sure.

 

mac, I did as suggested and put the ini_set code right after the opening tag and got the same result.

Link to comment
Share on other sites

Sounds like UserDir mapping directive in apache is not enabled on the second machine.To enabe UserDir open up the apache conf file and change UserDir disable to UserDir public_html and restart the http server, then all requests from the browser to the http server and all php searching functions should be work. The URL request from the browser would be something like - http://example.com/~username/myfolder/test.xml

Link to comment
Share on other sites

thanks everyone for your help. What a puzzle.

 

trq, I own myfolder (at least I think I do)... I own the computer and aside from a user account I set up for my girlfriend I'm the only user.

 

Jazzman -- I assume you mean "httpd-userdir.conf"? There are two of them in my system for some reason. (The file "httpd.conf" refers to this file in the userdir section.)

 

But neither indicated that UserDir has been disabled.

Link to comment
Share on other sites

I put "localhost/ubuntu/myfolder" into the browser address bar, hit Enter, and got:

 

Access forbidden!

You don't have permission to access the requested object. It is either read-protected or not readable by the server.

If you think this is a server error, please contact the webmaster.

Error 403 localhost
Apache/2.4.7 (Unix) OpenSSL/1.0.1f PHP/5.5.9 mod_perl/2.0.8-dev Perl/v5.16.3

... which is weird, since in the folder its permission is given as drwx.

 

I contacted the webmaster, and he said he has no clue what's going on. (Sorry, it's been a long day and I thought I'd throw in a joke; the "webmaster" is me.)

Edited by stevieontario
Link to comment
Share on other sites

My suggestion:

 

Go to the /home/ubuntu/public_html directory.

 

Type "ls -l"

 

Copy the results here.

 

Type "ps -ef | grep httpd" or "ps -ef | grep apache2" (whichever returns results)

 

Copy the results here.

Link to comment
Share on other sites

To solve this issue try what @trq suggested ( somehow I skipped this comment) and set permissions to ubuntu home directory to be "chmod 755 /home/ubuntu/ -R" or much better to "chmod 750 /home/ubuntu/ -R", then add apache user to be a part of ubuntu user's group.

 

EDIT: His suggestion was to set these permissions to the sub directory, mine is to set these permissions to main user directory, otherwise the webserver won't be able to access files inside.  

Edited by jazzman1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.