PHP: Where to put rsort? - php

$count = 0;
if ($handle = opendir('./arkiv/')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
$count++;
print("".basename($file, ".php")."<br />\n");
}
}
echo '<br /><br />Tilbake';
closedir($handle);
}
This is the code I'm using, and I'm trying to figure out where to but 'rsort', but this is very new to me, can anyone help me?

Possible use can be like
$count = 0;
$fileArray = array();
if ($handle = opendir('./arkiv/'))
{
while (false !== ($file = readdir($handle)))
{
if ($file != "." && $file != "..")
{
$count++;
$fileArray[] = $file;
}
}
rsort($fileArray);
foreach($fileArray as $file)
{
print("".basename($file, ".php")."<br />\n");
}
echo '<br /><br />Tilbake';
closedir($handle);
}
Give it a try ....
GOOD LUCK !!!

Related

PHP copy images according to time

I want to copy images from one folder to another on server, now I use this code:
<?php
function read_dir($dir)
{
$list = array();
if (is_dir($dir))
{
if ($handle = opendir($dir))
{
while (false !== ($file = readdir($handle)))
{
if ($file != "." && $file != "..")
{
$list[] = $file;
}
}
}
closedir($handle);
}
return $list;
}
$src="oldfolder";
$dest="newfolder";
$list= read_dir($src);
foreach($list as $key => $val)
{
copy("$src/$val","$dest/$val");
}
echo "Done";
?>
But I need to copy just images selected by time - for example images uploaded between "now" and 5 min. ago..
Can anyone help?
Thanks
Now my PHP is like below. It seems that it run with "Done" result, but nothing is copied..
<?php
function read_dir($dir)
{
$list = array();
if (is_dir($dir))
{
if ($handle = opendir($dir))
{
while (false !== ($file = readdir($handle)))
{
$fpath = 'oldfolder'.$file;
if (file_exists($fpath)) {
if($file != "." && $file != ".." &&
DateTime::createFromFormat('U', filemtime($file)) < new DateTime("-5
minutes"))
{
$list[] = $file;
}
}
}
}
closedir($handle);
}
return $list;
}
$src="oldfolder";
$dest="newfolder";
$list= read_dir($src);
foreach($list as $key => $val)
{
copy("$src/$val","$dest/$val");
}
echo "Done";
?>
So this is my code, that works for me well - copy images between folders according to time (- 5 sec) set by other code in "time.txt" file:
<?php
function read_dir($dir)
{
$list = array();
if (is_dir($dir))
{
if ($handle = opendir($dir))
{
while (false !== ($file = readdir($handle)))
{
$fpath = 'oldfolder/'.$file;
if (file_exists($fpath)) {
$subor = fopen("./time.txt", "r");
$cas_txt=fgets($subor, 11);
fclose($subor);
$cas_zac = DateTime::createFromFormat('U', $cas_txt)->modify('-5 seconds');
if ($file != "." && $file != ".." && DateTime::createFromFormat('U',
filemtime($fpath)) > $cas_zac)
{
$list[] = $file;
}
}
}
}
closedir($handle);
}
return $list;
}
$src="oldfolder";
$dest="newfolder";
$list= read_dir($src);
foreach($list as $key => $val)
{
//copy file to new folder
copy("$src/$val","$dest/$val");
}
echo "Done";
?>
I have two more questions:
Please how can I rotate images in 180° by or after copy? Is it possible in one php code?
How can I send multiple files - images from my code - like an attachments by mail in php?
Thanks for your help.
You should use the filemtime function

php rename error: The system cannot find the file specified. (code: 2)

<?php
$dir = opendir('C:\Users\Prometheus\Desktop\milkmaid');
$i = 1;
// loop through all the files in the directory
while (false !== ($file = readdir($dir)))
{
if ($file != "." && $file != "..") {
$newName = $i.'.mp4';
$oldname = $file;
rename($oldname, $newName);
$i++;
}
}
?>
when i run above script, i am getting following error:
The system cannot find the file specified. (code: 2)
$dir is not a string. You can't concatenate $file with it. You will need to put the directory in a separate variable, and not forget to put a / in between directory and filename.
Adding $dir in the rename() works for me
<?php
$dir = opendir('C:\Users\Prometheus\Desktop\milkmaid');
$i = 1;
// loop through all the files in the directory
while (false !== ($file = readdir($dir)))
{
if ($file != "." && $file != "..") {
$newName = $i.'.mp4';
$oldname = $file;
rename($dir.$oldname, $dir.$newName);
$i++;
}
}
?>
Use it like this :-
$directory = '/public_html/testfolder/';
$i=1;
if ($handle = opendir($directory)) {
while (false !== ($fileName = readdir($handle))) {
$newName = $i.'.mp4';
rename($directory . $fileName, $directory . $newName);
$i++:
}
closedir($handle);
}
This worked for me
<?php
$counter = 1;
$dir = 'D:\files'; //path of folder
if ($handle = opendir($dir))
{
while (false !== ($fileName = readdir($handle)))
{
if($fileName != '.' && $fileName != '..')
{
$newName = $counter . " - " . $fileName;
rename($dir."/".$fileName, $dir."/".$newName);
$counter++;
}
}
closedir($handle);
}
?>

How to change the style of a few echoed pieces of text?

<?php
$blacklist = array("no.html", "one.php");
if ($handle = opendir('.')) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != ".." && !in_array($entry, $blacklist)) {
echo "<a href='$entry'><li id='file'>$entry\n</li></a>";
}
}
closedir($handle);
}
?>
What happens here is that every file in the directory (excluding those in the blacklist) get echoed out like so:
1.html
2.html
3.html
4.html
5.html
6.html
7.html
8.html
However, is there any way to style certain elements? For example, every 3 echo's, make the text bigger and the color red.
E.g.
1.html
2.html
3.html
4.html
5.html
6.html
7.html
8.html
Try this. I think it will help you
$i = 1;
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != ".." && !in_array($entry, $blacklist)) {
if($i%3 == 0){
echo "<a href='$entry'><strong><li id='file'>$entry</li></strong></a>";
}else{
echo "<a href='$entry'><li id='file'>$entry</li></a>";
}
}
$i++;
}
Something like this with the counter initialized will do the job.
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != ".." && !in_array($entry, $blacklist)) {
if ($i%3==0)
{
//the echo with the style
}
else
{
echo "<a href='$entry'><li id='file'>$entry\n</li></a>";
}
}
$i++
}

