Unlink() not working - php

I am trying to delete a file from the Server.
The files of my application is in a folder name "/public_html/app/";
All the images associated with the application is located in the following path: "/public_html/app/images/tryimg/"
The file in which I am writing the below code spec is in "/public_html/app/".
Here is my code snipet:
<?php
$m_img = "try.jpg"
$m_img_path = "images/tryimg".$m_img;
if (file_exists($m_img_path))
{
unlink($m_img_path);
}
// See if it exists again to be sure it was removed
if (file_exists($m_img))
{
echo "Problem deleting " . $m_img_path;
}
else
{
echo "Successfully deleted " . $m_img_path;
}
?>
When the above script is executed the message "Successfully deleted try.jpg" is displayed.
But when I navigate to the folder, the file is not deleted.
Apache: 2.2.17
PHP version: 5.3.5
What am I doing wrong?
Do I have to give a relative or absolute path to the image?

you're missing a directory separator:
$m_img = "try.jpg"
$m_img_path = "images/tryimg".$m_img;
// You end up with this..
$m_img_path == 'images/tryimgtry.jpg';
You need to add a slash:
$m_img_path = "images/tryimg". DIRECTORY_SEPARATOR . $m_img;
You also need to change your second file_exists call as you're using the image name and not the path:
if (file_exists($m_img_path))

You check the wrong path:
if (file_exists($m_img))
while you (tried to) delete(d) $m_img_path, so replace your check with
if (file_exists($m_img_path))
unlink() returns a boolean value to indicate whether the deletion succeeded or not, so it is easier/better to use this value:
if (file_exists($m_img_path))
{
if(unlink($m_img_path))
{
echo "Successfully deleted " . $m_img_path;
}
else
{
echo "Problem deleting " . $m_img_path;
}
}
Furthermore, the current directory is at the location where the script is executed, so you need to keep this in mind when using a relative path. In most situations it is probably better/easier to use absolute paths if possible.
If you need paths to a lot of files on your server, you might want to put the absolute path in a variable and use that, so it is easy to change the absolute location if your server configuration changes.

Related

can't get PHP unlink() to work

I have been trying, using PHP, to delete an image file from my image file directory on my Apache server. I can't get it to work. I keep getting the message "successfully deleted", but when I check the image file, it is still there. I suspect that the directory does not allow me access to unlink a file. But I am lost how I tell the server to allow me to unlink a file in the image file directory. I am running XAMMP (Apache server) on a MacBook Air. This is my code:
// DELETE THE PREVIOUS IMAGE FILE TO KEEP DISC USE TO A MINIMUM
$old_image_file = $_SESSION['previous_value']; // IMAGE FILE TO BE DELETED
echo "<p>old_image_file = ".$old_image_file."</p>";
// NAME IMAGE FILE TO BE DELETED AND INDICATE IN WHICH FOLDER IT SITS
$filename_to_be_deleted = '/files/image/'.$old_image_file;
echo "<p>$filename_to_be_deleted = ".$filename_to_be_deleted."</p>";
// CHECK IF FILE EXISTS BEFORE DELETING IT
if (file_exists($filename_to_be_deleted))
{
unlink(realpath($filename_to_be_deleted)); // DELETE THE FILENAME
}
// CHECK IF FILE STILL EXISTS OR NOT
if (file_exists($filename_to_be_deleted))
{
echo "<p>File was not deleted! " . $filename_to_be_deleted."</p>";
}
else
{
echo "<p>Successfully deleted " . $filename_to_be_deleted."</p>";
}
It's either path or CHMOD problem, let's find out.
Check unlink result with:
echo unlink(realpath($filename_to_be_deleted)); //returns true or false
If true is returned do the following and you should see the difference
echo realpath($filename_to_be_deleted);
echo $filename_to_be_deleted;
If false is returned make sure the containing directory's CHMOD is set to allow file deletion.
EDIT: Oh of course, follow Fred -ii-'s advice on using error_reporting().

unlink doesn't work ... file is writable and exist

