James
James

Reputation: 43647

How to remove file

We have a script, /scripts/ourscript.php and a file, /media/movie1.flv.

How can we remove this file, when we run ourscript.php?

Upvotes: 2

Views: 809

Answers (4)

Martin Bean
Martin Bean

Reputation: 39389

Use the unlink function: http://php.net/manual/en/function.unlink.php

<?php
unlink('/media/movie1.flv');
?>

Upvotes: 0

Pascal MARTIN
Pascal MARTIN

Reputation: 401002

You need to use the unlink() function in your ourscript.php script.

For example :

$success = unlink('/media/movie1.flv');
if ($success === false) {
    // TODO : the file has not been deleted ; deal with that problem
}


Note, though, that unkink() will generate a warning if there's a failure... So, you might want to configure display_errors properly, or use the @ operator

(Even if using the @ operator is generally not such a good idea)

Upvotes: 2

Pablo Santa Cruz
Pablo Santa Cruz

Reputation: 181280

You need to call unlink("/media/movie1.flv");

Here's DOC on unlink function. Do keep in mind that the user running your script (usually Apache's user) needs WRITE permissions on the file you are your to delete.

Upvotes: 2

Andy E
Andy E

Reputation: 344575

Using unlink()

$success = unlink("../media/movie1.flv");
echo "File was ". ($success ? "" : "not ") ."deleted";

Upvotes: 4

Related Questions