opendir array exclude file from results

The code below will select all of my php files from the named folder and then shuffle them and echo 10 results on my page, the folder contains an index.php file which i would like to be excluded from the results.
<?php
if ($handle = opendir('../folder/')) {
$fileTab = array();
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
$fileTab[] = $file;
}
}
closedir($handle);
shuffle($fileTab);
foreach(array_slice($fileTab, 0, 10) as $file) {
$title = str_replace('-', ' ', pathinfo($file, PATHINFO_FILENAME));
$thelist .= '<p>'.$title.'</p>';
}
}
?>
<?=$thelist?>
I have found a code to exclude index.php but I'm not sure how to incorporate it into my code above.
<?php
$random = array_values( preg_grep( '/^((?!index.php).)*$/', glob("../folder/*.php") ) );
$answer = $random[mt_rand(0, count($random) -1)];
include ($answer);
?>
Why not just modify the line
if ($file != "." && $file != "..") {
to
if ($file != "." && $file != ".." && $file != 'index.php') {
An approach based on glob() instead of readdir():
<?php
$files = glob('../folder/*.php');
shuffle($files);
$selection = array_slice($files, 0, 11);
foreach ($selection as $file) {
$file = basename($file);
if ($file == 'index.php') continue;
$title = str_replace('-', ' ', pathinfo($file, PATHINFO_FILENAME));
// ...
}
You can use
$it = new FilesystemIterator(__DIR__, FilesystemIterator::SKIP_DOTS);
$it = new RegexIterator($it, '/.php$/i', RegexIterator::MATCH);
$exclude = array("index.php");
foreach ( $it as $splFileInfo ) {
if (in_array($splFileInfo->getBasename(), $exclude))
continue;
// Do other stuff
}
Or Simply
$files = array_filter(glob(__DIR__ . "/*.php"), function ($v) {
return false === strpos($v, 'index.php');
});
You can exclude it while you reading directory content (like you do with '.' and '..'):
if ($handle = opendir('../folder/')) {
$fileTab = array();
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != ".." && $file != "index.php") {
$fileTab[] = $file;
}
}
closedir($handle);
shuffle($fileTab);
foreach(array_slice($fileTab, 0, 10) as $file) {
$title = str_replace('-', ' ', pathinfo($file, PATHINFO_FILENAME));
$thelist .= '<p>'.$title.'</p>';
}
}
?>
while (false !== ($file = readdir($handle)))
{
if ($file != "." && $file != ".." && $file != 'index.php')
$fileTab[] = $file;
}
you could just change this line if ($file != "." && $file != "..") { to if ($file != "." && $file != ".." && $file != 'index.php') {
The code you found replaces your cumbersome directory reading loop.
And it should be just:
$files = preg_grep('~/index\.php$~', glob("../folder/*.php"), PREG_GREP_INVERT);
Get 10 elements as before:
$files = array_slice($files, 0, 10);
Then output those.

Strip the extension from a file

I have a code that is working as i want it to apart from 1 problem.
You can see the result here http://test.whatanswered.com/health/what-can-a-first-aider-do.php on the right below "Related Articles" showing dead links.
The following is the HTML that should be displayed.
<p>Name of the page</p>
I would like to strip the dashes and php as above "Name of the page" but it also strips the dashes and php from the url.
The code is:
<?php
if ($handle = opendir('health')) {
$fileTab = array();
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
$fileTab[$file] = strtr(pathinfo($file, PATHINFO_FILENAME), '-', ' ');
}
}
closedir($handle);
shuffle($fileTab);
foreach(array_slice($fileTab, 0, 10) as $file => $health) {
$thelist .= '<p>'.$health.'</p>';
}
}
?>
<?=$thelist?>
I've refactored your code a little - the $fileTab array now just stores filenames and converting this to a title happens at the point of display:
if ($handle = opendir('health')) {
$fileTab = array();
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
$fileTab[] = $file;
}
}
closedir($handle);
shuffle($fileTab);
foreach(array_slice($fileTab, 0, 10) as $file) {
$title = str_replace('-', ' ', pathinfo($file, PATHINFO_FILENAME));
$thelist .= '<p>'.$title.'</p>';
}
}

Categories