How to delete an image from folder with php? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to delete an image from folder with php?

I have tried using unlink method . I got permission denied as error. Then i went to folder and gave access for both read/write . But still i am getting the same error. Is there any way to do it. I am in a windows machine.

30th Nov 2020, 9:05 AM
Ananthu SV
Ananthu SV - avatar
4 Answers
+ 1
I tested with the following PHP script. Please add the ?php. Sololearn takes a fit if I share that part in an answer. $filename = 'testing/test_file.txt'; if (unlink($filename)) { echo "File deleted at " . $filename; } else { echo "File not deleted at " . $filename; if (file_exists($filename)) { echo "The file still exists at " . $filename; } else { echo "The file does not exist which explains why it could not be deleted."; } } That works for me using WAMP in Windows 10 if the folder allows Write. One thing I didn't expect was that I had to restart WAMP for it to make use of new permission changes. WAMP's httpd(Apache HTTP Server Daemon) is running under the SYSTEM user as indicated by Task Manager details tab on my machine. Try my script to see if it works in your environment. Is there a way to share more of your code? I wonder if any image files are getting locked.
4th Dec 2020, 1:08 AM
Josh Greig
Josh Greig - avatar
0
You're correct to look at permissions. unlink should work if the permissions and everything around permissions are good. You should look more deeply at the permissions, though. Read and write permissions aren't as simple as you might think. In Linux and Unix at least, there are separate read, write, and execute permissions for owner, group, and other. In Windows, there are different permissions for different types of users and the user you log in with could be different from the user associated with your web server. Permissions on the folder that contains the image also matter. Here are more questions to investigate the problem more deeply: Can you create a new file using your PHP script? This will test that you can write in the directory. What permissions are on the directory? What user is running the web server? Do different users have different permissions on the image file?
30th Nov 2020, 9:41 AM
Josh Greig
Josh Greig - avatar
0
Josh Greig ya i all though understand the different kind of permission required for different files. I tried to change the ownership to xampp server (Solution from stackoverflow) but yet the file which I want to delete is not deleted. I saw various YouTube videos to fix it but i couldn't. I am not an expert in all these to figure out the OS related stuff because i am still learning things . Now can you tell me what is the permission that is required to delete a file. In most of the tutorial i have seen people just allowing the folder with access to read/write but why not mine is fixed?
30th Nov 2020, 2:24 PM
Ananthu SV
Ananthu SV - avatar
0
Josh Greig Yes i will test this out in machine. I am using XAMMP i even tried restarting after giving permission to write for whole project folder but still couldn't fix. But really thank you for your help🙏😊 Thus is the github repo 👇👇 https://github.com/Sananthu-47/Social_Feeds The file which has the unlink function is in 👉👉 process/delete-post.php
4th Dec 2020, 3:57 AM
Ananthu SV
Ananthu SV - avatar