You are on page 1of 6

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

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
include_once "operation.php";
if (isset($_POST['btnAdd'])) {
$strName = trim(addslashes($_POST['txtName']));
$createdAt = date('Y-m-d');
$updatedAt = date('Y-m-d');

// Prepare and execute the SQL query


$sqlQuery = "INSERT INTO album (title, status, created_at, updated_at)
VALUES ('$strName', 1, '$createdAt', '$updatedAt')";

fnFireQuery($sqlQuery);

$strListingUrl = 'album.php?m=1';
fnRedirectUrl($strListingUrl);
}
?>
<html>
<head>
<title>Add Album</title>
<link rel="stylesheet" href="css/custom.css" />
</head>
<body>
<div class="wrapper">
<div class="listing_container">
<h1>Add Album</h1>
<div class="add_album_form form_container">
<form name="frmAddAlbum" id="frmAddAlbum" method="POST"
action="add_album.php">
<ul> <li>
<label for="txtName">Name</label>
<input type="text" class="input-control" name="txtName" id="txtName" />
</li>
</ul>
<div class="button_container">
<button type="submit" name="btnAdd"><span>Save
18 | P a g e
Div:-A Web Framework & Services Roll No:-202345027

Album</span></button>
</div>
</form>
</div>
</div>
</div>
</body>
</html>

Output:-

Operation.php :-

<?php
include_once "config.php";
function fnConnectDb()
{
$objCon = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
return $objCon;
}
function fnFireQuery($sqlQuery)
{
$objCon = fnConnectDb();
return mysqli_query($objCon, $sqlQuery);
}
function fnGetResults($sqlQuery)
{
$arrResults = array();
$resResults = fnFireQuery($sqlQuery);

while ($arrSpecResult = mysqli_fetch_assoc($resResults)) {


$arrResults[] = $arrSpecResult;
}

return $arrResults;
}

function fnRedirectUrl($strUrl)
19 | P a g e
Div:-A Web Framework & Services Roll No:-202345027

{
if (!headers_sent()) {
header("Location:" . $strUrl);
exit;
} else {
echo '<script type="text/javascript">location.href = "' . $strUrl . '";</script>';
exit;
}
}
function fnPhotoAlbum($intAlbumId)
{
$sqlQuery = "SELECT title FROM album WHERE id=$intAlbumId";
$arrAlbum = fnGetResults($sqlQuery);

if (is_array($arrAlbum) && count($arrAlbum)) {


$arrSpecAlbum = $arrAlbum[0];
return trim($arrSpecAlbum['title']);
}
return 'NA';
}
?>

Config.php:-
<?php
define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASS', '');
define('DB_NAME', 'stud_1');
define('SITE_URL', 'http://localhost/php_practice/Journal/q6_album/program6/');
?>

Upload Photos :-
<?php
include_once "operation.php";
if (isset($_POST['btnAdd'])) {
$strFileName = $_FILES['flPhoto']['name'];
$strFileToUpload = 'uploads/' . $strFileName;
move_uploaded_file($_FILES['flPhoto']['tmp_name'], $strFileToUpload);
$strName = trim(addslashes($_POST['txtName']));
$intAlbumId = trim(addslashes($_POST['cmbAlbum']));
$sqlQuery = "INSERT INTO photos (title, album_id, photo, status, created_at,
updated_at)
VALUES ('$strName', $intAlbumId, '$strFileName', 1, '" . date('Y-m-d') . "', '" .
date('Y-m-d') . "')";
20 | P a g e
Div:-A Web Framework & Services Roll No:-202345027

fnFireQuery($sqlQuery);
$strListingUrl = 'album.php?m=4';
fnRedirectUrl($strListingUrl);
}
?>
<html>
<head>
<title>Add Photo</title>
<link rel="stylesheet" href="css/custom.css" />
</head>
<body>
<div class="wrapper">
<div class="listing_container">
<h1>Add Photo</h1>
<div class="add_photo_form form_container">
<form name="frmAddPhoto" id="frmAddPhoto" method="POST"
action="add_photo.php" enctype="multipart/form-data">
<ul>
<li>
<label for="txtName">Title</label>
<input type="text" class="input-control" name="txtName" id="txtName" />
</li>
<li>
<label for="cmbAlbum">Album</label>
<select name="cmbAlbum" id="cmbAlbum">
<?php
$sqlQuery = "SELECT * FROM album";
$arrAlbums = fnGetResults($sqlQuery);

if (is_array($arrAlbums) && count($arrAlbums)) {


foreach ($arrAlbums as $key => $arrSpecAlbum) {
?>
<option value="<?php echo $arrSpecAlbum['id']; ?>"><?php echo
$arrSpecAlbum['title']; ?></option>
<?php
}}
?>
</select>
</li>
<li>
<label for="flPhoto">Photo</label>
<input type="file" class="input-control" name="flPhoto" id="flPhoto" />
</li>
</ul>
<div class="button_container">

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

<button type="submit" name="btnAdd"><span>Save


Photo</span></button>
</div>
</form>
</div>
</div>
</div>
</body>
</html>

Output:-

Photo Gallery(Index.php)

<?php
include_once "operation.php";
?>
<html>
<head>
<title>Photo Gallery</title>
<link rel="stylesheet" href="css/custom.css" />
</head>
<body>
<div class="gallery_wrapper">
<div class="button_container">
<a href="album.php">Manage Albums</a>
</div>
<?php include_once "message.php"; ?>
<div class="photo_gallery_container">
<h1>Photo Gallery</h1>
<div class="listing">
<ul>
<?php
$sqlQuery = "SELECT * FROM photos";
$arrPhotos = fnGetResults($sqlQuery);
if (is_array($arrPhotos) && count($arrPhotos)) {
22 | P a g e
Div:-A Web Framework & Services Roll No:-202345027

foreach ($arrPhotos as $key => $arrSpecPhoto) {


$strPhotoName = $arrSpecPhoto['photo'];
$strPhotoUrl = SITE_URL . 'uploads/' . $strPhotoName;
$strTitle = trim($arrSpecPhoto['title']);
?>
<li>
<img class="album_photo" src="<?php echo $strPhotoUrl; ?>"
title="<?php echo $strTitle; ?>" alt="<?php echo $strTitle; ?>" />
</li>
<?php
}
}
?>
</ul>
</div>
</div>
</div>
</body>
</html>

Gallery.py:-

#! D:\xampp\python.exe
print("Content-type: text/html\n\n") # Print the HTTP header
import subprocess
cmd = "D:\\BCA\\Software\\xampp27\\php\\php.exe
D:\\BCA\\Software\\xampp27\\htdocs\\php_practice\\Journal\\q6_album\\program6\\i
ndex.php"
proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, universal_newlines=True)
script_response, script_err = proc.communicate()
arrOutput = script_response.splitlines()
strHtmlOutput = "".join(arrOutput)
print(strHtmlOutput)

Output:-

23 | P a g e

You might also like