PHP footer display file time stamp - php

For each page I am coding I have a foot that I use php include "footer.php" in all of them. Below is the foot.php code. I need my code to be able to give the correct time stamp for each file not one speific file. So in the end each page should have a different time stamp because I am workign on each file at different times becasue the website I am making is for a class assignments. And of course each assignment is being coded at on different days. If this doesn't make since let me know.
<footer align="center">
<hr width="900px" size="2" noshade="noshade" color="black" align="center">
<div style="padding:0 30px">
<p>Validated by:</p>
<img src="https://www.w3.org/Icons/valid-xhtml11" alt="Valid XHTML 1.1" height="31" width="88">
<img src="https://jigsaw.w3.org/css-validator/images/vcss-blue" alt="Valid CSS">
<p>Last modified:
<?php
$timestamp = filemtime(__FILE__);
$date_time = new DateTime();
$date_time->setTimezone(new DateTimeZone("America/New_York"));
$date_time->setTimestamp($timestamp);
echo $date_time->format("F j Y g:i a.");
?>
</p>
</div>
</footer>
I asked for help on this before but not working out.

First we need a function that reads all files of a folder in an association
reads out the timestamp values
function getFileModificationTimes(string $directory): array {
$files = scandir($directory);
$modificationTimes = [];
foreach ($files as $file) {
if ($file === '.' || $file === '..') {
continue;
}
$path = $directory . '/' . $file;
if (!is_file($path)) {
continue;
}
$modificationTimes[$file] = filemtime($path);
}
return $modificationTimes;
}
and a second function that determines the largest/most recent value
function getMaxModificationTime(array $modificationTimes) {
$maxModificationTime = 0;
$maxFile = '';
foreach ($modificationTimes as $file => $modificationTime) {
if ($modificationTime > $maxModificationTime) {
$maxModificationTime = $modificationTime;
$maxFile = $file;
}
}
return [$maxFile, $maxModificationTime];
}
the footer now shows the date of the newest file from the folder
<?php
$modificationTimes = getFileModificationTimes('/path/to/directory');
list($maxFile, $maxModificationTime) = getMaxModificationTime($modificationTimes);
?>
<footer align="center">
<hr width="900px" size="2" noshade="noshade" color="black" align="center">
<div style="padding:0 30px">
<p>Validated by:</p>
<img src="https://www.w3.org/Icons/valid-xhtml11" alt="Valid XHTML 1.1" height="31" width="88">
<img src="https://jigsaw.w3.org/css-validator/images/vcss-blue" alt="Valid CSS">
<p>The file with the most recent modification time is <?=$maxFile?>, which was modified at <?=date('Y-m-d H:i:s', $maxModificationTime)?>.
</p>
</div>
</footer>

If you want to show the modification time of the file including the footer, then remove $timestamp = filemtime(__FILE__); from the footer file and put it before the include().
Eg:
$timestamp = filemtime(__FILE__);
include('footer.php');

Related

How do I hide the .php extension when listing files in php?

I have the following code to show the files in a folder in php.
<html>
<head>
<?php require '/opt/bitnami/apps/wordpress/htdocs/head.php';?>
</head>
<body>
<br>
<br>
<br>
<br>
<?php
if ($handle = opendir('.')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != ".."&& $file != "posts.php") {
$thelist .= ''.$file.'<hr>';
}
}
closedir($handle);
}
?>
<center>
<h1>files</h1>
<br>
<br>
<div style=background-color:white;color:black;>
<br>
<p1 style="font-size: 25px;"><?php echo $thelist;?></p1><br>
<hr>
</div>
</center>
</body>
</html>
So, I do not want the .php file extension to not show up when they are being listed. For example if there is a file called test.php, I want it to just list test. What code should I add/change to make this work?
Thanks for any help.
You can try using str_replace()
str_replace(".php","",$file);

Getting Images With PHP And Display In Script

I have a blog and I want to be able to show images that are stored in a folder using a simple PHP script. Here is the PHP:
<?php
$folder = "$segment_url/files/photos/$post_year/$post_id/";
$filetype = ".jpg";
$files = glob($folder.$filetype, GLOB_BRACE);
foreach ($files as $file)
{
echo "
<img class=\"galleryPhoto\" src=\"$file\" /> // PROBLEM 1
";
}
?>
<?php echo $folder; ?>
<?php
$display_content = "
<div class=\"pageSection text\">
$content_intro
</div>
<div class=\"contentSpace\"></div>
<div class=\"pageSection text\">
// PROBLEM 2
</div>
<div class=\"contentSpace\"></div>
<div class=\"pageSection text\">
$content_conclusion
</div>
";
?>
In reference to the above code:
Problem 1: Even though the path to the images folder is correct, no images are being displayed.
Problem 2: How do I get the echoed content to be displayed here rather than at the top of the page?
For Problem 1 : You are missing to use full image path, otherwise search will search in current directory. Try like this.
<img class=\"galleryPhoto\" src=\"$folder/$file\" /> // PROBLEM 1