I have an image sharing website and recently I've noticed I can't delete some of my images via script.
My file is writable and exist (so it's not a permission issue), but why can't I unlink it?
echo $file = base_path('./files/images/2013/11/TubeCom_3313c73ab7924b1f36ee49ad0979a16ad490f9a2.jpg');
echo is_writable($file) ? ' #is_writable ' : ' !!is_writable ';
echo is_file($file) ? ' #is_file ' : ' !!is_file';
$res = unlink($file);
var_dump($res);
Here is the result:
./home/siteecom/domains/site.com/public_html/files/images/2013/11/TubeCom_3313c73ab7924b1f36ee49ad0979a16ad490f9a2.jpg
#is_writable #is_file bool(false)
I've also tried relative path ... didn't work
I recently run into an issue like this before.
Firstly you'll need to turn on error reporting as unlink() will tell you exactly what's wrong:
ini_set('display_errors', 1);
error_reporting(E_ALL);
You'll want to make sure the directory that contains the file you want to delete is writable (please supply the chmod permissions for us to help furthur).
You should look into using realpath() to get the absolute path to the file. (I don't think this is the issue as it doesn't throw a file not found error).
Your problem is almost certainly related to incorrect permissions for the directory you're trying to delete in and also the script thats trying to delete said files.
If you could supply those permissions of both with something like:
echo "Directory = ".substr(sprintf('%o', fileperms(DIRECTORY)), -4) . "<br />";
echo "PHP File = ".substr(sprintf('%o', fileperms(SCRIPT)), -4) . "<br />";
We can try and help you further.
You probably missed that in order to delete a file you'll need write permissions to the file and it's ancestor folder. Make sure that the directory ./files/images/2013/11 is writable by PHP.

php unlink file renovated

I want to delete with php (unlink function) file which is out of webroot. my web root is in
C:\server\webroot\project\... in webroot I have folder named project and in there I have .php files.
whats about files directory. it is situated C:\server\mp3_files...
Also I've created in httpd.conf Alias("mp3") of mp3_files directory
I am writing this script in C:\server\webroot\project\test.php
script is like so =>
function delete($filename){
if (unlink("/mp3/" . $filename)){
echo "Deleted";
} else {
echo "No";
}
}
delete("file.txt");
this script gives me in php-errors => PHP-WARNING No such file or directory
also I have in (test.php) html form this =>
Download
And this works (It opens this file.txt)
So I'm wondered why can't delete with marked function "delete($filename)" ?
"/mp3/" . $filename is an absolute filepath, not relative to the webserver root, so it's assuming that you have an mp3 directory under your filesystem root when you should be looking under /server/mp3
EDIT
And is it /server/mp3 or /server/mp3_files
your post seems to contradict your code
File function in PHP go from the file system root.
You should write:
function delete($filename){
if (unlink("C:\\server\\mp3_files\\" . $filename)){
echo "Deleted";
} else {
echo "No";
}
}
delete("file.txt");
To make sure the internal PHP file path cache gets the correct information, reset with it with clearstatcache() before and after the unlink. Normally the path cache is reseted after every PHP function which is related to file manipulation. Reseting the cache is required if you remove files with shell_exec('rm file.txt') or similar.
See http://php.net/manual/ini.core.php#ini.realpath-cache-size and http://php.net/manual/ini.core.php#ini.realpath-cache-ttl

file_exists() does not run

$myfilepath = SITEROOT."/uploads/vaibhav_photo/thumbnail/".$user_avatar['thumbnail'];
if(file_exists($myfilepath))
{
echo "file exist";
}
else
{
echo "file does not exist";
}
It always goes to else part even though file is present.
if anybody have an alternate option for this in PHP please reply as fast as possible,
file_exists works on file paths only. http:// URLs are not supported.
$myfilepath = SITEROOT.DS.'uploads'.DS.'vaibhav_photo'.DS.'thumbnail'.DS.$user_avatar['thumbnail'];
NOTE::SITEROOT have actual root path and DS is constant DIRECTORY_SEPARATOR.
Pekka is correct that file_exists does not support http protocol.
You can however use file_get_contents
if(file_get_contents($myfilepath)) {
echo "file exist";
}
By default this pulls back the entire contents of the file. If this is not what you want you can optimise this by adding some flags:
if(file_get_contents($myfilepath, false, null, 0, 1)) {
echo "file exist";
}
This syntax will return the first character if it exists.
Check what your working directory is with getcwd().
Your path
example.com/uploads/etc/etc.jpg
is interpreted relative to the current directory. That's likely to be something like
/var/www/example.com
so you ask file_exists() about a file named
/var/www/example.com/example.com/uploads/etc/etc.jpg
You need to either figure out the correct absolute path (add the path containing all site roots in front of SITEROOT) or the correct relative path (relative to the directory your script is in, without a leading /).
tl;dr: try
$myfilepath = 'uploads/etc/etc.jpg';

Setting up path in the below code

<?php
if($_FILES['Filedata']['size']>=520000)
{
echo "\n Sorry, Not Moved Size below 5.2kb or 5200 bytes Only\n";
return;
}
$ext = end(explode('.', strtolower($_FILES['Filedata']['name'])));
if(move_uploaded_file($_FILES['Filedata']['tmp_name'], "./".$_FILES['Filedata']['name']))
{
echo "\nfile moved Success\n";
return;
}
?>
When i set path, it does not work... i dont know where to exactly set path such that the file gets saved in the directory.
See the move_uploaded_file documentation.
The first argument ($_FILES['Filedata']['tmp_name']) is the source, which you shouldn't change. The second argument ("./".$_FILES['Filedata']['name']) is the destination, which will currently put the file in the current working directory with its original name (This can be a security issue; you should put the file in an upload directory that has no execute permissions.)

Categories