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
Related
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.
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.
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.
I'm trying to upload a file (msword/doc) to a Apache server folder via an HTML form. It works when I test it locally (I'm testing it via MAMP), but when I upload it to a remote server (such as GoDaddy), it doesn't work. It shows "There was a problem with the file upload".
Below is the snippet of code that processes the file upload. I can't figure out what is wrong with my conditional.
// Move the file to the target upload folder
$target = FILE_UPLOADPATH . basename($new_file);
if (move_uploaded_file($_FILES['new_file']['tmp_name'], $target))
{
// The new file move was successful, now make sure any old file is deleted
if (!empty($old_file) && ($old_file != $new_file))
{
#unlink(FILE_UPLOADPATH . $old_file);
}
}
else
{
// The new file move failed, so delete the temporary file and set the error flag
#unlink($_FILES['new_file']['tmp_name']);
echo 'There was a problem with the file upload.' . PHP_EOL;
}
Are you sure that the folder you are uploading to has permission for files to be written to it? If not, use chmod 0777 and test with that.
Does your destination folder have proper permissions? http://en.wikipedia.org/wiki/Chmod The directory write to typically needs 775: What are the proper permissions for an upload folder with PHP/Apache?
Also, do you want users to have direct access to the file? If not you should consider writing the file to a folder that is above your web root directory.
If $_FILES['new_file']['error'] == 0 then the upload isn't the problem, but the call to move_uploaded_file() is. You probably have incorrect permissions on the directory you're trying to move the file to.
For me, I happened to be testing locally with a file under php's upload_max_filesize while testing remotely with a file over upload_max_filesize. See https://stackoverflow.com/a/30359278/3325776 for more info.
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);
}
?>