simple opendir() with pagination

I am very new in php, though was wable to make opendir() work (just show list of files in a table) now I have a problem of creating a pagination. Sorry I have been working with tutorials out there but can't make them work.I quick help would be very much appreciated.
here's my code:
$dir = 'files/memoranda';
$exclude = array('.','..','.htaccess');
$y = (isset($_GET['y']))? strtolower($_GET['y']) : '';
$res = opendir($dir);
while(false!== ($file = readdir($res))) {
if(strpos(strtolower($file),$y)!== false &&!in_array($file,$exclude)) {
echo'
<div class="Row">
<div class="Cell">
<h4>'.$file.'</h4>
</div>
<div id="Cell" style="text-align: center;vertical-align:middle;border-top:solid 1px #cecece;">
<h4><img style="text-align:center;padding-top:6px;" src="icon_download.png" width="30px" height="30px" ></h4>
</div>
</div>
';
}
}

Use Field Values For Filepath

I wonder whether someone may be able to help me please.
I'm trying to load an xml file with the following filepath:
/UploadedFiles/username/location/files.xml
What I've been trying to do for some time, is to use the values of two forms fields, 'username' and 'location' and incorporate them into the filepath, but I just can't seem to get this right.
I just wondered whether someone could someone perhaps tell me how I take these values and use them in the filepath.
Many thanks
POST AMENDMENT
I thought I could solve my issues by simply changing the file path, hence my original post, but I fear my lack of PHP knowledge as let me down and I suspect the issues I have are deeper within my code.
Please find below more detail.
The script below shows how I initially save the images.
'upload.php'
<?php
require_once 'Includes/gallery_helper.php';
require_once 'ImageUploaderPHP/UploadHandler.class.php';
$galleryPath = 'UploadedFiles/';
function onFileUploaded($uploadedFile) {
global $galleryPath;
$packageFields = $uploadedFile->getPackage()->getPackageFields();
$username=$packageFields["username"];
$locationid=$packageFields["locationid"];
$username = preg_replace('/[^a-z0-9_\-\.()\[\]{}]/i', '_', $_POST['username']);
$location = preg_replace('/[^a-z0-9_\-\.()\[\]{}]/i', '_', $_POST['locationid']);
$dirName = preg_replace('/[^a-z0-9_\-\.()\[\]{}]/i', '_', $_POST['folder']);
$absGalleryPath = realpath($galleryPath) . DIRECTORY_SEPARATOR . $username . DIRECTORY_SEPARATOR . $location . DIRECTORY_SEPARATOR;
$absThumbnailsPath = $absGalleryPath . 'Thumbnails' . DIRECTORY_SEPARATOR;
if (!is_dir($absGalleryPath)) mkdir($absGalleryPath, 0777, true);
chmod($absGalleryPath, 0777);
if (!is_dir($absGalleryPath . $dirName)) mkdir($absGalleryPath . $dirName, 0777, true);
chmod($absGalleryPath . $dirName, 0777);
if ($uploadedFile->getPackage()->getPackageIndex() == 0 && $uploadedFile->getIndex() == 0)
initGallery($absGalleryPath, $absThumbnailsPath, FALSE);
$originalFileName = $uploadedFile->getSourceName();
$files = $uploadedFile->getConvertedFiles();
$sourceFileName = getSafeFileName($absGalleryPath, $originalFileName);
$sourceFile = $files[0];
if ($sourceFile) $sourceFile->moveTo($absGalleryPath . $sourceFileName);
$thumbnailFileName = getSafeFileName($absThumbnailsPath, $originalFileName);
$thumbnailFile = $files[1];
if ($thumbnailFile) $thumbnailFile->moveTo($absThumbnailsPath . $thumbnailFileName);
$descriptions = new DOMDocument('1.0', 'utf-8');
$descriptions->load($absGalleryPath . 'files.xml');
$xmlFile = $descriptions->createElement('file');
// <-- please check the following line
$xmlFile->setAttribute('name', $_POST['folder'] . '/' . $originalFileName);
$xmlFile->setAttribute('source', $sourceFileName);
$xmlFile->setAttribute('size', $uploadedFile->getSourceSize());
$xmlFile->setAttribute('originalname', $originalFileName);
$xmlFile->setAttribute('thumbnail', $thumbnailFileName);
$xmlFile->setAttribute('description', $uploadedFile->getDescription());
$xmlFile->setAttribute('username', $username);
$xmlFile->setAttribute('locationid', $locationid);
$xmlFile->setAttribute('folder', $dirName);
$descriptions->documentElement->appendChild($xmlFile);
$descriptions->save($absGalleryPath . 'files.xml');
}
$uh = new UploadHandler();
$uh->setFileUploadedCallback('onFileUploaded');
$uh->processRequest();
?>
This script produces the following file structure:
UploadedFiles (Pre-existing folder)
'username' (Subfolder containing 'location' folder)
'location' (Subfolder containing original image, 'files.xml' and 'Thumbnails' folder)
'Thumbnails' (Subfolder containing thumbnail of original image)
NB. The 'username' and 'location folder names are derived from the current user and location values.
I then come onto my issue with the creation of the image gallery.
The code below is my initial 'gallery.php' script. I need to change the top PHP section of the script to match the new folder structure i.e. UploadedFiles/'username'/'location'/Thumbnails and UploadedFiles/'username'/'location'.files.xml
'gallery.php'
<?php
$galleryPath = 'UploadedFiles/';
$thumbnailsPath = $galleryPath . 'Thumbnails/';
$absGalleryPath = realpath($galleryPath) . DIRECTORY_SEPARATOR;
$descriptions = new DOMDocument('1.0');
$descriptions->load($absGalleryPath . 'files.xml');
?>
<head>
<title>Gallery</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link href="Libraries/fancybox/jquery.fancybox-1.3.1.css" rel="stylesheet" type="text/css" />
<link href="Styles/style.css" rel="stylesheet" type="text/css" />
<!--[if IE]>
<link href="Styles/ie.css" rel="stylesheet" type="text/css" />
<![endif]-->
<script src="Libraries/jquery/jquery-1.4.3.min.js" type="text/javascript"></script>
<script src="Libraries/fancybox/jquery.fancybox-1.3.1.pack.js" type="text/javascript"></script>
<script type="text/javascript">
$(function() { $('a.fancybox').fancybox(); });
</script>
<style type="text/css">
<!--
.style1 {
font-size: 14px;
margin-right: 110px;
}
.style4 {font-size: 12px}
-->
</style>
</head>
<body style="font-family: Calibri; color: #505050; font-size: 9px; border-bottom-width: thin; margin-top: 5px; margin-left: -476px; margin-right: 1px; margin-bottom: -10px;">
<div align="right" class="style1"> <a href = "imagefolders.php" /> View Uploaded Images In Folder Structure <a/> ← View All Uploaded Images </div>
<form id="gallery" class="page">
<div id="container">
<div id="center">
<div class="aB">
<div class="aB-B">
<?php if ('Uploaded files' != $current['title']) :?>
<?php endif;?>
<div class="demo">
<input name="username" type="text" id="username" value="IRHM73" />
<input name="locationid" type="text" id="locationid" value="1" />
<div class="inner">
<div class="container">
<div class="gallery">
<ul class="gallery-image-list">
<?php for ($i = 0; $i < $descriptions->documentElement->childNodes->length; $i++) :
$xmlFile = $descriptions->documentElement->childNodes->item($i);
$name = htmlentities($xmlFile->getAttribute('originalname'), ENT_COMPAT, 'UTF-8');
$description = htmlentities($xmlFile->getAttribute('description'), ENT_COMPAT, 'UTF-8');
$folder = htmlentities($xmlFile->getAttribute('folder'), ENT_COMPAT, 'UTF-8');
$source = $galleryPath . rawurlencode($xmlFile->getAttribute('source'));
$thumbnail = $thumbnailsPath . rawurlencode($xmlFile->getAttribute('thumbnail'));
?>
<li class="item">
<a class="fancybox" target="_blank" rel="original" href="<?php echo $source; ?>"><img class="preview"
alt="<?php echo $name; ?>" src="<?php echo $thumbnail; ?>" /></a></li>
<p><span class="style4"><b>Image Description:</b> <?php echo htmlentities($xmlFile->getAttribute('description'));?> <br />
<b>Contained in folder:</b> <?php echo htmlentities($xmlFile->getAttribute('folder'));?> </span><br />
<?php endfor; ?>
</li>
</p>
</ul>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="aB-a"> </div>
</div>
</div>
</div>
</form>
</body>
</html>
I admit to being at a real loss at how to solve this, I don't whether it's my 'gallery.php' script that needs to change or my 'upload.php'
Any help would be so gratefully received.
Kind regards
OK, let's say you have this values : username, location.
Then, (if I get your question right) in the responding script (the one receiving the form action), try this :
<?php
$username = $_GET['username'];
$location = $_GET['location'];
$filepath = "/UploadedFiles/$username/$location/files.xml";
?>
Hint (2 ways of concatenating strings) :
$filepath = "/UploadedFiles/$username/$location/files.xml";
OR
$filepath = "/UploadedFiles/".$username."/".$location."/files.xml";
This will work regardless you are using post or get method in your form . Just make sure you choose field name username and location for input text html element.
$filepath = "/UploadedFiles/{$_REQUEST['username']}/{$_REQUEST['location']}/files.xml"
You have to define them as variable and include within the string
$username = "user1";
$location = "userlocation1";
$url = "/UploadedFiles/$username/$location/files.xml";
//Next you might want to verify the location
if(is_file($url)) {
//do your operation....
}

