-1

To print image in certain div, basically what I'm doing it's: echo first part of html page before that "div" (myGallery.html)/ then php script -> echo images from secure folder outside of public_html/ then second part of html (myGallery1.html) after specific "div". Please give me some basic idea how to do it in normal way.

<?php

$userMail = $_POST["email"] = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
$password = $_POST["pass"] = filter_var($_POST['password'], FILTER_SANITIZE_STRING);

$userMail = strtolower("$userMail");
$userMail = stripcslashes($userMail);
$password = stripcslashes($password);


$database = file_get_contents( "/home/customer/www/domain/System/sysData.json");
$obj = json_decode($database);
$dbusername = $obj->username;
$dbpassword = $obj->password;
$my_db = $obj->my_db;


$conn = new mysqli("localhost", $dbusername, $dbpassword, $my_db);
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT id, username, email, password FROM customers";
$result = $conn->query($sql);

$row = mysqli_fetch_array($result, MYSQLI_ASSOC);


if ($row["email"] == $userMail && $row["password"] == $password) {
    echo "Login Succes". "<br>";

$gal = file_get_contents( "/home/customer/www/domain/public_html/Log/myGallery.html");
echo $gal;

$dir = "/home/customer/www/domain/myGallery";
if (is_dir($dir)){
  if ($dh = opendir($dir)){
    while (($file = readdir($dh)) !== false){
      if ($file == '.' or $file == '..') continue;
          $path = '/home/customer/www/domain/myGallery/'. $file;
          $imgData = base64_encode(file_get_contents($path)); 
          $src = 'data: '.mime_content_type($path).';base64,'.$imgData; 
          echo "<img class=\"images\" src=\"$src\" data=\"$file\" >";
    }
    closedir($dh);
  }
};

$gal1 = file_get_contents( "/home/customer/www/domain/public_html/Log/myGallery1.html");
echo $gal1;

  
} else {
    echo "Failed to LogIn";
};

$conn->close();


?>

Sergiy
  • 11
  • 3
  • What's with `stripcslashes`? Why do you assing the output of `filter_var` to `$_POST`? Why do you use `filter_var` instead of `filter_input`? – Dharman Feb 18 '21 at 21:40
  • **Never store passwords in clear text or using MD5/SHA1!** Only store password hashes created using PHP's [`password_hash()`](https://php.net/manual/en/function.password-hash.php), which you can then verify using [`password_verify()`](https://php.net/manual/en/function.password-verify.php). Take a look at this post: [How to use password_hash](https://stackoverflow.com/q/30279321/1839439) and learn more about [bcrypt & password hashing in PHP](https://stackoverflow.com/a/6337021/1839439) – Dharman Feb 18 '21 at 21:40
  • Thank you guys for showing my newbie mistakes =) About the passwords - already in proc. Filter_var...just found peace of code((( What about my html page separation in 2 parts? – Sergiy Feb 18 '21 at 21:55
  • What isn't working and what is _normal way_? – AbraCadaver Feb 18 '21 at 22:12
  • You need something in `myGallery.html` to split on like a comment `` in between the divs or something. – AbraCadaver Feb 18 '21 at 22:14
  • Everything is working ok...just seems kinda weard. But yeah, it's separated in a place . Think it's ok to continue with this code? – Sergiy Feb 18 '21 at 22:24

1 Answers1

0

Like a simple templating system you need a way to determine where content should be placed. For something this simple, in myGallery.html just add a comment <!-- IMAGES --> or something that is unique like {{IMAGES}} or whatever as a marker:

<html>
<head>
</head>
<body>

<div>
<!-- IMAGES -->
</div>

</body>
</html>

Then you can do one of several things. You can split the file contents and echo when needed:

$gal = file_get_contents("/home/customer/www/domain/public_html/Log/myGallery.html");
$parts = explode('<!-- IMAGES -->', $gal);
echo $parts[0];
// loop and get image content
    // echo image content
// end loop
echo $parts[1];

Or modify your loop to store and not display the images, then replace the marker with the image tags:

// loop and get image content
    $images[] = "<img class=\"images\" src=\"$src\" data=\"$file\" >";
// end loop

$gal = file_get_contents("/home/customer/www/domain/public_html/Log/myGallery.html");
echo str_replace('<!-- IMAGES -->', implode($images), $gal);
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87