add looping link on an image - php

I am trying to add links on the cabinet photo
So each drawer will have a link that will redirect to another page when clicked.
So when I have more than 3 links a new cabinet will appear.
All the folders are directly from my directory from my computer.
$dir = 'C:\xampp\htdocs\OJT\IRMS\TestFiles';
$filecounter = (count(scandir($dir))-2);//4
$numberofcabinets = ceil($filecounter/3); //2
echo $numberofcabinets;
if ($handle = opendir('TestFiles')) {
for($cabinetcounter=0;$cabinetcounter<$numberofcabinets;$cabinetcounter++){ //for,//cabinetimg
echo "<div class='folder'>
<img src='img/Cabinet.jpg' width='200' height='170'>";
for($drawer=0;$drawer<=3;$drawer++){ //for
while (false !== ($entry = readdir($handle))) { //start of listing file name
if ($entry != "." && $entry != "..") { //start of '..' exclusion
echo "<div class='drawer$drawer'><a href='search.php?entry=$entry'>$entry</a></div>"; //content
}
}
} //for
echo "</div>";
} //for
}
closedir($handle);

Related

PHP/HTML - issue with form handling - unlink() warning

I'm trying to allow user to delete images from a folder on server through html form and PHP.
Here's my html form markup along with PHP script generating list images from the folder mentioned before. I've added checkboxes with path+filename as value.
<form action="delete.php" method="POST">
<div id="formlist">
<?php
$path = ".";
$dh = opendir($path);
$i=1;
$images = glob($path."*.png");
while (($file = readdir($dh)) !== false) {
if($file != "." && $file != ".." && $file != "index.php" && $file != "form.css" && $file != ".htaccess" && $file != "error_log" && $file != "cgi-bin") {
echo "<div class='formshow'><a href='$path/$file' data-lightbox='Formularze' data-title='$file'><img class='formimg' src='$path/$file' width='500px'/></a><input type='checkbox' name='deleteform' value='$path/$file'></div>";
$i++;
}
}
closedir($dh);
?>
</div>
<input type="submit" value="Usun zaznaczone formularze">
</form>
Now, here's my delete.php file:
<?php
$path = ".";
$dh = opendir($path);
$i=1;
$deletepath = glob('deleteform');
while (($file = readdir($dh)) !== false) {
unlink($deletepath);
$i++;
}
?>
I keep this error:
Warning: unlink() expects parameter 1 to be a valid path, array given
I'm quite green with PHP, so I decided to ask you guys - how may I make this work? Should i unserialize() it and add [0], [1] counters?
To delete all itens in a folder user this:
$directory = "folder/";
if ($cat_handle = opendir($directory)) {
while (false !== ($entry = readdir($cat_handle))) {
#unlink($directory.$entry);
}
closedir($cat_handle);
}
You need delete itens or folder?
if you need delete specific item:
$file_name = "fulano.jpg";
if ($handle = opendir('folder/')) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
if($entry == $file_name){
#unlink('folder/'.$name);
}
}
}
closedir($handle);
}
I believe it works no seu caso, change to accept array now.

how can i add child node to the parents nodes in php?

I am reading a rootpath and listing all the folder. I have to add some child node to every folder programatically ?
<?php
$rootpath = 'D:/Storage/';
if ($handle = opendir($rootpath)) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
echo "<li><a href=/hi.php?dev=$entry&action=viewcam>$entry</a>";
}
}
closedir($handle);
?>
I am getting like
- folder1
- folder2
but I want like
- folder1
Appple
orange
- folder2
Appple
orange
Any suggestion ?
There is no need to specifically "create a node" on the server side for this. All you have to do is output the html markup you want to have:
<?php
$rootpath = 'D:/Storage/';
if ($handle = opendir($rootpath)) {
echo "<ul>\n";
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
echo "<li>$entry</li>\n";
echo "<ul>\n";
foreach (['Apple', 'orange'] as $fruit) {
echo "<li>".$fruit."</li>\n";
}
echo "</ul>\n";
}
}
echo "</ul>\n";
}
closedir($handle);
?>
(I fixed a few minor issues with your code on-the-fly...)
Obviously this is just an example to show the basic approach.

