PHP Unlink Doesn't Work

I am trying to delete a photo in php using unlink. I used it before on another server, but this time it does not work. I used the absolute path for the test, but still does not work:

I used it like: disconnect ('img1.jpg');

and:

unlink('http://www.mysite.com/img1.jpg'); 

Please, who has such experience?

+7
source share
7 answers

url not allow in ulink function

can you use this

It’s better, it’s also safe to use the absolute path. But you can get this path dynamically.

eg. via:

 getcwd(); 

Depending on where your PHP script is located, your variable might look like this:

 $deleteImage = getcwd() . 'img1.jpg'; unlink($deleteImage); 

check this

 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 may have one with relative paths.

+6
source

Although unlink () supports URLs ( see here ), now http: // is not supported: http cover information

use the file system path to delete the file.

+2
source

If you are using unlink on linux or unix, you should also check the is_writable results (string $ filename). And if the function returns false, you should check the file permissions with fileperms (string $ filename) .

File permissions are common problems in web spaces, for example. if you upload the file to ftp with the ftp user and the web server is acting as another user.

If this is a problem, you do with

chmod o + rwd img1.jpg

or

chmod 777 img1.jpg

for large write (and delete) permissions for other users.

+2
source

use the file system path
first define the path as follows:

 define("WEB_ROOT",substr(dirname(__FILE__),0,strlen(dirname(__FILE__))-3)); 

and the check file exists or not, if it exists, then disconnect the file.

 $filename=WEB_ROOT."img1.jpg"; if(file_exists($filename)) { $img=unlink(WEB_ROOT."img1.jpg"); } 
+1
source

unlink will not work with unlink('http://www.mysite.com/img1.jpg');

use unlink($_SERVER['DOCUMENT_ROOT'].'img1.jpg'); instead unlink($_SERVER['DOCUMENT_ROOT'].'img1.jpg'); // accept the current directory or,

 unlink($_SERVER['DOCUMENT_ROOT'].'dir_name/img1.jpg'); 

There may be a problem with permission to access .please files.

0
source

Give the relative path from the folder where the images are stored in the file where you are writing the script. If the file structure is similar:

 -your php file -images -1.jpg then unlink(images/1.jpg); 

Or there may be a problem with access rights to the folder. Are your files on the server or are you using it on localhost? If it is on the server, give 755 permissions to the image folder.

0
source

unlink($fileName); failed for me.
Then I tried to use realpath($fileName) as unlink(realpath($fileName)); it worked.

Just post it, in case anyone finds it helpful.

php unlink

0
source

Source: https://habr.com/ru/post/954670/


All Articles