WFS
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
→ Create Album
<?php
include_once "operation.php";
if(isset($_POST['btnAdd']))
$strName = trim(addslashes($_POST['txtName']));
$sqlQuery = " INSERT INTO album SET title = '$strName', status=1, created_at='".date('Y-m-
d')."', updated_at='".date('Y-m-d')."'";
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>
2
W
<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 Album</span></button>
</div>
</form>
</div>
</div>
</div>
</body>
</html>
Output:-
3
W
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)) != FALSE)
$arrResults[] = $arrSpecResult;
return $arrResults;
3
W
function fnRedirectUrl($strUrl)
if(!headers_sent())
header("Location:".$strUrl);
exit;
else
?>
<script type="text/javascript">
location.href = '<?php echo $strUrl; ?>';
</script>
<?php
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']);
3
W
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/");
//http://localhost/tybca/practical/program6/
?>
-Upload Photos :-
<?php
include_once "operation.php";
if(isset($_POST['btnAdd']))
/*echo '<pre>';
print_r($_POST);
print_r($_FILES);
die;*/
$strFileName = $_FILES['flPhoto']['name'];
$strFileToUpload = 'uploads/'.$strFileName;
3
W
move_uploaded_file($_FILES['flPhoto']['tmp_name'], $strFileToUpload);
$strName = trim(addslashes($_POST['txtName']));
$intAlbumId = trim(addslashes($_POST['cmbAlbum']));
$sqlQuery = " INSERT INTO photos SET title = '$strName',album_id=$intAlbumId,
photo='$strFileName',status=1, created_at='".date('Y-m-d')."', updated_at='".date('Y-m-
d')."'";
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>
3
W
<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>
3
W
<input type="file" class="input-control"
name="flPhoto" id="flPhoto" />
</li>
</ul>
<div class="button_container">
<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>
3
W
<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))
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; ?>" />
3
W
</li>
<?php
?>
</ul>
</div>
</div>
</div>
</body>
</html>
Gallery.py
#! D:\xampp\python.exe
print ("Content-type: text/html\n\n");
import subprocess
cmd = "D:\\BCA\\Software\\xampp27\\php\\php.exe D:\\BCA\\Software\\xampp27\\htdocs\\
php_practice\\Journal\\q6_album\\program6\\ind ex.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);
3
W
print(strHtmlOutput);
Output:-