Not creating file via my browser - php

when I run my page on my local host it does not create the file. I have given read and write permissions to the folder site. i have used system and exec functions and when i run the page via the terminal it creates a file but via my browser it doesnt.
$my_file = '/var/www/site/file.txt';
$handle = fopen($my_file, 'w') or die('Cannot open file: '.$my_file);

The following commands will correctly apply permissions to the directory.
cd /var/www/site
sudo chmod 664 *
sudo find . -type d -exec chmod 755 {} \;
If the error persists, please post any PHP errors that are thrown.

Did you try with a relative adresse?
$my_file = 'site/file.txt';

Related

Can't write file with PHP on Linux Server

Heys Guys! I'm trying to create a new xml file with php on my ubuntu server. The code looks like
$myfile = fopen("test.xml", "w") or die ("Unable to write file");
fwrite($myfile, "Test");
fclose($myfile);
But whenever this php file is called I get "Unable to write file". Does anybody know what the issue is and how to fix it? Thanks
In the Linux server, it may be a file permission issue try this command below in your directory. I am assuming your directory is /var/www/html if not replace this with your directory path and run commands.
chown -R www-data:www-data /var/www/html
chmod -R 755 /var/www/html

How to fix permission denied in php?

I am trying to write code that writes a file to a directory on a surfer. I am using the function fopen("textfile.txt","w") but I get the message:
Warning: fopen(testfile.txt)[function.fopen]: failed to open stream: Permission denied in G:\PleskVhosts....\contribute.php on line 57
How can I fix this?
Make sure your web server user have the right permission to the folder you are going to create files.
So change the mode of the folder with 75*,add the web server user to the group of the folder.You can check this link for details.
You need change permissions for that file. If you use linux try chmod 755, if use windows change permission manually.
If you have your project deployed on linux you can use something like this for changing folders/files permissions
sudo find /path/of/project -type d -exec chmod 755 {} \; ; sudo find /path/of/project -type f -exec chmod 644 {} \;

Laravel HTML to PDF conversion (DOMPDF)

DOMPDF Wrapper for Laravel 5, It's a html to pdf converter for laravel 5.
I have tried to convert laravel default auth register page to PDF and the path is http://www.careertag.dev/auth/register. Code used for this purpose is given below. But I am getting font issue. Could you please help me to solve this issue?
routes.php
Route::post('/screenshot', function(){
$pdf = App::make('dompdf.wrapper');
//$pdf->loadHTML('<h1>Test</h1>');
$view = View::make('auth.register')->render();
$pdf->loadHTML($view);
$pdf->stream();
//return $pdf->stream('invoice');
return $pdf->download('profile.pdf');
});
Error
ErrorException in font_metrics.cls.php line 343: file_put_contents(C:\xampp\htdocs\careertag\storage\fonts/13350985545a9988387f8a573f409063.ttf): failed to open stream: No such file or directory
Create fonts folder inside storage directory and make sure storage is writable by web server by running follwing command. This worked for me.
sudo chmod -R 777 storage
Above (777) can be dangerous, therefore to achieve it in a secure way, try the below commands.
sudo find storage -type d -exec chmod 755 {} \;
sudo find storage -type f -exec chmod 644 {} \;
Copy the 'fonts' directory of your theme form the location 'project-directory/public/themes/your-theme/path-to-fonts-directory' to the location
'project-directory/storage/fonts-directory'
This works for me when I was hanged for long time.

Opencart fopen(/upload/system/logs/): failed to open stream: Is a directory

