Jump to content

Recommended Posts

I'm using Win 8, php 5.3.13 on a new pc for the first time.

 

The following simple if test should return EITHER of the choices (or so I do

beleive)

 

But when I run it on my new Windiows 8 PC for the first time it does

neither. Strange!

 

The line echo "$info_test_file<br><br>" ; does return a correct result

but the if test doesn't seem to.

================================

<?php
echo "test<br><br>" ;

$info_test_file ="../php/info_test.txt" ;

echo "$info_test_file<br><br>" ;

if (file_exists($info_test_file)) {
    echo "<font color=\"#FFFFFF\">File test DOES exist!</font><br />" ;
    $we_are_local = "1" ;
}  else {
    echo "<font color=\"#FFFFFF\">File test DOES NOT exist!</font><br />" ;
}
 

?>

=========================

 

eatc7402

Link to comment
https://forums.phpfreaks.com/topic/282950-simple-if-test-does-not-seem-to-work/
Share on other sites

You are outputting the text in white text color! Is your page background white?

echo "<font color=\"#FFFFFF\">File test DOES exist!</font><br />" ;
 echo "<font color=\"#FFFFFF\">File test DOES NOT exist!</font><br />" 

If it is white you wont see any thing!

Edited by Ch0cu3r

Not sure if this is the case here but I have encountered servers where adding a "../" in front of an address causes it to not work at all.

 

You have:

$info_test_file ="../php/info_test.txt" ;

Just to test, try removing the ../

 

Like this:

$info_test_file ="php/info_test.txt" ;

I know it sounds silly but that actually matters on my GoDaddy server. It won't locate files if there is a ../ in front of addresses.

Edited by Nightasy

Nope, I'm serious. But I'm also still learning. I always figured there was a reason for it but I just remove the ../ and everything works as a result so I never looked into it. Ya know what, it also occurred to me that the server at my last college had the same issue. I had to remove the ../ on that server too.

Edited by Nightasy

the path should either be the absolute path on the server, or else a path relative to the script being executed. "../" means "start where the script is being executed and go up one dir level". So based on the path in the OP, that file should be located at:

 

/full/server/path/script.php <-- the script that is running

/full/server/php/info_test.txt <-- this is where the file should be, based on the path given

 

what the actual server path is ("/full/server/path/") depends on how the server and file structure is setup. If you are on shared hosting then you likely do not have full root access and as far as your script is concerned, the root dir starts at your account's directory, which is usually the public html directory (usually named htdocs or public or public_html or similar) or one dir level above that. But regardless it will still look for it starting from there. Anyways, point is, the important thing is where it is located relative to the script that is running.

Example, please :)

 

If you are asking me for an example, I suppose this would suffice. This is from the index.php page on my most recent project.

<div id="introvideodiv">
	<video poster="videos/1.jpg" preload="auto" controls>
		<source src="videos/placeholder.mp4" type='video/mp4; codecs="avc1.42E01E, mp4a.40.2"' />
	</video>

If you'll notice, I have no ../ in the addresses for the poster and the video. If I add the ../ they won't work at all. No poster would show and no video would be located. My GoDaddy server is a shared server as far as I know and this is probably why I can't add the ../ in the addresses. I just figured I would mention it here because that little issue on my server stumped me for a couple hours when I first got the server through GoDaddy. Now my testing server is WAMP and it requires the ../ so I just do live test now in a password protected section (using sessions) on my server as it became a pain to keep changing that little detail on every page.

 

Needless to say, though, aside from the white text on a white background my only assumption is that the address to the text file is not correct for whatever the reason. Be it the ../ or it needing an absolute path. On a side note however, the issue would also occur if for some reason the php folder is not permitting access. Just brain storming here. Perhaps an issue with the folder permissions or .htaccess restrictions?

Edited by Nightasy

well that's client-side code so "root" means the root public dir that browsers can access.

 

So for example if the page that code is on is http://www.yoursite.com/path/to/page.php and you did this:

 

<video poster="../videos/1.jpg" preload="auto" controls>
		<source src="../videos/placeholder.mp4" type='video/mp4; codecs="avc1.42E01E, mp4a.40.2"' />
	</video>
the browser would attempt to look for them at http://www.yoursite.com/path/videos/1.jpg and http://www.yoursite.com/path/videos/placeholder.mp4.jpg

 

But, if your page is on http://www.yoursite.com/page.php (the public root), and you prefix with "../" then it will still resolve to the root public dir, so doing "../videos/1.jpg" would be the same as http://www.yoursite.com/videos/1.jpg

 

So basically the page that code is on must not be in the public root; it must have been http://www.yoursite.com/path/to/whatever/page.php and you expected your files to be located at http://www.yoursite.com/path/to/whatever/videos/1.jpg but it resolved to http://www.yoursite.com/path/to/videos/1.jpgso the "../" was indeed messing things up.

Well, I suppose that explains it. Like I said I never looked into it but yea, my entire site is inside of a folder and uses a splash screen index page at the root. So I guess that explains why I had the issue on my new server and why I had it in college too. In college I had to put my site in a folder due to the way they taught some restrictions with .htaccess and folder permissions. Perhaps this is the case for eatc7402, I'm happy to have learned what you just told me though. I always wondered why that was an issue for my current server (and in my college course too). Learn something new everyday. :tease-03:

Ah, relative path. I get it.

Actually both of the filenames you had were relative. You can tell because they don't start with a slash (or *:\ for Windows).

 

That ../ matters because you're changing the path to the file. It's as if you had

$address = "221B Baker Street";
$address = "21B Baker Street";
Only one of them will get you where you want to go. Edited by requinix

Yea, I completely understand it. In my case, adding a ../ basically leaves out the folder that the page is in, since it is not in the root folder. I completely get it now, I just never looked into why and now I know. Adding a ../ would send it to my root/videos folder instead of the root/folder_my_page_is_in/videos folder. It makes sense.

Edited by Nightasy
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.