Search for a folder and and get the content of the files inside

I'm trying to search for a folder and retrieve the files inside of the folder (get content) I'm able to search for the folder using the follow code but I can't pass from there I can't see the content an retrieve the files inside. The files inside will be txt files and I would like to be able to open and see then.
How can achieve what i want? Thank you.
<?php
$dirname = "C:\windows";//Directory to search in. *Must have a trailing slash*
$findme = $_POST["search"];
$dir = opendir($dirname);
while(false != ($file = readdir($dir))){//Loop for every item in the directory.
if(($file != ".") and ($file != "..") and ($file != ".DS_Store") and ($file !=
"search.php"))//Exclude these files from the search
{
$pos = stripos($file, $findme);
if ($pos !== false){
$thereisafile = true;//Tell the script something was found.
echo'' . $file . '<br>';
}else{
}
}
}
if (!isset($thereisafile)){
echo "Nothing was found.";//Tell the user nothing was found.
echo '<img src="yourimagehere.jpg"/>';//Display an image, when nothing was found.
}
?>
New code
<?php
$dirname = "C:\\Windows\\";//Directory to search in. *Must have a trailing slash*
$findme = 'maxlink'; //$_POST["search"];
$files = scandir($dirname);
foreach ($files AS $file)
{
if ($file == '.' or $file == '..' or $file == '.DS_Store' or $file == 'search.php') continue;
if (stripos($file, $findme) !== false)
{
$found = true;
echo 'FOUND FILE ' . $file . '<hr>';
echo 'OPENING IT:<br>';
echo file_get_contents($dirname . $file);
echo '<hr>';
}
else
{
echo 'not found: ' . $file . '<br>';
}
}
if (!isset($found))
{
echo "Nothing was found.";//Tell the user nothing was found.
echo '<img src="yourimagehere.jpg"/>';//Display an image, when nothing was found.
}
The following code uses a recursive function for searching the directory. I hope it’ll solve your problem.
function scandir_r($dir){
$files = array_diff(scandir($dir), array(".", ".."));
$arr = array();
foreach($files as $file){
$arr[] = $dir.DIRECTORY_SEPARATOR.$file;
if(is_dir($dir.DIRECTORY_SEPARATOR.$file)){
$arr = array_merge($arr, scandir_r($dir.DIRECTORY_SEPARATOR.$file));
}
}
return($arr);
}
$dirname = "C:\windows";
$findme = "/".preg_quote($_POST["search"], "/")."/";
$files = preg_grep($findme, scandir_r($dirname));
if(sizeof($files)){
foreach($files as $file){
$_file = $dirname.DIRECTORY_SEPARATOR.$file;
echo "$file<br/>";
}
}
else{
echo "Nothing was found.";
echo "<img src=\"yourimagehere.jpg\"/>";
}

A minor issue while iteration of array in php.Guidance please

