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
Share on other sites

the original one was before i added the if statement and it said:

 

Warning: unlink() [function.unlink]: No error in C:\xampp\htdocs\FYP - Final Year Project\website\remove.php on line 25

 

but then adding the if statement, it just runs the else message.

Link to comment
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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...

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.