0

I'm trying to retrieve all images from a folder in the server to my web page through PHP.
At local server, they're displaying fine but at main server, images are not displaying and showing error 404 not found

even though the images are there.

Please help me to get rid of this. The images are in Big Rock server.

Here is my code:

$filename = $_POST['file'];
echo '<table class="table1" border="0" width="100%" >';
echo "<tr>";
echo "<td class='td1'>";
$images = glob($_SERVER["DOCUMENT_ROOT"].
  "/admin/gallery/".$filename.
  "/*.*");
$count = 0;
foreach($images as $image) {
  echo '<a href="'.$image.
  '" target="_blank"><img src="'.$image.
  '"class="img-rounded mySlides" width="200" height="150"/></a><br />';
  if ($count < 3) {
    $count++;
    echo "</td>";
    echo "<td class='td1'>";
  } else {
    echo "</tr>";
    $count = 0;
    echo "<tr>";
    echo "<td class='td1'>";
  }
}
alessandrio
  • 4,282
  • 2
  • 29
  • 40
  • Please check the relative path for the image. Check the example here [https://stackoverflow.com/questions/44212488/cant-find-correct-relative-paths-of-css-background-image/44212527?noredirect=1#comment75437135_44212527] – Rohit Kumar May 28 '17 at 10:37

4 Answers4

0

The HTTP 404 Not Found Error means that the webpage you were trying to reach could not be found on the server. It is a Client-side Error which means that either the page has been removed or moved and the URL was not changed accordingly, or that you typed in the URL incorrectly. Give extensions in giving url.

0

There is no issue with the permission on the file. The error is due to mismatch of the Relative Path of the image. 404 means the resource doesn't exist.

You can check my answer here [can't find correct relative paths of CSS background-image

Rohit Kumar
  • 776
  • 3
  • 21
  • I have checked all types of path definitions but i couldn't fix the problem – rahul bommaraju May 28 '17 at 11:17
  • @rahulbommaraju If you can share the path on the console. If you don't want to disclose website use XXX instead. – Rohit Kumar May 28 '17 at 11:37
  • The paths should of the image should be something like http://yourwebsite/admin/gallery/abc.png if not you share the Page Inspect Path. – Rohit Kumar May 28 '17 at 11:43
  • the path which i have tried is http://example.com/admin/gallery. It is just showing image icons but image is not displaying – rahul bommaraju May 28 '17 at 17:48
  • @rahulbommaraju If you can try with https://www.w3schools.com/html/html5.gif in the above the image name is missing – Rohit Kumar May 29 '17 at 02:58
  • i changed this glob($_SERVER["DOCUMENT_ROOT"]."/admin/gallery/".$filename."/*.*"); to glob("example.com/admin/gallery/".$filename."/*.*"); even i'm getting error .Please see above code – rahul bommaraju May 29 '17 at 05:52
  • @rahulbommaraju Please share the output of the $images before you are using foreach statement. – Rohit Kumar May 29 '17 at 13:46
0

The problem is glob($_SERVER["DOCUMENT_ROOT"]."/admin/gallery/".$filename."/.").$_SERVER["DOCUMENT_ROOT"] will take path as home/xxxx/public_html/admin/gallery.which cannot open in the browser.The image will open only by the path like this "http://example.com/folder/img.jpg".so i have cropped the path till admin folder like home/xxx/public_html/admin/gallery to admin/gallery and in the img src i have given src="http://example.com/".$image as shown in the code

$filename=$_POST['file'];
echo '<table class="table1" border="0" width="100%" >';
echo "<tr>";
echo "<td class='td1'>";
$curdir=getcwd();
$images = glob($curdir."/admin/gallery/".$filename."/*.*");
$count=0;
foreach($images as $image) 
{
$dir="http://screccs.com".str_replace($curdir,"",$image);
 echo "<a href='".$dir."' target='_blank'>";     echo "<img src='".$dir."'class='img-rounded' width='200' height='150'/></a><br />";
if($count<3)
{
$count++;
echo "</td>";
echo "<td class='td1'>";
}
else
{
echo "</tr>";
$count=0;
echo "<tr>";
echo "<td class='td1'>";
}
}
echo '</tr>';
echo '</table>';
}
-1

I believe issue is related to folder permissions. Give proper read permission and it will work.

  • @Hitanshu-Malhotra there is no issue with the permission, the error clearly says 404. This is an issue of Relative Paths. – Rohit Kumar May 28 '17 at 10:35
  • @rahulbommaraju Press F12 on the Page on the browser move to the console tab you can check for the error. You will be able to find the image src. – Rohit Kumar May 28 '17 at 10:51
  • yes it is showing error:Failed to load resource the server responded with a status of 404 (Not Found) – rahul bommaraju May 28 '17 at 10:57