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

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.

Related

unlink a file in a lower directory

I am trying to find a solution to remove files from a directory listing. I found an example that works bery well for files within the same directory. However I assume that I would need to change directories but I am not understanding how to accomplish this.
Here is the example that works for files in the current directory
<?php
if(isset($_GET['delete'])){
$delurl=$_GET['delete'];
unlink($delurl);
}
?>
<?php
if ($handle = opendir('.')) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
echo "<br><b>$entry - Delete<br></b>";
}
}
closedir($handle);
}
?>
Ok got it.
<?php
if(isset($_GET['delete'])){
$delurl=$_GET['delete'];
unlink($delurl);
}
?>
<?php
if ($handle = opendir('../videos/')) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
echo "<br><b>$entry - Delete<br></b>";
}
}
closedir($handle);
}
?>
I just added the path after href=\?delete= and before $entry

Rename all files in a directory with numbers

I was wondering if anyone could help me write a PHP script for me that renames all the files in a directory in a sequence.
So...
DSC_10342.JPG -> 1.JPG
DSC_10343.JPG -> 2.JPG
DSC_10344.JPG -> 3.JPG
and so on.
Here's my version:
// open the current directory (change this to modify where you're looking)
$dir = opendir('.');
$i = 1;
// loop through all the files in the directory
while (false !== ($file = readdir($dir)))
{
// if the extension is '.jpg'
if (strtolower(pathinfo($file, PATHINFO_EXTENSION)) == 'jpg')
{
// do the rename based on the current iteration
$newName = $i . '.jpg';
rename($file, $newName);
// increase for the next loop
$i++;
}
}
// close the directory handle
closedir($dir);
Use rename to rename the files. You can use this handy script to loop through all files in a directory:
<?php
if ($handle = opendir('.')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
echo "$file\n";
}
}
closedir($handle);
}
?>
Then it's just a matter of looking at the filenames ($file) and figuring out what number to give them. If you need more help than that, just tell me and I'll give more details.
Try this:
$handler = opendir($directory);
$index = 1;
while ($file = readdir($handler)) {
if ($file != "." && $file != "..") {
rename($directory."/".$file, $directory."/".$index.".JPG");
$index++;
}
}
closedir($handler);
Using someone's snippet it would look like this:
<?php
$path = '.';
$i = 1;
if ($handle = opendir($path)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != ".." && is_file($path.'/'.$file)) {
$oldname = $path.'/'.$file;
$path_info = pathinfo($oldname);
rename($oldname, $path.'/'.($i++).'.'.$path_info['extension']);
}
}
closedir($handle);
}
?>
It will rename files with all extensions and skip directories that may be inside your directory.

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 :)

PHP to randomise image

I'm loading a folder full of images in, to create a jQuery Image Gallery.
There are currently 100 images being loaded in to create the gallery. I've got all that to load without issue.
All I want to do is make the image(s) that are loaded, load in randomly.
How do I achieve this?
My code is :
<?php
$folder = "images/";
$handle = opendir($folder);
while(($file = readdir($handle)) !== false) {
if($file != "." && $file != "..")
{
echo ("<img src=\"".$folder.$file."\">");
}
}
?>
Thanks in advance.
Just store all the image paths in an array and do a random shuffle of the array. And then echo the elements
<?php
$folder = "images/";
$handle = opendir($folder);
$imageArr = array();
while(($file = readdir($handle)) !== false) {
if($file != "." && $file != "..")
{
$imageArr[] = $file;
}
shuffle($imageArr); // this will randomly shuffle the image paths
foreach($imageArr as $img) // now echo the image tags
{
echo ("<img src=\"".$folder.$img."\">");
}
}
?>
Traverse the directory and store the image file names into an array and randomly select path names from the array.
A basic example:
$dir = new DirectoryIterator($path_to_images);
$files = array();
foreach($dir as $file) {
if (!$fileinfo->isDot()) {
$files[] = $file->getPathname();
}
}//$files now stores the paths to the images.
You can try something like this:
<?php
$folder = "images/";
$handle = opendir($folder);
$picturesPathArray;
while(($file = readdir($handle)) !== false) {
if($file != "." && $file != "..")
$picturesPathArray[] = $folder.$file;
}
shuffle($picturesPathArray);
foreach($picturesPathArray as $path) {
echo ("<img src=\"".$path."\">");
}
?>

Evaluating PHP Code

I am very much a beginner when it comes to using PHP. I was given this code, to try and output the contents of a files on a folder, onto a server, but my issue is I do not know how to read and alter this code to fit my specific file path. Can someone help me out with this, and lets just use the name folder as an arbitrary pathname.
<?php
if ($handle = opendir('.')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
echo "$file\n";
}
}
closedir($handle);
}
?>
<?php
$dir_path = '.'; // '.' = current directory.
// '..' = parent directory.
// '/foo' = directory foo in the root file system
// 'folder' = a dir called 'folder' inside the current dir
// 'a/b' = folder 'b' inside 'a' inside the current dir
// '../a' = folder 'a' inside the parent directory
if ($handle = opendir($dir_path)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
echo "$file\n";
}
}
closedir($handle);
}
?>
<?php
$path = '.';
if ($handle = opendir($path)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
echo "$file\n";
}
}
closedir($handle);
}
?>
Detailed explanation and examples: http://www.php.net/function.opendir

Categories