Reputation: 12429
I am having trouble with unlink in php... The files in the directory are uploaded with php form.
ls -l of /files/uploads/
total 6976
-rw-r--r-- 1 alex admin 689030 15 Aug 11:40 01805_goneclubbing_1680x1050.jpg
-rwxrwxrwx 1 alex admin 174932 15 Aug 11:52 4vvF60D.tmp.jpg
-rw-r--r-- 1 alex admin 2699554 15 Aug 12:16 example.JPG
php script (cakePHP framework):
$file_path = '/files/uploads/';
$file_name = $file['Upload']['path'];
$classroom_id = $file['Upload']['classroom_id'];
if (unlink($file_path . $file_name)) {
if ($this->Upload->delete($id)) {
$this->setFlash('File deleted');
$this->redirect(array('controller' => 'classrooms', 'action' => 'view', $classroom_id));
}
}
php error:
Warning (2): unlink(/files/uploads/example.JPG) [function.unlink]: No such file or directory [APP/controllers/uploads_controller.php, line 55]
I searched on stackoverflow, tried giving 777 permissions and still not working. I can access the image through the browser at that path.
Thanks for the help!
Upvotes: 1
Views: 3708
Reputation: 3972
You have an opening slash in your file path which is telling the application to look at root
, not from your current location. You should change your path to include a .
,so that it is
./files/uploads/example.JPG.
Upvotes: 1
Reputation: 15375
On any UNIX system, /
is your system root. So when you try to access /files
, you try to access a folder files
located at your system root. I think you want to access /path_to_www/files
, so either use a variable which stores your base path or use a relative path.
On the other hand, when you try to access /files
from your web browser, it reaches the /
of your web directory (or an alias path). So it is totally normal that you can access your image from your browser but not from php.
Upvotes: 4
Reputation: 88697
The error tells you what the problem is - the file $file_path . $file_name
doesn't exist. Echo it out - does it equal what you think it should?
You are referencing the storage location cannonically ('relative' to the root directory /) - did you mean to? Is there actually a directory called /files
on your box? If there is I would be surprised...
You should make it if (is_file($file_path . $file_name) && unlink($file_path . $file_name))
in case the file has gone missing because of an external cause to get rid of the ugly error message, and probably also do $file_name = ltrim($file['Upload']['path'],'/');
to get rid of any stray leading slashes...
Upvotes: 1
Reputation: 96159
ls -l of /files/uploads/You did this with your user account/shell, didn't you?
<?php
$file_path = '/files/uploads/';
$file_name = $file['Upload']['path'];
$classroom_id = $file['Upload']['classroom_id'];
// <-- debug
echo '<pre>files in ', $file_path, ":\n";
foreach( glob($file_path.'*') as $f) {
echo "'", $f, "'\n";
}
echo "----\n</pre>\n";
// debug -->
if (unlink($file_path . $file_name)) {
if ($this->Upload->delete($id)) {
$this->setFlash('File deleted');
$this->redirect(array('controller' => 'classrooms', 'action' => 'view', $classroom_id));
}
}
Upvotes: 0
Reputation: 11478
Try echoing getcwd()
to make sure you're in the directory you think you are.
Upvotes: 0