unable to open file with php on ubuntu machine - php

I trying to create new file in Ubuntu system using PHP script,
but when I run this script the error
Unable to Open file
is appear
although I sure that the file's path is right and I have permissions to access this file I don't know where is the wrong
this is my code
$myfile = fopen('inc/users/future.php', "w")or die("Unable to open file!") ;
$text='<?
$host ="'.$host.'";
$user ="'.$db_admin.'" ;
$pass ="'.$db_password.'";
$db ="'.$database.'" ;
$myconn;?>';fwrite($myfile, $text);
fclose($myfile);
the path of this script is
/var/www/html/ghost/index.php
and the path of the file which I wish to open is
/var/www/html/ghost/inc/users/future.php
in other hand when I run this script in windows machine every thing is go fine

In your script use
fopen(dirname(__FILE__) . '/inc/users/future.php', 'w')
This will create a filepath from the directory your index.php. If you script is called from another file, php might search coming from that file.
Also check if the php process has sufficient file permissions to read and open the file. Try setting the file to chmod 777 just to test if that is the case (do not leave it on 777 though).
Keep in mind that if the file is a symbolic link, the 'w' parameter of fopen will not work.

Related

php cronjob doesn't write to file

i'm writing a simple php script which made some stuffs every hour and write a log file.
The issue is cronjob doesnt write to log file, is it a permission issue? is it a path issue?
I have an hosting service by siteground. Script file has permission to write, read and execute.
I've already try to set a cronjob which just send me an email without write a file and it's running.Thanks
This is my code:
//
//some stuffs here
//
$file='www.domainname.com/logfile.csv';
$handle = fopen ($file, "w");
fwrite($handle, "hello, i done some stuffs");
fclose($handle);
You should use the absolute or relative path to the file on the file system.
example: -
$handle = fopen( $_SERVER['DOCUMENT_ROOT'] . '/logfile.csv' );
Generally having a writable file in the web servers directory is a bad idea. If your program can write to the file, then the web server can write to the file too. That opens the possibility for someone to overwrite the file through the web server. At best you lose data from being over written. At worst it allows remote code execution.
It would be better to put the writable files outside the web server directory.
<Parent directory>
<Place for writable files>
<Web server directory>
In your programs run from the webserver you just need to specify the path to the files.
Relative Path
$relitivePath = '/../writeDir';
$handle = fopen( $_SERVER['DOCUMENT_ROOT'] . $relitivePath . '/logfile.csv' );
Full Path
$fullPath = '/somedir/ParentDir/writeDir'
$handle = fopen( $fullPath . '/logfile.csv' );
Running from cron a bit different than running it from shell. The environment you are use to is not set up like yo have when sshing in. Your going to have to list the full path or set up the environment.
$fullPath = '/somedir/ParentDir/writeDir'
$handle = fopen( $fullPath . '/logfile.csv' );
To get your full path
When ssh'ed in:
cd to the directory where the file located.
Do a pwd this will print the current working directory path.
Use that path in like above.
In the program it is a good idea to turn error reporting on.
You also don't want to have the program you call from cron in the web directory.
I see your service provider has a way to email any output from cron. That will help with error reporting.

fwrite function not able to find directory other than local server

