We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Phalcon moveTo function is not moving files to target directory in server?

I'm trying to upload an image to a specific directory but the function moveTo is not moving the image.

public function uploadRecipeImageAction() {
    if ($this->request->hasFiles() == true) {
        $file = $this->request->getUploadedFiles()[0];
        $target_file = '/uploads/ketogenic-recipes/'.preg_replace("/[^a-z0-9\_\-\.]/i", '', basename($file->getName()));
        $file->moveTo($_SERVER['DOCUMENT_ROOT'] . $target_file);
    }
    return $target_file;
}

The function works fine locally but it's not working in server. There are no errors in logs

uploads and ketogenic-recipes directory permissions are set to 0775

PHP Version 7.2.20

Phalcon version is 3.4

var_dump($_SERVER['DOCUMENT_ROOT']) = string(30) "/home/web/public_html/test2"
var_dump($target_file); = string(42) "/uploads/ketogenic-recipes/harrypotter.jpg"


10.1k

Most of the time this is caused by a permission issue. Could you try

if (is_writable(dirname($_SERVER['DOCUMENT_ROOT'].$target_file))) {
    echo 'The dir is writable';
} else {
    echo 'The dir is not writable';
}

Most of the time this is caused by a permission issue. Could you try

if (is_writable(dirname($_SERVER['DOCUMENT_ROOT'].$target_file))) {
   echo 'The dir is writable';
} else {
   echo 'The dir is not writable';
}

The dir is not writable but permissions are set to 0775? Should I try 777? and will it not be dangerous? Any other alternative?

Most of the time this is caused by a permission issue. Could you try

if (is_writable(dirname($_SERVER['DOCUMENT_ROOT'].$target_file))) {
   echo 'The dir is writable';
} else {
   echo 'The dir is not writable';
}

I changed uploads and directories permission to 777 but still I got the not writable message!



10.1k

This will depend on your server configuration and owner of the directory. If your webserver is running as user www and the owner of the directory is www you should be fine with 0775. First number is Owner, Second Group, third is everybody (https://en.wikipedia.org/wiki/Chmod)



10.1k

I changed uploads and directories permission to 777 but still I got the not writable message! Maybe your webserver config is blocking this. And are you sure the path is correct?

This will depend on your server configuration and owner of the directory. If your webserver is running as user www and the owner of the directory is www you should be fine with 0775. First number is Owner, Second Group, third is everybody (https://en.wikipedia.org/wiki/Chmod)

Everything seems fine but still not moving the images to target directory!



5.8k
Accepted
answer
edited Aug '19

We moved from old server to new server. And I found few wrong configurations which was the reason for not moving images/files to target directories. Thanks to @ruudboon for the help;

PHP-FPM was not enabled

Our site was running on PHP 7.2.20 but PHP-FPM was not enabled on our website. So this means it was not using site specific php.ini

Wrong home directory

I mistakenly set home path for the sub-domains in cPanel. They were pointing to the sub-domain root instead of the web folder ( in phalcon we have web as home directory ). So $_SERVER['DOCUMENT_ROOT'] was pointing to the root folder; not to the web folder.



10.1k

Perfect and thnx for posting the solution!