trying to move a div before another div without screwing up php that I do not understand :)

I am a designer/front-end developer but I have to move the div with a class of "span-4" before the div with a class of "span-20." The developer who wrote the php is not available to ask how to not mess up his php markup.
Thank you in advance for any help!
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>THRIVE</title>
<?php include("header.php"); ?>
</head>
<body>
<div class="container">
<div id="thumbnails" class="span-20 last">
<?php
$accessoriesDir = "images/accessories/";
$collectionCount = 1;
while(is_dir($accessoriesDir . $collectionCount . "/"))
{
$collection_dir = $accessoriesDir . $collectionCount . "/";
if ($season_dh = opendir($collection_dir))
{
$accsArray = array();
$accsCount = 0;
while (($accessoryFolder = readdir($season_dh)) !== false)
{
if(filetype($collection_dir . $accessoryFolder) == "dir" && strlen($accessoryFolder) > 2)
$accsArray[$accsCount++] = $accessoryFolder;
}
closedir($season_dh);
?>
<div id="collection<?php echo $collectionCount?>" class="thumbsHolder <?php if($collectionCount !== 1) echo 'hidden'?>">
<div class="cats span-4">
<?php
foreach($accsArray as $index => $accessory)
{
$class = "accessory-tab";
if($index == 0)
{
$class .= " selected";
}
echo "<a id='".strtolower($accessory)."-tab' class='".$class."'>".$accessory."</a><br/>";
}
?>
</div>
<?php
foreach($accsArray as $index => $accessory)
{
?>
<div id="<?php echo strtolower($accessory);?>-view" class="view <?php if($index != 0) echo 'hidden';?>">
<?php
$imageCount = 0;
$accessoryDir = $collection_dir.$accessory."/";
$dh = opendir($accessoryDir);
while (($file = readdir($dh)) !== false && $imageCount < 8)
{
if(filetype($accessoryDir . $file) == "file" && substr($file, -4) == ".jpg")
{
?>
<div class="thumbnailExpander <?php if($imageCount % 2 !== 0)echo 'expandLeft';?> <?php if($imageCount >= 4)echo 'bottom';?>"
style="left:<?php echo ($imageCount % 4) * (317-132) ?>px;top:<?php echo 110+((132+53)*intval($imageCount/4));?>px;">
<!--div class="thumbAnchor"-->
<img src="<?php echo $accessoryDir . $file ?>" style=""/>
<!--/div-->
</div>
<?php
$imageCount++;
}
}
?>
</div><!-- #accessory-view -->
<?php
closedir($dh);
}
?>
</div><!-- .thumbsHolder -->
<?php
}
$collectionCount++;
}
?>
</div><!-- #thumbnails -->
<?php include("nav.php"); ?>
<div id="debug" style="color:white"></div>
</div>
</body>
</html>
Refactoring this code so that the span-4 is displayed first is not going to be an easy task. It's going to be near impossible (and not advisable) if you have no PHP experience.
I would suggest that you use CSS to move the span-4 element around on the displayed page. That way you'll not need to touch the PHP. Even better; when the PHP developer returns get them to refactor this so that the display code is not entwined with the business logic (it's a horrible practice that makes questions like this impossible to answer).
Hope that helps some.
Try these two steps:
(1) Cut everything from the first <?php line after span-20 all the way down to the closing tag of the span-4... and paste it above the span-20 div. This includes all the garbage + the span-4 div in its entirety.
(2) In the code you just pasted, find this line:
<div id="collection<?php echo $collectionCount?>" class="thumbsHolder <?php if($collectionCount !== 1) echo 'hidden'?>">
... and cut & paste it back in the space it where came from.
That should work for you, unless I need to get my eyes checked.
EDIT: There's a chance it won't work, depending on whether the span-4 really needs to be a child of #collection. If that's the case, you'll need to tuck into the CSS and separate the two, or somehow carry the parent id out with the span-4.

Categories