I am using codeigniter with php. I am trying to write image in my file structure(Ubuntu file structure). This image is coming from url. My code to write image is
$content = file_get_contents($url);
$filename = "myImage.jpg";
$filepath = "/opt/lampp/htdocs/affiliatedSystem/assets/file/".$filename;
//Store in the filesystem.
$fp = fopen($filepath , "w");
fwrite($fp, $content);
fclose($fp);
This code works perfectly fine on localhost but when I uploaded project on server its giving error like this
Message: fopen(/opt/lampp/htdocs/affiliatedSystem/assets/file/myImage.jpg):
failed to open stream: No such file or directory I also gave file
permission to folder 777 on server.
So, How can I write image on server?
(i can't add a comment, so i write in the answer. But is it the solution?).
I had the same problem on Fedora Linux. The owner of the folder was me and no apache. Try to change the owner folder with your user apache (if you use apache).
First, active httpd_unified :
setsebool -P httpd_unified on
Second, give right to user apache :
chown yourApacheUser:yourApacheGroup /var/www/..../yourDirectory
Finally, I get the answer, I am using $_SERVER['DOCUMENT_ROOT'] function at the starting of url like this$_SERVER['DOCUMENT_ROOT']."/affiliatedsystem/assets/file/".$filename;
and it gives me right url of my server and I am able to write file in my directory on server.

Access log won't log after replacing with a new log file

I'm trying to phrase access log files on my Nginx server.
For phrasing the file, I simply rename the original access log file and create a new access log file immediately so I won't miss anything.
But after replacing the file, Nginx won't log anything onto that file but works until I replace the file.
Nginx start logging again to the replaced file after I restart Nginx.
I can not see what I'm doing wrong, any help?
First bit of the PHP code
if(rename("access.log", $tempname)){ // I'm renaming the access log file
$fp = fopen("access.log","wb");
if( $fp == false ){
}else{
fwrite($fp,$content); // I'm creating a new access log file
fclose($fp);
}
// I'm phrasing the renamed file here
}
As I said in my comments it probably not possible to remove the file due to the nature of nginx, my suggestion would be using the same approach, but without actually removing the log file. Instead just clear it.
Pseudo Code
file = open "nginx.log", READ
new_file = open tmpname, WRITE
new_file.write file.contents
file.close
new_file.close
sys_command "cat /dev/null > nginx.log"
Or using a script
#!/bin/bash
cp nginx.log nginx.backup.log
cat /dev/null > nginx.log
This way you are not destroying the file and the file handle that nginx has will still be valid.

File rename failing on web server

I have the following script which works fine locally:
<?php
//Report all errors
error_reporting(E_ALL);
if ($handle = opendir('instance/system/application/images/dir/testimages/')){
while (false !== ($fileName = readdir($handle))){
$newName = str_replace(" ", "_", $fileName);
rename($fileName, $newName);
}
echo "All files have been renamed";
closedir($handle);
}
?>
However when run on the web server I get the following error:
Warning: rename(.,.) [function.rename]: Permission denied in C:\inetpub\vhosts\domain.com\httpdocs\rename.php on line 10
Any idea how I can resolve this?
Cheers
Note:
I am using IIS7 and a windows server.
Couple of things:
In windows, go to the directory where you want to rename files, right click, and look at the permissions for both the folder and the files within. Whatever the process is running the web server needs to have permissions to modify those files, or create new files in the directory.
Secondly, and more subtly - I think the root cause is a bug. In the PHP manual, there's a comment suggesting that rename will put the file in the current working directory unless you specify your full directory path in the "to" argument.
So, if you are trying to rename "c:\instance/system/application/images/dir/testimages/banana pic.jpg" to banana_pic.jpg, your current code will write that to the current working directory - probably the location of php.exe - that will fail.
I'd try to specify the folder in which you want the renamed file to be placed, and see if that works...
Set permission to files You want to rename to 777.
In file manager, like, filezilla, right click on file and set permissions.
Or just google: set permissions to files on server.
You can try through PHP too: http://php.net/manual/en/function.chmod.php
In order to rename or chmod a file, you need to have access to do that. This means that the file should be either owned by the webserver process, or should be with appropriate mods that allow it to be changed by anyone (like 777)
NOTE: This comment is not appropriate for a windows server. I didn't realise this when I commented. This is appropriate for linux, not windows.
You do not have permission to edit the image so need to chmod() the file:
The chmod() function changed the permissions of the file so you can rename it. The "777" means that anyone can (for a millisecond) change, read and execute the file. Then you write it back to "644", so that anyone can read, but only you can change the file. This second step is for security. You don't want files on your server editable and executable by everyone.
<?php
//Report all errors
error_reporting(E_ALL);
if ($handle = opendir('instance/system/application/images/dir/testimages/')){
while (false !== ($fileName = readdir($handle))){
$newName = str_replace(" ", "_", $fileName);
chmod($fileName, 777);
rename($fileName, $newName);
chmod($newName, 644);
}
echo "All files have been renamed";
closedir($handle);
}
?>

php fopen can't find file that definitely exists

I'm trying to load data from a CSV on my Windows PC into a database, something I've successfully done previously. fopen can't find my input file.
Here's the specific code I'm having trouble with:
<?php
ini_set('track_errors', '1');
$handle = fopen("C:/Users/Sam/Documents/test.csv", 'r') or die("can't open file: $php_errormsg");
?>
The error printed is:
[function.fopen]: failed to open stream: No such file or directory
The file definitely exists, and I get the same problem on Unix machines. How do I fix this?
Windows 7 (and Vista?) only lets a user access his own home directory and does not allow Apache (or other users) to. Unfortunately, this is a major headache, and I would suggest that you just move the file somewhere public.
This type of behavior is easier to fix in Linux, but you're still better off moving the file out of your directory into some path where Apache has read access.

Categories