0

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"
Iftikhar uddin
  • 3,117
  • 3
  • 29
  • 48
  • 1
    `/uploads/ketogenic-recipes/harrypotter.jpg` is an absolute path? You are aware it is outside the project (sorry for asking)? – Tpojka Aug 05 '19 at 14:11
  • @Tpojka actually it is `web/ uploads/ketogenic-recipes/filename` but web is skipped via htaccess. And even I tried by adding web. But still not working. – Iftikhar uddin Aug 05 '19 at 14:19
  • `$target_file` is pointing exactly at an absolute path related to OS root location. You need to combine `$_SERVER['DOCUMENT_ROOT']` when you are setting `$target_file` location. Probably `$_SERVER['DOCUMENT_ROOT'] . '/uploads/ketogenic-recipes/'.preg_replace("/[^a-z0-9\_\-\.]/i", '', basename($file->getName()));` or something similar. You have to set file location related to your web server, not related to system root and `'/what/ever/comes/after/initial/slash'` should be an absolute path. – Tpojka Aug 05 '19 at 14:45

2 Answers2

0

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.

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.

Iftikhar uddin
  • 3,117
  • 3
  • 29
  • 48
0

In our case, the problem was directly linked to the PHP upload_max_filesize set to 2MB by default. Increasing the value in php.ini file fixed the problem.

wlarcheveque
  • 894
  • 1
  • 10
  • 28