Jump to content

Deleting file from folder


captain_scarlet87

Recommended Posts

Hi,

 

I'm currently trying to delete a file from a folder however I am getting these errors:

 

Warning: unlink(testflash.flv) [function.unlink]: No such file or directory in C:\wamp\www\html\delete_video.php on line 6

 

Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\html\delete_video.php:6) in C:\wamp\www\html\delete_video.php on line 8

 

Would anyone kindly point out where I have made a mistake? Thanks.

 

view_videos(admin).php:

<?php
  $current_dir = "includes/videos/";    // Location to read files from.
  $dir = opendir($current_dir);        // Opens the directory.

  echo ("<p><h1>Video Tutorials:</h1></p><hr><br />");
  while ($file = readdir($dir))            // while loop
    {
    $parts = explode(".", $file);                    // pull apart the name and dissect by period
    if (is_array($parts) && count($parts) > 1) {    // does the dissected array have more than one part
        $extension = end($parts);        // set to we can see last file extension
        if ($extension == "flv"){    // Set allowable file extensions.
             echo "<table><tr><td><a href=video_player.php?video=$file> $file </a><br /></td>";    // Link to location of video.
             echo "<td><a href=delete_video.php?video=$file> Delete </a><br /></td></tr></table>";
        }
       }
    }
  echo "<hr><br />";
  closedir($dir);        // Close the directory.
?>
</body>
</html>

 

delete_video.php:

<?php
//delete_video.php

$file = $_GET['video'];

unlink($file);

header("location:view_videos(admin).php");
?>

Link to comment
Share on other sites

It seems like you're not pointing to the right path. Is testflash.flv in the same directory as delete_video.php? You probably need  to exactly where the video files are stored. Right now PHP is looking for the file in the same directory as delete_video.php.

 

So I would try changing  unlink($file); to  unlink('C:\path\to\your\videos\' . $file);

Link to comment
Share on other sites

Also, the second error is due to the fact that the first error is outputting content to the page. You can't do a header() after any content is output to the page. Fixing the first error should resolve the second as well.

 

As Jagual pointed out, you are not setting the path to the file. In the first script you use

 $current_dir = "includes/videos/";    // Location to read files from. 
$dir = opendir($current_dir);        // Opens the directory.

 

But in the delete script there is no path specified. It doesn't "know" to look for the file in the "includes/videos/" directory.

Link to comment
Share on other sites

Also, that code could use some work. glob() would be a much better solution than reading all the files in that directory. Plus, using explode() to determine the extension is a poor choice.

 

Give this a try

<?php
$current_dir = "includes/videos/";    // Location to read files from.
$flvFiles = glob($current_dir."*.flv");

$fileList = '';
foreach($flvFiles as $file)
{
    if (is_file($file))
    {
        $fileName = basename($file);
        $fileList .= "  <tr>\n";
        $fileList .= "    <td><a href=video_player.php?video=" . rawurlencode($fileName) . "> {$fileName} </a><br /></td>\n";
        $fileList .= "    <td><a href=delete_video.php?video=" . rawurlencode($fileName) . "> Delete </a><br /></td>\n"
        $fileList .= "  </tr>";
    }
}

if ($fileList == '')
{
    $fileList = "<tr><td>No tutorial files in current directory</td></tr>\n";
}
?>
<html>
<body>
<p><h1>Video Tutorials:</h1></p>
<hr><br />
<table>
<?php echo $fileList; ?>
</table>
<hr><br />
</body>
</html>

 

<?php
//delete_video.php
$current_dir = "includes/videos/";    // Location to read files from.
$file = rawurldecode($_GET['video']);
unlink($current_dir.$file);
header("location:view_videos(admin).php");
?>

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.