Jump to content

[SOLVED] unlink help


richard_PHP

Recommended Posts

hello all.

 

absolute novice to unlink so the code will probably appear a load of rubbish. anyway..

 

ive listed the files from a directory and want to have it so each file is displayed with a radio button, clickable, and a submit button at the end of the form. all this so that whatever file is chosen then you can delete the file from the directory. which is where unlink comes into it.

 

BUT.. it either doesnt work or i get an error, like i said, absolute novice using this function so i cant really find my problem. codes:

 

files.php (directory listing)

		<form action="remove.php" method="post" name="files">
		<?
			//define the path as relative
			$path = "uploads/";
			//using the opendir function
			$dir_handle = @opendir($path) or die("Unable to open $path");
			//running the while loop
			while ($file = readdir($dir_handle)) {
				if($file!="." && $file!="..")
				echo "<input name='$file' type='radio' value='' /><a href='uploads/$file' target='_blank'>$file</a><br />";
			}
			//closing the directory
			closedir($dir_handle);
		?>
		<p><input name="submit" type="submit" value="Delete File" /></p>
	</form>

 

remove.php ('you have deleted a file' type page)

		<?php
		$file = $_POST['files'];
		if (file_exists('$file')){
		unlink("$file");
		echo "Did it work?";
		}
		else {
		echo "Oops";
		}
	?>

Link to comment
https://forums.phpfreaks.com/topic/148462-solved-unlink-help/
Share on other sites

You can not catch $_POST['files'] it does not exisit because you are using a form that generates dynamic names. Instead of naming the files that way you need to make that the value of files. Like this.

	<form action="remove.php" method="post" name="files">
		<?
			//define the path as relative
			$path = "uploads/";
			//using the opendir function
			$dir_handle = @opendir($path) or die("Unable to open $path");
			//running the while loop
			while ($file = readdir($dir_handle)) {
				if($file!="." && $file!="..")
				echo "<input name='files' type='radio' value='$file' /><a href='uploads/$file' target='_blank'>$file</a><br />";
			}
			//closing the directory
			closedir($dir_handle);
		?>
		<p><input name="submit" type="submit" value="Delete File" /></p>
	</form>

Link to comment
https://forums.phpfreaks.com/topic/148462-solved-unlink-help/#findComment-779507
Share on other sites

renamed the form to 'list'.. still not working :(

 

files.php

      <form action="remove.php" method="post" name="list">
         <?
            //define the path as relative
            $path = "uploads/";
            //using the opendir function
            $dir_handle = @opendir($path) or die("Unable to open $path");
            //running the while loop
            while ($file = readdir($dir_handle)) {
               if($file!="." && $file!="..")
               echo "<input name='files' type='radio' value='$file' /><a href='uploads/$file' target='_blank'>$file</a><br />";
            }
            //closing the directory
            closedir($dir_handle);
         ?>
         <p><input name="submit" type="submit" value="Delete File" /></p>
      </form>

 

remove.php

		<?php
		$file = $_POST['list'];
		if (file_exists("$file")){
		unlink("$file");
		echo "Did it work?";
		}
		else {
		echo "Oops";
		}
	?>

Link to comment
https://forums.phpfreaks.com/topic/148462-solved-unlink-help/#findComment-779517
Share on other sites

got it working!

 

worked out that in order for the unlink to remove a file, it needs to know the correct path. so adding a variable which declares the containing folder i added a line like so:

 

		<?php
		$file = $_POST['files'];
		$path = "uploads/";
		if (file_exists($path . $file)){
		unlink($path . $file);
		echo "Did it work?";
		}
		else {
		echo "Oops";
		}
	?>

 

thanks for everyone's help! ;D ;D ;D

Link to comment
https://forums.phpfreaks.com/topic/148462-solved-unlink-help/#findComment-779525
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.