You are on page 1of 6

Div:-A Web Framework & Services Roll No:-20234506059

QUESTION:5
Write a python program to perform Count Visitors of Page and call it
using PHP

Code:-
Count_Visitors.py:-
import sys;
if sys.argv[1]:
intCounter = sys.argv[1]
else:
intCounter = 1;
print(int(intCounter)+1);

Count_Visitors.php:-

<?php
if(!isset($_COOKIE['visitor_count'])){
$strComand = escapeshellcmd("python visitor_count.py 1");
$intCounter = shell_exec($strComand);
setcookie("visitor_count",$intCounter, time() + 86400);
echo "Total Visitor:".$intCounter;}
else{
$intCounter = $_COOKIE['visitor_count'];
$strComand = escapeshellcmd("python visitor_count.py $intCounter");
$intCounter = shell_exec($strComand);
setcookie("visitor_count",$intCounter, time() + 86400);
echo "Total Visitor:".$intCounter; }
?>

Output:-

18 | P a g e
Div:-A Web Framework & Services Roll No:-20234506059

QUESTION:6
Write a PHP script to create Photo Album Application and call it using
python.
Create Album
Upload Photos in Album
Display Album in Gallery

Code:-

Create Album:-

<?php
if (isset($_POST["create_album"])) {
$albumName = $_POST["album_name"];

if (!empty($albumName)) {
$albumDirectory = "C:/xampp/htdocs/Journal/albums/$albumName";

if (!is_dir($albumDirectory)) {
if (mkdir($albumDirectory, 0777, true)) {
echo "Album '$albumName' created successfully.";
} else {
echo "Error creating album directory.";
}
} else {
echo "Album '$albumName' already exists.";
}
} else {
echo "Album name cannot be empty.";
}
}
?>

<!DOCTYPE html>
<html>
<head>
<title>Create Album</title>
</head>
<body>
<h1>Create Album</h1>
<form action="create_album_process.php" method="POST">
<label for="album_name">Album Name:</label>
<input type="text" name="album_name" required><br><br>
<input type="submit" name="create_album" value="Create Album">
</form>
</body>

19 | P a g e
Div:-A Web Framework & Services Roll No:-20234506059

</html>

Uploads Photos in Album:-

<?php
if (isset($_POST["upload_photos"])) {
$albumName = $_POST["album_name"];
$targetDirectory = "C:/xampp/htdocs/Journal/albums/$albumName/";

if (!is_dir($targetDirectory)) {
mkdir($targetDirectory, 0777, true);
}

if (isset($_FILES["photos"])) {
foreach ($_FILES["photos"]["tmp_name"] as $key => $tmp_name) {
$photoName = $_FILES["photos"]["name"][$key];
$targetFile = $targetDirectory . $photoName;
$fileExtension = strtolower(pathinfo($targetFile, PATHINFO_EXTENSION));

$allowedExtensions = ['jpg', 'jpeg', 'png', 'gif'];

if (in_array($fileExtension, $allowedExtensions)) {
$uniqueFilename = uniqid() . "_" . $photoName;
$targetFile = $targetDirectory . $uniqueFilename;

if (move_uploaded_file($_FILES["photos"]["tmp_name"][$key], $targetFile)) {
echo "Photo '$uniqueFilename' uploaded successfully.";
} else {
echo "Error uploading photo '$photoName'.";
}
} else {
echo "Error: Invalid file type for '$photoName'. Only JPG, JPEG, PNG, and GIF files
are allowed.";
}
}
}
}
?>

<!DOCTYPE html>
<html>
<head>
<title>Upload Photos</title>
</head>
<body>
<h1>Upload Photos</h1>
<form action="upload_photos_logic.php" method="POST" enctype="multipart/form-

20 | P a g e
Div:-A Web Framework & Services Roll No:-20234506059

data">
<label for="album_name">Select Album:</label>
<select name="album_name" required>
<?php
$albumDirectory = "C:/xampp/htdocs/Journal/albums";
$albums = scandir($albumDirectory);
foreach ($albums as $album) {
if ($album != "." && $album != ".." && is_dir("$albumDirectory/$album")) {
echo "<option value='$album'>$album</option>";
}
}
?>
</select><br><br>
<input type="file" name="photos[]" multiple accept="image/*"><br><br>
<input type="submit" name="upload_photos" value="Upload Photos">
</form>
</body>
</html>
Display Album in Gallery:-

<!DOCTYPE html>
<html>
<head>
<title>Album Gallery</title>
</head>
<body>
<h1>Album Gallery</h1>
<h2>Select an Album:</h2>
<form action="view_album.php" method="POST">
<select name="album_name" required>
<?php
$albumDirectory = "C:/xampp/htdocs/Journal/albums";
$albums = scandir($albumDirectory);
foreach ($albums as $album) {
if ($album != "." && $album != ".." && is_dir("$albumDirectory/$album")) {
echo "<option value='$album'>$album</option>";
}
}
?>
</select>
<input type="submit" name="show_album" value="Show Album">
</form>

<?php
if (isset($_POST["show_album"])) {
$selectedAlbum = $_POST["album_name"];
$albumPath = "C:/xampp/htdocs/Journal/albums/$selectedAlbum";

21 | P a g e
Div:-A Web Framework & Services Roll No:-20234506059

if (!is_dir($albumPath)) {
echo "Album directory not found.";
} else {
$photos = scandir($albumPath);
foreach ($photos as $photo) {
if ($photo != "." && $photo != "..") {
$fileInfo = pathinfo("$albumPath/$photo");
$extension = strtolower($fileInfo['extension']);
if (in_array($extension, ['jpg', 'jpeg', 'png', 'gif'])) {
echo "<div class='image-container'>";
echo "<p>Image Type: $extension</p>";
echo "<img src='$albumPath/$photo' alt='$photo'>";
echo "</div>";
}
}
}
}
}
?>
</body>
</html>

Subprocess.py:-

import subprocess

php_script_path = "C:/xampp/htdocs/Journal/view_album.php"

result = subprocess.run(["php", php_script_path], capture_output=True, text=True)

if result.returncode == 0:
print("PHP script executed successfully.")
print("Output:")
print(result.stdout)
else:
print("Error executing PHP script.")
print("Error Output:")
print(result.stderr)

22 | P a g e
Div:-A Web Framework & Services Roll No:-20234506059

Output :-

23 | P a g e

You might also like