I'm very new to PHP, but as a start, I've made a file deleter / lister, depending on time. If you access the page and the server finds files that are older than approx. 10 days, it will delete the file. Otherwise, it will display the file. What I'm trying to do, is sort the files that are displayed after time, so the newest come first. I've tried a variety of different approaches + searched around here, but not been able to find something that works for exactly this. I gave it a shot with directly applying
arsort($dir)
but since it's an object, it wouldn't let me. Can't really see it as a good way to sort in the for each for each item.
function fileDelete(){
$dir = new DirectoryIterator(dirname(__FILE__));
$now = time();
$j = 0;
foreach($dir as $fileinfo){
if($fileinfo->getExtension() == "png"){
if($now - filemtime($fileinfo) >= (60 * 60 * 24 * 10)){
unlink($fileinfo);
echo $fileinfo . " deleted " . "<br>";
$j++;
} else {
$data = getImageSize($fileinfo);
$width = $data[0];
$height = $data[1];
?>
<img src="<?php echo $fileinfo ?>" width="<?php echo ($width/2) ?>" height="<?php echo ($height/2) ?>">
<?php }
}
}
if($j == 0){
return "No files deleted";
} else {
return "<br>" . $j . " files deleted. <br>";
}
}
Here is my code. I also tried to array_push() it into another array and sort that, but for some reason, it didn't work. I hope someone can give me best practice on this and sorry for the duplicate if you find any. I didn't.
I actually fixed my own problem:
I split up the code into two foreach-loops. Made all the checks in the first one, deleted the file and then displayed the file in the next foreach loop.
To answer it fully: It was $fil[0] I was looking for.
function fileDelete(){
$dir = new DirectoryIterator(dirname(__FILE__));
$now = time();
$j = 0;
$filer = array();
foreach($dir as $fileinfo){
if($fileinfo->getExtension() == "png"){
if($now - filemtime($fileinfo) >= (60 * 60 * 24 * 10)){
unlink($fileinfo);
echo $fileinfo . " deleted " . "<br>";
$j++;
} else {
$filer[$fileinfo->getMTime()][] = $fileinfo->getFilename();
}
}
}
arsort($filer);
foreach($filer as $fil){
$data = getImageSize($fil[0]);
$width = $data[0];
$height = $data[1];
?>
<img src="<?php echo $fil[0] ?>" width="<?php echo ($width/2) ?>" height="<?php echo ($height/2) ?>">
<?php }
if($j == 0){
return "No files deleted";
} else {
return "<br>" . $j . " files deleted. <br>";
}
}
Related
I want to include a link after the picture, to copy that picture into an other catalog.
The code scans a catalog for pictures and displays them on a page, newest on top.
$folder = $cam.'/grabs/';
$filetype = '*.*';
$files = glob($folder.$filetype);
if ($sort == 'Nye') {
usort($files, create_function('$b,$a', 'return filemtime($a) - filemtime($b);'));
}
$count = count($files);
echo '</font><table>';
echo "<font color='white'> $count bilder";
for ($i = 0; $i < $count; $i++) {
echo '<tr><td>';
echo '<a name="'.$i.'" href="#'.$i.'"><img src="'.$files[$i].'" /></a>';
echo substr($files[$i],strlen($folder),strpos($files[$i], '.')-strlen($folder));
echo ' - Save' ;
echo '</td></tr>';
}
echo '</table>';
?>
My problem is that i do not know how to get the filname instead of $i in movefile='.$i.
I have tried to put in $files but that gives me an error message.
Learned a lot today.
The glob( will take every picture and give it a number $i
The original .$i. Returns a line number. (As stated in one of the comments, thanks for the hint.)
So, to get the filename, you have to add $files before the $i to get the right one.
The answer was:
.$files[$i].
echo ' - Save' ;
So I'm creating a points system for my website which I want to change to an echo instead of the actual integer when shown on the users profile.
For example: When the integer is lower than 1000 it displays as the actual number (lets say: 645). But when it is between 1000 and 1100, it will display as '1k' and so on. What I've got so far does work, but displays incorrectly and does seem a bit of a waste of space.. Is there any way to do this in a much simpler; faster way?
Thanks!
code:
<?php
$points_disp = $user_data['points'];
if($points_disp < 1000){
echo $points_disp;
} else if ($points_disp >= 1000){
echo '1k';
} else if ($points_disp >= 1200){
echo '1.2k';
} else if ($points_disp >= 1400){
echo '1.4k';
} else if ($points_disp >= 1600){
echo '1.6k';
} else if ($points_disp >= 1800){
echo '1.8k';
} else if ($points_disp >= 2000){
echo '2k';
}
?>
Edit: I figured out an easier way to do this;
code (for anyone else who needs to do this):
<?php
$points_disp = $user_data['points'];
$fdigit = substr($points_disp, 0, 1);
$sdigit = substr($points_disp, 1, 1);
if ($points_disp < 1000){
echo $points_disp;
} else if ($points_disp >= 1000){
echo $fdigit . "." . $sdigit . "k";
}
echo $num;
?>
You can use switch case:
$points_disp = $user_data['points'];
switch(true)
{
case ($points_disp < 1000):
$num = $points_disp;
break;
case ($points_disp > 1000 && $points_disp < 1100 ):
$num = '1.2k';
break;
//...so on
}
echo $num;
Try this,
if($points_disp < 1000){
echo $points_disp;
} else if($points_disp >= 1000) {
echo round($points_disp/1000,1) . "K";
}
I am working on project which shows articles and this was done by article manager (a ready to use php script) but I have a problem, I want to show only four article titles and summaries from old list of article randomly which contains 10 article. Any idea how to achieve this process?
I have auto generated summary of article
<div class="a1">
<h3><a href={article_url}>{subject}</h3>
<p>{summary}<p></a>
</div>
When a new article is added the above code will generated and add into summary page. I want to add it to side of the main article page, where user can see only four article out of ten or more randomly.
<?php
$lines = file_get_contents('article_summary.html');
$input = array($lines);
$rand_keys = array_rand($input, 4);
echo $input[$rand_keys[0]] . "<br/>";
echo $input[$rand_keys[1]] . "<br/>";
echo $input[$rand_keys[2]] . "<br/>";
echo $input[$rand_keys[3]] . "<br/>";
?>
Thanks for your kindness.
Assuming I understood you correctly - a simple solution.
<?php
// Settings.
$articleSummariesLines = file('article_summary.html', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$showSummaries = 4;
// Check if given file is valid.
$validFile = ((count($articleSummariesLines) % 4 == 0) ? true : false);
if(!$validFile) {
die('Invalid file...');
}
// Count articles and check wether all those articles exist.
$countArticleSummaries = count($articleSummariesLines) / 4;
if($showSummaries > $countArticleSummaries) {
die('Can not display '. $showSummaries .' summaries. Only '. $countArticleSummaries .' available.');
}
// Generate random article indices.
$articleIndices = array();
while(true) {
if(count($articleIndices) < $showSummaries) {
$random = mt_rand(0, $countArticleSummaries - 1);
if(!in_array($random, $articleIndices)) {
$articleIndices[] = $random;
}
} else {
break;
}
}
// Display items.
for($i = 0; $i < $showSummaries; ++$i) {
$currentArticleId = $articleIndices[$i];
$content = '';
for($j = 0; $j < 4; ++$j) {
$content .= $articleSummariesLines[$currentArticleId * 4 + $j];
}
echo($content);
}
I've a problem that i can't solve. I think it's an easy fix, but after 3 hours of searching and trail-error. I've decided to ask the question over here:
This is my time function for a schedule application.
date_default_timezone_set('Europe/Amsterdam');
$current_time = time();
$unixtime = $current_time;
$time = date("Gi",$unixtime);
global $time;
function time_left($time, $active_class, $maxtime, $mintime){
if($time < $maxtime and $time > $mintime){
global $active_class;
$active_class = 'active';
echo 'succes!';
}
}
Here is my foreach loop, i loop through an array
foreach($rows as $row){
switch($hours){
case 1:
$t = '8:45 - 9:15';
$mintime = 845;
$maxtime = 914;
time_left($time, $maxtime, $mintime);
break;
case 2:
$t = '8:45 - 9:15';
$mintime = 845;
$maxtime = 914;
time_left($time, $maxtime, $mintime);
break;
/* etc.. etc.. etc... */
}
echo "<li class='" . $active_class ."'>";
echo "<div class='right'></div>";
echo "<div class='hour'>", $times, "</div><span class='divider-time'>|</span>";
$hours++;
$i = 0;
foreach($row[$day] as $r[1]){
$i++;
if ($i == 1) {
$class = 'vk';
} elseif ($i == 2) {
$class = 'lok';
} elseif ($i == 3) {
$class = 'doc';
}
echo "<span class='" . $class . "'>", $r[1], "</span>";
}
echo "</li>";
$class++;
}
I get the 'succes!' echo on the right location. But the active class is not working properly. The idea behind it is that the active class is only shown on one row. Now it searches for a match and everything behind it also gets the active class.
Thanks in advanced.
You should restart the $active_class variable in every iteration.
Otherwise, once it is set to active it won't change its value again.
foreach($rows as $row){
$active_class = '';
//YOUR CODE HERE
....
}
I have a php script that runs glob on a directory, and it returns all the images it finds in the directory, with an ad from google before all the images.
I would like it if I could use glob to load 10 of the images, then insert the javascript from googles ad services, and continue loading the images. So an ad every 10 images. Every attempt by me so far has failed spectacularly, and any help would be greatly appreciated!
Below is my PHP code
<?php
$manualwidth = $_GET['manwidth'];
$manualdir = $_GET['mandir'];
$manualmodel = $_GET['manurl'];
$manualurl = $manualdir . '/' . $manualmodel . '/';
$files = glob($manualurl .'{*.jpg,*.gif}', GLOB_BRACE);
for ($i=0; $i<count($files); $i++)
{
$num = $files[$i];
echo '<img src="'.$num.'" width="'.$manualwidth.'"><br>'." ";
}
?>
Echo the js every 10th iteration
<?php
$count = count($files);
for ($i=0; $i<$count; $i++)
{
if($i % 10 === 0) {
echo "google ads here";
}
$num = $files[$i];
echo '<img src="'.$num.'" width="'.$manualwidth.'"><br>'." ";
}
?>
How about putting the ad code into an html file in the directory:
ad.html
<script type="text/javascript>
//Code to run
</script>
//other ad stuff here
And then just include that file after every 10 images:
index.php
<?php
for($i = 1; $i <= count($files); $i++)
{
//Echo image code here
if($i%10 == 0)
include("ad.html");
}