Hi i am new in this website and i hope i have fun with all the members here,
I am trying to make code that get all the names of the files from folder and beside each one link to delete it
This is the code i have done but dunno why not working

The Full Code:

<?php
if($_GET['do'] == 'delete')
{
     unlink($myFile);
}

$myFile = $files[$i];
$collection = "";
$files = glob("gallery/*.*");
for
 ($i=0; $i<count($files); $i++) { $collection = $collection."\"".$files[$i]."\"".",". '<a href="index.php?do=delete">Delete file</a>.';}
 echo $collection;
 
 ?>

This part get the files names from the folder "gallery/" and post it:{And its working Perfect

<?php
$collection = "";
$files = glob("gallery/*.*");
for
 ($i=0; $i<count($files); $i++) { $collection = $collection."\"".$files[$i]."\"<br />";}
 echo $collection;
 
 ?>

This Part Which Delete the file (And its working perfect alone)
The Full Code:

<?php
if($_GET['do'] == 'delete')
{
     unlink($myFile);
}

$myFile = "Name Of The File";
?>
<a href="index.php?do=delete">Delete file</a>

But now i want him Automatic get the name of the file i want him to delete
when i try to change the "Name Of The File" with

<?php $myFile = $files[$i]; ?>

Not Working :/
So how i make him get the Name Of The File I Want To Delete?

Member Avatar for rajarajan2017
<?php
if($_GET['do'] == 'delete')
{
	$myFile = $files[$_GET['id']];
	echo $myFile;
    unlink($myFile);
}

$collection = "";
$files = glob("gallery/*.*");
for ($i = 0; $i < count($files); $i++)
{ 
$collection = $collection."\"".$files[$i]."\"".",". "<a href=\"test.php?do=delete&id='$i'\">Delete file</a>";
}
echo $collection;
?>

Nah Not Working :(
maby i have to make it 777 permission?

I gave it 777 permission...the same not working dunno why :/

Member Avatar for rajarajan2017
<?php
if($_GET['do'] == 'delete')
{
	$myFile = $_GET['param'];
	$myFile=trim($myFile) ;
	unlink($myFile);
}

$collection = "";
$files = glob("d:\gallery\*.*");
for ($i = 0; $i < count($files); $i++)
{ 
echo "<br/>";
$param = $files[$i];
$collection = $collection."\"".$files[$i]."\"".",". "<a href=\"test.php?do=delete&param='$param'\">Delete file</a>";
}
echo $collection;
?>
commented: Thanks Too Much For Trying Hard To Help Me +1

Didnt work and didnt even show the file names...
But after i changed the

$files = glob("d:\gallery\*.*");

To:

$files = glob("gallery/*.*");

he display the files names but dont delete when i press

BTW when i want to delete special file it workx:

<?php
if($_GET['do'] == 'delete')
{
	/*$myFile = $files[$_GET['id']];*/
	$myFile = "gallery/Untitled-1.jpg";
    unlink($myFile);
}
 
$collection = "";
$files = glob("gallery/*.*");
for ($i = 0; $i < count($files); $i++)
{ 
$collection = $collection."\"".$files[$i]."\"".",". "<a href=\"test.php?do=delete&id='$i'\">Delete file</a>";
}
echo $collection;
?>

WOHOOOOOOOOOOOOOOOOOOOOOO WORKING :D i made some changes and its working :D i removed the $param' in the link with $param (removed the ' ' ) and the folder location and its working WOW :D

The Final Code:

<?php
if($_GET['do'] == 'delete')
{
	$myFile = $_GET['param'];
	$myFile=trim($myFile) ;
	unlink($myFile);
}
 
$collection = "";
$files = glob("gallery/*.*");
for ($i = 0; $i < count($files); $i++)
{ 
$param = $files[$i];
$collection = $collection."\"".$files[$i]."\"".",". "<a href=\"index.php?do=delete&param=$param\">Delete file</a>";
}
echo $collection;
?>
Member Avatar for rajarajan2017

Wow! My code got works. Cool Great! I made a mistake with a single quotes.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.