Question

I want to delete with php (unlink function) file which is out of webroot. my web root is in

C:\server\webroot\project\... in webroot I have folder named project and in there I have .php files.

whats about files directory. it is situated C:\server\mp3_files...

Also I've created in httpd.conf Alias("mp3") of mp3_files directory


I am writing this script in C:\server\webroot\project\test.php

script is like so =>

function delete($filename){
if (unlink("/mp3/" . $filename)){
    echo "Deleted";
} else {
    echo "No";
}
 }
delete("file.txt");

this script gives me in php-errors => PHP-WARNING No such file or directory

also I have in (test.php) html form this =>

<a href="/mp3/file.txt">Download</a>

And this works (It opens this file.txt)

So I'm wondered why can't delete with marked function "delete($filename)" ?

Was it helpful?

Solution

"/mp3/" . $filename is an absolute filepath, not relative to the webserver root, so it's assuming that you have an mp3 directory under your filesystem root when you should be looking under /server/mp3

EDIT

And is it /server/mp3 or /server/mp3_files

your post seems to contradict your code

OTHER TIPS

File function in PHP go from the file system root.

You should write:

function delete($filename){
if (unlink("C:\\server\\mp3_files\\" . $filename)){
    echo "Deleted";
} else {
    echo "No";
}
 }
delete("file.txt");

To make sure the internal PHP file path cache gets the correct information, reset with it with clearstatcache() before and after the unlink. Normally the path cache is reseted after every PHP function which is related to file manipulation. Reseting the cache is required if you remove files with shell_exec('rm file.txt') or similar.

See http://php.net/manual/ini.core.php#ini.realpath-cache-size and http://php.net/manual/ini.core.php#ini.realpath-cache-ttl

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top