I got this error in my opencart shop right after uninstalling an extension. The complete shop does not work anymore. It has not my own template and all php functions do not work. This error shows up:
Warning: fopen(/my_path/system/logs/): failed to open stream: Is a directory in /my_path/system/library/log.php on line 6
This is the Log class:
class Log {
private $handle;
public function __construct($filename) {
//this is line 6
$this->handle = fopen(DIR_LOGS . $filename, 'a');
}
public function write($message) {
fwrite($this->handle, date('Y-m-d G:i:s') . ' - ' . print_r($message, true) . "\n");
}
public function __destruct() {
fclose($this->handle);
}
}
Looks like the $filename var is empty. This also reflects the not working php i mentioned above. The logs directory is 755 and i tried it with 777.
And a second error shows at the bottom of my shop:
Fatal error: Call to a member function get() on a non-object in /homepages/6/d421894284/htdocs/opencart/upload/index.php on line 104
Have anyone experienced this error in Opencart 2.0.1.1? Google says, that this must be a common error, but I canĀ“t find a solution.
the answer is simple , change ---->
define('DIR_STORAGE','/storage/');
define('DIR_STORAGE', DIR_SYSTEM . '/storage/');
and do that to config.php , and config.php inside admin folder , change both folders
this problem happens When the system cant create the storage folder which contains the logs cache and modification folders because of permissions problems ,some times the problem happens after storage directory transfer from the dash board , so to solve the problem you need to do the following
create a storage folder in the root of the application
sudo mkdir storage
set the new folder to 777 or 775 or 755 permissions sudo chmod -R 755 storage
create a storage folder in the root of the application
sudo mkdir logs
set the new folder to 777 or 775 or 755 permissions sudo chmod -R 755 logs
create a storage folder in the root of the application
sudo mkdir modification
set the new folder to 777 or 775 or 755 permissions sudo chmod -R 755 modification
The $filename for the log file is either not set in your admin panel or not being passed to the constructor. Check in Admin > System > Settings > Server > Error Log Filename and make sure you have something set there.
If so, next step would be to check the main index.php and make sure the code is intact around line 68 which should be :
$log = new Log($config->get('config_error_filename'));
$registry->set('log', $log);
$filename is empty , Find the filename because it not exist.
<?php
class Log {
private $handle;
public function __construct($filename) {
if (! empty( $filename )) {
$this->handle = fopen(DIR_LOGS . $filename, 'a');
}
}
public function write($message) {
fwrite($this->handle, date('Y-m-d G:i:s') . ' - ' . print_r($message, true) . "\n");
}
public function __destruct() {
fclose($this->handle);
}
}
check if $filename is not empty
We were facing the same problem and were able to resolve it by putting correct data in these two files:
config.php
admin/config.php
Please make sure that you have put correct entry for DB_PREFIX:
define('DB_PREFIX', 'oc_');
This is Local Project Root here, but you can change your Server Path easily.
Note: you can change to config.php and config.php inside admin folder both File Compulsory.
define('DIR_STORAGE', 'D:/xampp/htdocs/opencart_project/system/storage/');
OR
define('DIR_STORAGE', 'D:/xampp/htdocs/opencart_project/storage/');
chcon -R -t httpd_sys_rw_content_t /var/www/...
try this. i've solved this error using the command.
Is looking for the files inside your storage folder. Do this step by step
cd /var/www/opencart_storage/
mkdir logs
chmod 777 logs
cd logs
vim error.log
chmod 777 error.log
vim googleshopping.0.log
chmod 777 googleshopping.0.log
vim ocmod.log
chmod 777 ocmod.log
vim openbay.log
chmod 777 openbay.log
reload your opencart site
Presto!
I faced the same problem. Immediately after the installation appeared in the admin:
Warning: fopen (/ my_path / system / logs /): failed to open stream: Is a directory in /my_path/system/library/log.php on line 6
Here is the solution:
Permissions on the folder with logs after installation, you can leave 755! The file is located at /var/www/my_account/data/www/mysite.com/system/logs/openbay.log need to put permission 777. After that, I left all the mistakes.

php writting to a file from a website not working

Im trying to write to file using php but it doesn't see to work.
$file = fopen("test.txt","w");
fwrite($file,"Hello World!");
fclose("$file");
I published the site using the school server. and the php file/text file are all in public_html folder. The problem is, running this into terminal writes the file. Running it as a website doesn't.
How do i fix this?
P.S. I've tried chmod 644, 777, 755
Remove the quotes in fclose :
$file = fopen("test.txt","w");
fwrite($file,"Hello World!");
fclose($file);
Also, you have to grant all permission : see that answer.
Hmm... is it a UNIX/Linux server?
You probably want to give the group www-data permissions to write the folder.
Assuming your web folder is /var/www, try doing this:
sudo chown `whoami`:www-data /var/www
sudo chmod 775 /var/www
And try again.
If you're working in your personal web folder (http://domain.ext/~yourusername), the commands should be:
chown `whoami`:www-data ~/public_html
chmod 775 ~/public_html
Does it work running the script now?

Categories