I am using PHP. I have a file in local storage of Xampp. I have been trying to delete that using unlink. I tried copying the relative path and pasted in unlink, but it does not delete the file from local storage. I even tried pasting the path, so not the relative one, but it still does not delete the files. Can someone reckon what I might be doing wrong?
My code:
unlink("userdir/username/181");
As someone mention in comments I try changing the permission. I tried sudo chmod -R 755 /Users/hemlatamahaur/.bitnami/stackman/machines/xampp/volumes/root/htdocs
but it still says Operation not permitted. What shall I do?
this code working for me!
// first check location exist or not
if (file_exists(#locationfile)) {
unlink(#locationfile);
}
Related
I'm facing a problem with deleting video files from folder using php unlink() function , image are deleting but when trying deleting videos it says
unlink(file_path) : permission denied.
You (running your script as either through CLI or a webserver) need write access to the directory in which the files are located. so access to the file is not enough.
Your image directory would be different and writable for webserver or cli.
chmod("your/video/dir/path",0777);
try using above code before unlink the video in your script.
Edit: Looks like you are using Windows. Unfortunately, my answer is for Unix-like operating systems (so Linux, MacOS). You could try installing Bash extension for Win8+, but still I'm not sure if this would work. However, I'm keeping my answer in case of anyone with similar problem is looking here for an answer.
Changing permissions through PHP might work in some cases, but not always, because if you don't have permissions to delete the file you might also not have permissions to change them.
The best solution is to create a directory where you will keep files to which PHP will have full access. Let's call it dirname. Once you have created a directory, change its owner and group to the one corresponding to your web server user's name (if you're using Apache, it's "www-data"), for example: chown www-data:www-data dirname.
Once you've done that, change folder's permissions. My suggestion is 744, it will assure that user who owns it will have all permissions, and everyone else will be able only to read it. To do that, execute the following command: chmod -R 777 dirname.
Now you should be able to do anything you want with the files in given directory directly from PHP.
I have seen several similar questions, but no answer worked in my situation, except that it probably has something to do with permissions.
A PHP script served by Apache tells me unable to open database file.
When I print the path to that file, it returns a valid path, say DBPATH. The file does exist at that location; I gave it and its parent folder 777 rights; I gave them user:user access, where user is the sudoer that all script files belong to. I did the same to the whole htdocs/ folder, just in case.
When I print file_exists(DBPATH), it returns false. Is is most likely a matter of permissions, but I don't know what I should change for PHP to have access rights. I tried apache:apache, too. I cannot su apache (user not available).
My scripts are in htdocs/. DBFILE is somewhere out of it (I tried /tmp/test, all in 777, but no luck either).
No safe_mode, PHP 5.4 freshly installed, CentOS7.
Please someone give me a clue at least to help debug it.
Maybe such as: how can I check whether my file will be readable from apache/my php script, without running the script itself? How can I get the name of the user that is used to execute it?
Solved, more or less.
To debug I had the idea to move DBFILE to the same folder where the PHP script lives, and check it can find it - it did. Then I move DBFILE one folder after another in the tree to see where it stopped finding it.
It occurs that if only one of the folders in the whole path does not have execute rights for all users (xx5), the file cannot be found and file_exists returns false.
So the solution was to create another folder in a totally executable place (/var/www/data/ worked after chmod 755 data), and move the file there.
Do you use an absolute path or relative path?
Because file_exists() doesn't work with HTTP addresses (which is an absolute path). But you can enter the relative path.
I had the same problem and it fixed it. It was the same problem with unlink().
Exemple:
$file_relative_path = "./wp-content/uploads/fileDirectory/fileName.jpg";
if (file_exists($file_relative_path)) {
unlink($file_relative_path);
}
I had a similar problem and was able to solve it by the answer of JulienD:
If the execute flag of a directory in the file system (Linux) is not set, then PHP (still) scans this directory with glob or scandir. However, a subsequent check with file_exists() on this list of results, I wanted to find broken symbolic links, returned false!
So the solution was to set the Execute right for the directory, as mentioned by JulienD.
hi am getting serious issue when i upload image i get error from uploader missing temporary folder. i search the internet and use this code ini_set('upload_tmp_dir','/sub_domain_home/folder/tmp'); but this not work. then i echo the directory of tmp image upload i use this code sys_get_temp_dir(); i get empty result. i need help how can i fix this issue ? because my default tmp folder not work properly. after when i remove my server home directory default files and folders.
SOLved
i contact to my server provider and tell him to all details and info what i do for solve this issue. they answer me we don't saw any issue we change ur server if you want ? and i agree then thye change my server after 10 hours i check my website my issue solved and all works fine. uploading etc.
why did you delete everything in /home ? I assume you are using linux?
Do this:
do a phpinfo() on a test.php to see where your php.ini is located
Stop using ini_set, sometimes it does not work properly
Set a temp dir in the php.ini
Create above temp dir (or make sure it exists) and chmod 775 or something so you can test properly if it's working
Check phpinfo() again to see if the tmp dir has taken effect.
Stop deleting files you think you don't need before reading about them online
I have a PHP script that deletes files. It can delete files from my root directory, but when I try to delete from a subdirectory, it says "Permission denied". I know the file exists and PHP can access it because I can read and write to the file, but I can't delete it.
Why?
EDIT: If this is relevant, I am using Zymic hosting. But I have another site on Zymic where the deleting works fine. I don't get it...
EDIT: I use ajax to access the PHP file with the code to delete, and the ajax sends the file name to delete. I know the file name it sends is correct, because the warning message prints it for me. The PHP code is simply:
$file=$_POST['file'];
echo unlink($file);
EDIT: I fixed it! I don't know why this worked, but I FTP-chmodded the directory from 755 to 775 Can anyone tell me why it worked?
To delete the file you need write permissions to the folder that contains it, check that first.
CHMOD xxx -> Owner Group Other
first case:
755 - Owner (read, write, execute), Group (read, execute), Other (read, execute)
second case:
775 - Owner (read, write, execute), Group (read, write, execute), Other (read, execute)
Try to add this at the beginning of the script you're running:
error_reporting(E_ALL | E_STRICT);
That should be able to point out accurately what is going on, chances are that you do not have permissions to write to the folder
Especially if you're working on a Linux environment. In Linux everything is a file, even folders. When it comes to deleting files, you need to be able to write to the file that represents a folder, that's why having permissions to write to the file you're trying to get rid of, does not have anything to do with deleting it.
You have to fclose($myfile) first before using unlink($myfile),,because if it is open on the server by anyone it will not delete it. Also place this script in the same directory as the files you wish to delete,, otherwise you may accidently delete the whole DIR.
I am trying to use unlink command on my server Apache/2.2.3 PHP Version 5.2.10. I have been using the same script on a bunch of other servers including my local PC. Unfortunately, the command doesn't work.
I tried this so far:
chmod everything to 777
putting new files and unlinking them
also I notice that I can't upload or saveXML files to the server
I am guessing that it is some kind of setting inside the php.ini file, but I don't know which.
May be it's a safe_mode?
Solved
Checking if it is writable first and then unlink it seem to do the trick
if (is_writable("$bannerlink")) unlink("$bannerlink");
thanks to everybody