Jump to content
Theboy22

cum fac sa mi afiseze fotografiile in tabel in ordinea cronologica cum au fost uplodate?

Recommended Posts

am urmatorul script:

<?php
$path = "upload";

if(isset($_POST['file']) && is_array($_POST['file']))
{
	foreach($_POST['file'] as $file)
	{	
		unlink($path . "/" . $file) or die("Failed to <strong class='highlight'>delete</strong> file");
	}
	header("location: " . $_SERVER['REQUEST_URI']); //redirect after deleting files so the user can refresh without that resending post info message
}
?>
<form name="form1" method="post">
<?php

$path = "upload";
$dir_handle = @opendir($path) or die("Unable to open folder");

while (false !== ($file = readdir($dir_handle))) 
{

if($file == "index.php")
continue;
if($file == ".")
continue;
if($file == "..")
continue;
echo "<input type='CHECKBOX' name='file[]' value='$file'>";

echo "<a href='upload/$file' target='_blank'>";
echo "<img src='upload/$file' alt='$file' style='height: 100px; width: 100px;'/></a>&nbsp;&nbsp;";

	


}
closedir($dir_handle);

?>
<br>
<input type="submit" name="Delete" value="Delete">
</form>

 

scriptul imi afiseaza fotografiile din folderul UPLOAD, dar as vrea sa le afiseze in ordine cronologica cum au fost upldate .

Mentionez fotograriile cand sunt uplodate in folderul UPLOAD au numele la data si ora de upload

Exemplu: 2022-03-30-14-33-33_62444e1d7cf18.jpg.

 

Imi cer scuze ca primul meu topic a inceput cu o cerere de ajutor.

Va multumesc anticipat

Link to comment
Share on other sites

Parcurgi lista de fisiere (acel while al tau), extragi data din fiecare nume (explode dupa _), o convertesti in timestamp (strtotime) si formezi un array cu fisierele.

Ex.: $FILES_LIST[$file_timestamp] = $file;

Apoi sortezi $FILES_LIST, apoi parcurgi array-ul si-l afisezi. In loc de $file, cand formezi array-ul de fisiere, poti sa-l faci un pic mai "complex", adica de forma:

<?php
$FILES_LIST[$file_timestamp] = array(
  "name"=>$file_name, // aici trebuie sa extragi numele din path banuiesc
  "path"=>$file,
  "size"=>$file_size, // aici iei filesize-ul dupa $file
  // etc
);
?>

 

Edited by Wav3
  • Upvote 1
Link to comment
Share on other sites

Ai putea inlocui 

$dir_handle = @opendir($path) or die("Unable to open folder");
while (false !== ($file = readdir($dir_handle))) 
{
if($file == "index.php")
continue;
if($file == ".")
continue;
if($file == "..")
continue;
echo "<input type='CHECKBOX' name='file[]' value='$file'>";
echo "<a href='upload/$file' target='_blank'>";
echo "<img src='upload/$file' alt='$file' style='height: 100px; width: 100px;'/></a>&nbsp;&nbsp;";
}
closedir($dir_handle);

Cu

function sortByTime($a, $b) {
    $datePuzzleA = explode("-", explode("_", $a)[0]);
    $dateA = strtotime(implode("-", array_slice($datePuzzleA, 0, 3))." ".implode(":", array_slice($datePuzzleA, 3, 3)));
    $datePuzzleB = explode("-", explode("_", $b)[0]);
    $dateB = strtotime(implode("-", array_slice($datePuzzleB, 0, 3))." ".implode(":", array_slice($datePuzzleB, 3, 3)));
    if ($a === $b) {
        return 0;
    }
    return $b > $a ? -1 : 1;
}

$files = array_diff(scandir($path), array('.', '..', 'index.php'));
usort($files, "sortByTime");
foreach($files as $file) {
    echo "<input type='CHECKBOX' name='file[]' value='$file'>";
    echo "<a href='upload/$file' target='_blank'>";
    echo "<img src='upload/$file' alt='$file' style='height: 100px; width: 100px;'/></a>&nbsp;&nbsp;";
}

Desi nu cred ca e cea mai eleganta solutie.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...