I have a Apache/2.4.6 server running on a RedHat server. I am running a slim application at the root of the weber server in /var/www/html/.
I want to upload and image and save it to a specified folder. I can run the app perfectly fine on my local apache environment.
Here is my simple controller function:
public function test($request, $response, $args)
{
$files = $request->getUploadedFiles();
if (empty($files['file']))
return $response->withJson(['message' => 'Could not find file'])->withStatus(400);
$file = $files['file'];
$fileName = $file->getClientFilename();
//app and images folder both ahve 777 permissions
$file->moveTo('/var/www/html/app/images/{$fileName}');
return $response->withStatus(200)->write('Test Method');
}
I can echo the file name and see it is there it just move the file I get unwritable path. I don't understand why.
This was an issue caused by SEL(Security Enhanced Linux) which comes activated with RedHat. Apparently SEL does not allow uploads to any destination by default regardless of folder permissions and you have to manually allow this. I was able to finally upload using the following command:
chcon -R unconfined_u:object_r:httpd_sys_rw_content_t:s0 /var/www/app/images
I don't know much about SEL so unfortunately can't add more detail to this other than this solution worked for me.
Related
I am working on a mobile application that uploads a file to my Linux server. The API is made on Laravel which is within the same Linux Server inside /var/www/html directory.
I want my image that is being uploaded from the mobile application to be stored inside /home/uploads folder for that I have set FILESYSTEM_DRIVER=public and FILESYSTEM_PATH=/home/uploads inside my Laravel Project.
Now I made a function inside my controller to upload my file which is below
public function uploadFile(Request $request)
{
if ($request->hasFile('file')) {
Storage::putFileAs('Documents/', $request->file, 'Some name.jpg');
}
}
When I try to run the above code directly from my Laravel project, the file gets uploaded inside /home/uploads/Documents/Some name.jpg but when I hit the same function using Postman, I get a success message but I see no file inside my folder. What can be the cause of this issue?
Below is a screenshot of how I am sending the file through API using POSTMAN
Helps appreciated
maybe it is not a Laravel problem but a linux system wrong configuration try to set the www folder permissions recursively :
chmod -R 777 /var/www
indeed if laravel does not return an error it is possible that the problem is at a lower level
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"
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.
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.
Just did a fresh installation of nginx on my local machine.
I am trying to give write permission to nginx public folder but I get this error mod: cannot access '/usr/share/nginx/html': No such file or directory
the public file path for the nginx server is file:///usr/share/nginx/html
Please assist
First thing you should make sure is, your path is correct. Confirm if the folder name is "html" or "public_html".
Then you apply your permission using "sudo" prefix if you are not root user.
Revised:
I have an uploaded file that I would like to move to my other server for processing with another application. I thought the best way would be to use SSH built into Laravel however, it's proven to be a headache.
In my configuration file (remote.php), I've successfully set up my connection to my other server using SSH keys. I tested this by running
SSH::run(['cd /var/www/html', 'ls'], function($line) {
echo $line . PHP_EOL;
});
and it gave me a list of files in /var/www/html/ on my remote server. All seems good, right?
Now I want to use SSH::put() to upload my uploaded file to the remote server. I used:
SSH::put($file->getRealPath(), '/var/www/html/uploads');
Guess what? It ran without an error so I assumed it was uploaded successfully. When I checked that directory on the remote server, it was blank. I've tried to set file permissions to 777 on /var/www/html/uploads to see if it was a permission issue, same thing--nothing.
I also tried using SSH::get() to download a file from my remote server and it ran successfully.
Here's a dump on $file->getRealPath():
string(14) "/tmp/phpikt2mg"
Both machines are running Centos and have their public keys registered on both ends.
I'm a little bit late, but just faced the same problem and finally found the reason of failure! So it could save some minutes to others.
You should use full path with filename as a second parameter instead of just a path to folder. For this question it should be
SSH::put($file->getRealPath(), '/var/www/html/uploads/' . $file->getFilename());
I have set up a Ubuntu LAMP server, and everything is working fine, however, I have a PHP script that allows the user to upload files to a directory within the www directory. The files saved via PHP are chmod 644.
How can I change the setting so that the files saved on the server are chmod 755 by default?
Educate yourself as to what a umask is.
Use the umask() function.