Does PHP disable a function using a path?

I would like to delete a file from a folder in PHP, but I just have a path to this file, would it be ok to give a path to disconnect? for example

unlink('path/to/file.txt'); 

If this does not work, the only way to get rid of these files is to create the .php file in the path / to / directory and somehow include it in my file to call the method to delete the file, right?

+7
source share
9 answers

Check out the unlink documentation:

 bool unlink ( string $filename [, resource $context ] ) 

and

file name
The path to the file.

Thus, it only accepts a string as a file name.

Make sure the file is accessible from the path where the script is running. This is not a problem with absolute paths, but you can have one with relative paths.

+5
source

Got an easy way for your question

Use this code to delete a file from a folder

 $_SERVER['DOCUMENT_ROOT'] 

it can be used inside the unlock function

processed code

  unlink($_SERVER['DOCUMENT_ROOT'] . "/path/to/file.txt"); 
+16
source

unlink works great with paths.

Description of bool unlink (string $ filename [, resource $ context])

Deletes the file name. Like Unix C, the unlink () function. Level E_WARNING error will be generated on failure.

file name

 Path to the file. 

In case of problems with the rights prohibiting the error, this sometimes caused, when you tried to delete the file located in the folder above in the hierarchy, in your working directory (that is, when you try to delete the path starting with "../") .

So, to get around this problem, you can use chdir () to change the working directory to the folder where the file you want to disable is located.

 <?php $old = getcwd(); // Save the current directory chdir($path_to_file); unlink($filename); chdir($old); // Restore the old working directory ?> 
+5
source

You can use unlink with an outline.

You can also unlock the directory if you first cleaned it.

Here is the manual: http://php.net/manual/en/function.unlink.php

+1
source

Remember to check if the file exists, or you get an error if it is not:

 $file_with_path = $_SERVER['DOCUMENT_ROOT'] . "/path/to/file.txt"; if (file_exists($file_with_path)) { unlink($file_with_path); } 
+1
source

According to the documentation, unlink accepts a string parameter for the path.

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

In other words ... you have what you need to delete the file.

0
source

Not only is this normal, it is the only way to delete a file in PHP (besides system calls).

0
source

We can use this code.

 $path="images/all11.css"; if(unlink($path)) echo "Deleted file "; 
0
source
 if (isset($_POST['remove_file'])) { $file_path=$_POST['fileremove']; // chown($file_path, 'asif'); // echo $file_path; if (file_exists($file_path)) { unlink($file_path); echo "file deleted<br> the name of file is".$file_path.""; # code... } else echo "file is not deleted ".$file_path.""; # code... } 
-3
source

All Articles