I have several files in a directory.I want to display all those filenames with the extension .txt and .jpeg
<?php
if ($handle = opendir("/home/work/collections/utils/")) {
while (false !== ($file = readdir($handle))) {
if ($file == '.' || $file == '..') {
continue;
}
$actual_file=pathinfo("/etrade/home/collections/utils");
if (($actual_file["extension"]== "txt") ||
($actual_file["extension"]== "jpg") ||
($actual_file["extension"]== "pdf")) {
//Require changes here.Dont know how to iterate and get the list of files
echo "<td>"."\n"." $actual_file['basename']."</a></td>";
}
}
closedir($handle);
}
Please help me on how to iterate and get the list of files .For instance I want all files with jpg extension in a seperate column and pdf files in a seperate column(since I am going to display in a table)
See if this does what you want (EDITED):
<?php
$ignoreFiles = array('.','..'); // Items to ignore in the directory
$allowedExtensions = array('txt','jpg','pdf'); // File extensions to display
$files = array();
$max = 0;
if ($handle = opendir("/home/work/collections/utils/")) {
while (false !== ($file = readdir($handle))) {
if (in_array($file, $ignoreFiles)) {
continue; // Skip items to ignore
}
// A simple(ish) way of getting a files extension
$extension = strtolower(array_pop($exploded = explode('.',$file)));
if (in_array($extension, $allowedExtensions)) { // Check if file extension is in allow list
$files[$extension][] = $file; // Create an array of each file type
if (count($files[$extension]) > $max) $max = count($files[$extension]); // Store the maximum column length
}
}
closedir($handle);
}
// Start the table
echo "<table>\n";
// Column headers
echo " <tr>\n";
foreach ($files as $extension => $data) {
echo " <th>$extension</th>\n";
}
echo " </tr>\n";
// Table data
for ($i = 0; $i < $max; $i++) {
echo " <tr>\n";
foreach ($files as $data) {
if (isset($data[$i])) {
echo " <td>$data[$i]</td>\n";
} else {
echo " <td />\n";
}
}
echo " </tr>\n";
}
// End the table
echo "</table>";
If you just want to display two lists of files (it's not clear what part you're having trouble with from your question) can't you just store the filenames in an array?
You don't seem to be getting the file details - you're getting the pathinfo for /etrade/home/collections/utils, but you never add the file name to it.
<?php
if ($handle = opendir("/home/work/collections/utils/")) {
while (false !== ($file = readdir($handle))) {
if ($file == '.' || $file == '..') {
continue;
}
$actual_file=pathinfo($file);
switch ($actual_file['extension'])
{
case ('jpg'):
$jpegfiles[] = $actual_file;
break;
case ('pdf'):
$pdffiles[] = $actual_file;
break;
}
}
closedir($handle);
}
echo "JPG files:"
foreach($jpegfiles as $file)
{
echo $file['basename'];
}
echo "PDF Files:"
foreach($pdffiles as $file)
{
echo $file['basename'];
}
?>
Obviously you can be cleverer with the arrays, and have use multi-dimensional arrays and do away with the switch if you want.

How to display images for delete

I have the following code that deletes the images from a folder but it does not display the images on the page and also whats worse is that it display all folder and files within the given folder.
I only want the images to be deleted not any folders with the given folder name.
my code is:
<?php
$path = "../imagefolder";
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 = "../imagefolder";
$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 "<img src='$file' alt='$file'><br />";
}
closedir($dir_handle);
?>
<input type="submit" name="Delete" value="Delete">
</form>
This is the code that displays my gallery
<?php
function createLbFromDir ($linkname, $galname, $directory, $thumbdirectory, $extensions = array ('jpg', 'jpeg','png','gif')) {
$gallery = "";
$dh = opendir ($directory);
while ($file = readdir ($dh)) {
$parts = explode(".", basename ($file));
$extension = $parts[count($parts)-1];
if (!is_dir ($directory . $file) && ($file != ".." && $file != ".") && in_array($extension, $extensions)) {
$gallery.= "<img src="".$thumbdirectory.$file."" alt="">n";
}
}
return $gallery;
}
// Page variables
$pageTitle = "SAFAAS - Asian Clothes Specialists";
$currentPage = "gallery";
require_once("includes/header.php");
require_once("includes/menu.php");
?>
<div id="portfolio_content" class="block">
<ul>
<?php echo createLbFromDir ("Linkname", "galleryname", "imagefolder/" , "imagefolder/thumbfolder/"); ?>
</ul>
</div>
The images are displayed for the gallery fine and the gallery works and uses the lightbox slider plugin and works fine.
I just need the images to display on my deleteimages.php page and only images are shown not subfolders within the folder.
Change this
while (false !== ($file = readdir($dir_handle)))
to
while (false != ($file = readdir($dir_handle)))
This section of your code:
$echo "<img src='$file' alt='$file'><br />";$
should be:
$echo "<img src='$path$file' alt='$file'><br />";$
that should fix it :)

Categories