From the title you can see i am looking for a way to delete a file from a different directory. All i can find on the subject is the unlink(), but from what i read in the documentation and from testing that function is it deletes the file name from the code you put it in. Makes me think it's quite similar to closing the file. What I am trying to do is actually delete a file using code so my user doesn't have to manually go to the folder and find the song they just deleted from the mysql database.
unlink() will delete the file off your server
rmdir() will delete a directory off your server
Note: Once it's gone, it's gone.
unlink truly deletes the specified file from the disk
We can delete files by giving its URL or path in PHP by using unlink command. This command will work only if write permission is given to the folder or file. Without this the delete command will fail. Here is the command to delete the file.
$path="images/all11.css";
if(unlink($path)) echo "Deleted file ";
realpath — Returns canonicalized absolute pathname
is_readable — Tells whether a file exists and is readable
unlink — Deletes a file
Run your filepath through realpath, then check if the returned path exists and if so, unlink it.
Related
I have a strange issue with unlink.
when I use unlink() function, it removes the symlink which is linked to existing file correctly.
But for the symlink file which is actually pointing un-existing file, it does not work.
I have googled here and there, but can not find the right reason.
The working flow is like this:
first PHP file removes the source file (which is triggered by ajax request)
and then second php file tries to remove the symlink which is pointing the file just removed by first PHP file.
But second PHP file fails to remove the symlink.
Any idea why this kind of thing is happening?
Well, it is due to file_exists() function.
Before unlink the file, it checks to see if file is exists using file_exists() function.
But the problem is this function returns false if symlink is invalid.
So, I have a script in
/var/www/vhosts/Domain/SubDomain/Script.php
that writes images to
/var/www/vhosts/Domain/wwwDomain/PhotoLocation/
and this works fine.
However, when I run this script:
/var/www/vhosts/Domain/SubDomain/Script_to_Delete_Photo.php
which uses the 'unlink' command
unlink ("/var/www/vhosts/Domain/wwwDomain/PhotoLocation/Image.jpg")
I get the error
"PHP Warning" "No such file or directory in
/var/www/vhosts/Domain/SubDomain/Script_to_Delete_Photo.php"
I thought that since I could WRITE the file fon a different subdomain, I could Delete from the other subdomain as well.
am I missing something?
Do I need to set Permissions somewhere else, or set a path differently? I specifically call the File by Absolute Path, and I can verify that the file exists there.
All looks fine. Some suggestions:
files are case sensitive
check if the folder Photolocation has write permission (it usually will for you can write to the location)
close the handle after you've created the file. If the handle is still open, delete is not possible
check with file_exists() just to make sure the file does exist
did you create the file or a shortcut?
when creating the file, do not suppress possible errors. It might be a header problem in the created file itself. Check your error-files for clues.
Hi I want to create folders (with php) outside of the webroot and move uploaded files to them.
is that possible?how?
Use rename, copy or move_uploaded_file, though you need to make sure the folder has the correct permissions (write permissions for your webserver / php executing user).
Also, Handling file uploads might have some useful information for you.
Use php's move_uploaded_file() function.
Linked in a comment, however it bears repeating: Read the documentation.
Check if the directory exists and if not, create it:
if (!is_dir('/dir/path')) {
mkdir('/dir/path');
}
Move your uploaded file to the directory:
move_uploaded_file($_FILES["file"]["tmp_name"], "/dir/path");
I have a script that uploads files to a directory with file permissions 0644. I am unable to delete the file via FTP or using PHP's unlink() function (550 error). After scouring the web, I was unable to find a method to fix this problem. I am aware that the issue has to do with group/owner permissions, but I don't know how to fix the problem.
Should I use copy() or rename() instead?
Any ideas?
Edit: All uploaded files have owner/group set as: 48 48. All other files that I have uploaded via FTP and NOT the PHP script are 1006 1006. Is the owner/group set for the incorrect user?
I have already tried using chmod() to set permissions to 0666. I think the problem may be with the user?
Edit 2: Should I use exec() and run a command that changes the owner and group of the file?
It depends on what user your script is running as. Try uploading the files as 0655 instead
if you're using move_uploaded_file(); function, permissions are set correctly, so unlink(); should work
try this
move_uploaded_file($from, $to);
chmod($to, 0666);
You will want to have write permission to delete the file. 6 represents read/write. The first number after the zero represents the file's owner, who created it. If you are running the script to create the file, you should be able to delete it as long as you use the same user, probably the server user.
I'm uploading files via JS and storing the temp path in the session.
Than i use the following code to move the files.
if(move_uploaded_file($_SESSION['temp_img'][$key]['path'], $dest.$bigimg)){
$dest and $bigimg are defined earlier in the script with the id from the database.
Any Ideas or alternatives ?
MANCHUCK's answer was close but not quite there. You must call move_uploaded_file within the script where the file was uploaded. You cannot do what you're doing, that is, "storing temp path in the session" because that path is only valid for one request.
From the PHP manual:
The file will be deleted from the
temporary directory at the end of the
request if it has not been moved away
or renamed.
(Emphasis mine)
move_uploaded_file checks that a file has been uploaded to that page. You are actually uploading the file to a different PHP script then storing in a session. Instead of using move_upload_file use rename.
What is the output of $_SESSION['temp_img'][$key]['path'], also do you have permission to write to the web directory your placing the files. You may need to set it to 777 for some hosts to allow the webserver to write there.