开发者

php unlink problems - file exists

开发者 https://www.devze.com 2023-03-28 18:25 出处:网络
I am having trouble with unlink in php... The files in the directory are uploaded with php form. ls -l of /files/uploads/

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 he开发者_JS百科lp!


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.


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...


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.


Try echoing getcwd() to make sure you're in the directory you think you are.


ls -l of /files/uploads/
You did this with your user account/shell, didn't you?
Maybe the process running php "sees" the file system differently than your user account does (e.g. a chroot environment or something like that).

<?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));
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号