php cronjob doesn't write to file - php

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.

Related

unable to open file with php on ubuntu machine

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.

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.

uploading file doesn't work in linux

So I have code for uploading image to server that actually work when I tested it on the windows system, but as soon as I put it on Linux, it is doesn't work. So problem is that it doesn't give me any mistake, in the end it put the link on data base, but doesn't put the image in the folder where it spouse to be. So the problem is only in uploading image in server.
So here is the code where the path and image download.
$path = "../users/".$IDN."/";
if(preg_match('/[.](JPG)|(jpg)|(jpeg)|(JPEG)|(gif)|(GIF)|(png)|(PNG)$/',$_FILES['imgupload']['name']))
{
$filename = $_FILES['imgupload']['name'];
$source = $_FILES['imgupload']['tmp_name'];
$target = $path.$filename;
move_uploaded_file($source, $target);
// and other funny actions
What is the owner/group of that ../users/$IDN? Could you print out the output of following command:
ls -l ../users
Also what is your web service? Is it apache? What username your web server is running with?
If your web service user does not have permission to write in target directory, then you can't really write unless you change permission.
Also try to change error_reporting to ALL in your php.ini to see the error message.

PHP: fopen can't create file on servers public_html

on my localhost my script generates a file that does not exist and everything seems to work. now i have uploaded script on the server. i been trying to create a file on the servers public html. but fopen cannot create the file
$file = '/home/mysite/public_html/contacts.xml';
chmod($file, 0777);
if(!($f = fopen($file, "w"))){
echo "error openning file<br />";
}
I always get 'error openning file'.
you must set the chmod 777 via FTP or web administration interface of your webserver for security is not possible to set via PHP

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);
}
?>

Categories