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.
Related
My aim is to download multiple files into the folder on my localhost. I am uploading them using the HTML form.
Here is the code (really sorry that I can't give a link to the executable version of the code because it relies on too many other files and database if anyone knows the way then please let me know)
foreach ($_FILES as $value) {
$dir = '/';
$filename = $dir.basename($value['name']);
if (move_uploaded_file($value['tmp_name'],$filename)) {
echo "File was uploaded";
echo '<br>';
}
else {
echo "Upload failed";
echo '<br>';
}
}
So this little piece of code give me an error:
And here are the lines of code:
The problem is that the adress is correct, I tried enterring it into my file directory and it worked fine, I have seen some adviced on other people's related questions that // or \ should be used instead, but my version works just fine! Also I have checked what's inside the $_FILES and here it is if that's required for someone trying to help:
Thank you very much if anyone could help!!
You are trying to move the file to an invalid (or non-existent) path.
For the test you will write
$dir = 'c:/existing_dir/';
$filename = $dir.basename($value['name']);
If you want to move the file to a folder that is relative to the running file try
$dir = '../../directory/';// '../' -> one directory back
$filename = $dir.basename($value['name']);
By starting your file path with $dir = '/'; you are saying store the file on the root folder, I assume of C:
Apache if correctly configures should not allow you access to C:\
So either do
$dir = '../';
$filename = $dir.basename($value['name']);
to make it a relative path or leave the $dir = '/'; out completely
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().
I am having trouble using fopen() to create a text document for later use as a cookie file.
I have read the documentation for this function, but to no avail.
Notes:
Ubuntu
read / writable ("w+")
I have tried several storage locations including:
/home/jack/Desktop/cookie
/var/www/cookie
/home/jack/Documents/cookie
PHP
echo "debug";
echo "\r\n";
$cookie = fopen("/home/jack/Documents/cookie", "w+");
fclose($cookie);
if(!file_exists($cookie) || !is_writable($cookie))
{
if(!file_exists($cookie))
{
echo 'Cookie file does not exist.';
}
if(!is_writable($cookie))
{
echo 'Cookie file is not writable.';
}
exit;
}
Result
file is not created
Output to browser: debug Cookie file does not exist.Cookie file is not writable.
Other Fun Facts
I have tried using fopen(realpath("/home/jack/Documents/cookie"), "w+")
echo "\r\n" gives a space. Why not a newline?
I believe the problem must be something to do with my permissions to create the file, but I have no problem "right-click" creating the text document on the Desktop.
THIS WORKS THIS WORKS THIS WORKS THIS WORKS THIS WORKS THIS WORKS THIS WORKS
echo "debug";
echo "\n";
$jack = "jack";
$cookie = "/home/jack/Documents/cookie";
touch($cookie);
chmod($cookie, 0760);
if(!file_exists($cookie) || !is_writable($cookie))
{
if(!file_exists($cookie))
{
echo 'Cookie file does not exist.';
}
if(!is_writable($cookie))
{
echo 'Cookie file is not writable.';
}
exit;
}
fclose($cookie);
THIS WORKS THIS WORKS THIS WORKS THIS WORKS THIS WORKS THIS WORKS THIS WORKS
Instead of fopen()..
touch() to create
chmod() for permissions
I also added user name jack to www-data group.
chmod($path, 0760) group read / write
Reference
chmod() octal values here.
Look at the documentation for file_exists again. It does not take a file handle as an argument, it takes a string filename. The same is true for is_writable. Even if it did, you are opening the file handle and then immediately closing it, so I'm not sure why you're trying to use the file pointer at all after it's been closed.
You may be correct in that you have improper permissions set, but I would start here, first.
Also, if you're only trying to create the file, you may look into using the touch method, instead:
if( touch( $filename ) ) {
// It worked!
} else {
// It didn't work...
}
The web server is not executing as your user. touch /home/jack/Documents/cookie && chmod 777 /home/jack/Documents/cookie to allow the web server user to access the file.
Note this is BAD in production environments.
It looks like a permission issue. What user is PHP running as? It's likely running as www-data or something similar. You should make sure that the folders you are trying to write to are writable by either the user or group that PHP is running as. If you created those folders while logged in a jack, they probably belong to jack:jack and are not accessible by www-data:www-data.
You can also add jack to the www-data group, to make things a bit easier for development.
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.
I'm having problems deleting a file from a higher directory, I found this post and tried it but no luck....:
gotdalife at gmail dot com 25-Sep-2008
02:04
To anyone who's had a problem with the
permissions denied error, it's
sometimes caused when you try to
delete a file that's in a folder
higher in the hierarchy to your
working directory (i.e. when trying to
delete a path that starts with "../").
So to work around this problem, you
can use chdir() to change the working
directory to the folder where the file
you want to unlink is located.
<?php
> $old = getcwd(); // Save the current directory
> chdir($path_to_file);
> unlink($filename);
> chdir($old); // Restore the old working directory ?>
here is the code that I currently have:
session_start();
if (!isset($_SESSION['agent']) OR ($_SESSION['agent'] !=md5($_SERVER['HTTP_USER_AGENT']))){
require_once ('includes/login_functions.inc.php');
$url = absolute_url();
header("Location: $url");
exit();
}
$folder = $_GET['folder'];
$filename = $_GET['name'];
$path = "../gallery/photos/$folder";
if (isset($_POST['submitted'])) {
if ($_POST['sure'] == 'Yes') {
$old = getcwd(); // Save the current directory
chdir($path);
unlink($filename);
chdir($old); // Restore the old working directory
}
else{
echo '<p>The photo has NOT been deleted.</p>';
}
}
I'm getting the error message :
Warning: unlink() [function.unlink]:
No error in
J:\xampp\htdocs\bunker\admin\delete_file.php
on line 37
line 37 being:
unlink($filename);
can anybody see what I've done wrong?
I always use absolute filepath names.
I'd define the filedir as a constant in your config, then concatenate so you have an absolute filepath, then make a call to unlink().
Btw: I hope you know your code is highly insecure.
See here:
http://bugs.php.net/bug.php?id=43511
and here
http://php.bigresource.com/Track-php-03TimDKO/
http://www.phpbuilder.com/board/showthread.php?t=10357994
Though I wouldnt recommend doing this, as per the comments above. Is there the option for a different approach?