Why does this not work on 5.6?

Web programming qustions, like PHP, ASP, ASP .NET, Perl etc. belong in this section.
Post Reply
Colbyt
Administrator
Posts: 1277
Joined: Fri Nov 08, 2019 12:11 pm
Latest blog post: The magical little can that replaces 4 pantry item...
Answers: 2
Reputation: 606
Location: Central Kentucky USA
Has thanked: 146 times
Been thanked: 39 times
Contact:

Why does this not work on 5.6?

Post by Colbyt »

I don't know the proper terminology so I will just post the fragment and ask the question.

Only recently did I upgrade localhost to php 7.x and what worked on 5.6 did not work on 7.

The following works fine on php7 but fails on a server running 5.6.x

Code: Select all

$filename = "images/$name-$page.jpg "; 
      //echo "<br>".$filename; // debug
       if (file_exists($filename)) {  echo "<p align='center'><img src='$filename'>";  } 
       else { echo "image does not exist";  }
In the older php version it was alway like:

Code: Select all

$filename = "images/".$name."-".$page.".jpg "; 
The latter also works on 7. The fail yesterday must have been a typo on my part


Colbyt
Reducing my online presence to do some other things.
User avatar
J. H. Rasmussen
Administrator
Posts: 1091
Joined: Thu Nov 07, 2019 9:13 pm
Latest blog post: Any Google Updates for May 2021?
Reputation: 519
Location: Copenhagen, Denmark
Has thanked: 25 times
Been thanked: 38 times
Contact:

Re: Why does this not work on 5.6?

Post by J. H. Rasmussen »

Since you are using a relative path "images/$name-$page.jpg", then it would depend on the php-script location, if you use a php-script to include another php-script that read that image, then it's the first php-script's path that will be prepended to path of the image.

If you want to make sure the image path is relative to the script where the image code is, then you could use something like:

Code: Select all

file_exists(dirname(__FILE__) . '/' . $filename)
You also need to remove the space between jpg " since a filename or path don't end with a space.


Colbyt
Administrator
Posts: 1277
Joined: Fri Nov 08, 2019 12:11 pm
Latest blog post: The magical little can that replaces 4 pantry item...
Answers: 2
Reputation: 606
Location: Central Kentucky USA
Has thanked: 146 times
Been thanked: 39 times
Contact:

Re: Why does this not work on 5.6?

Post by Colbyt »

Thanks for the reply.
This is a test script, a part of a much larger one, to verify that it work on a production server.
I'm too tired to deal with this right now but in reply to your reply:
The file is in the root of the of public_html/sitename/.
Images is a folder within that folder.
The script is located in that same folder.
It works perfectly on localhost but fails on 2 different servers.

I'm not seeing a space in jpg; but just in case here is the full test script from what is working on localhost.

Code: Select all

<?php
//error_reporting(E_ALL);
 // ini_set('display_errors', '1');
// localhost=PHP Version 7.4.1 server=version 5.6.x and then upgraded to 7.something
    $page="2";
    $name="Craps";
    $filename = "./images/".$name."-".$page.".jpg ";
    //$filename = "images/$name-$page.jpg "; 
    // $filename = $_SERVER['DOCUMENT_ROOT'] . "/kycasi-net/images/".$name."-".$page.".jpg "; // localhost tes
      echo "<br>".$filename; // debug
     //  if (file_exists($filename)) {  echo "<p align='center'><img src=".$filename.">";  } 
      if(file_exists($filename)) {  echo "<br>what is wrong with this script?<p align='center'><img src='$filename'>";  }  
       else { echo "image does not exist";  }
       echo "<br>";
       echo "<p align='center'><img src=".$filename.">"; 
       
?>
In the real script which is also working fine on localhost the variables $page and $name are retrieved from the DB.
If the page is 2 the image shows. If 1 it does not. If 3 or greater it does not because there is no Craps-3.jpg.


Colbyt
Reducing my online presence to do some other things.
Colbyt
Administrator
Posts: 1277
Joined: Fri Nov 08, 2019 12:11 pm
Latest blog post: The magical little can that replaces 4 pantry item...
Answers: 2
Reputation: 606
Location: Central Kentucky USA
Has thanked: 146 times
Been thanked: 39 times
Contact:

Re: Why does this not work on 5.6?

Post by Colbyt »

Answer found and it now works on one server. I have not tested it on the other yet.

Apparently xampp or xampp on windows does not cache the file-exists data but a Linux production does so at the beginning of the function one needs to add:

Code: Select all

clearstatcache();
Now whether the wrong thing was cached because of syntax error in the very first script run or not I can't say but it did solve the problem.

edit: fully tested and working on cPanel server and Centos CWP server.


Colbyt
Reducing my online presence to do some other things.
Post Reply

Return to “Web Programming”

Who is online

Users browsing this forum: No members and 15 guests