Php file create error

PHP can’t write files

The problem is that it’s always a failure, even after I did a chown apache:apache /var/www/html/* or a chmod 777 * in that directory, so anyone knows a way to understand / fix this? EDIT : So there the problem was thath the directory itself did’nt have the chown

Can you try creating a file in the directory? That should isolate whether its a permissions issue or a problem with coding..

Are you creating your file and running your script in that directory or a sub-directory of /var/www/html/ ?

Use -R option with chmod and chown to recursively apply the mods. Yet, 777 is the worst chmod you can set. Use a more decent one like 640 on files and 750 on directories.

If you only did a chmod «in that directory», the problem is likely that there is no permission to write on «that directory». So if you are writing to /var/www/html/ and failed, check the permissions on /var/www/html/ . Changing the permission on a file in the directory will only help if the file already exists. Changing the permission of the directory you are writing to will allow you to create a new file.

2 Answers 2

It’s hard to say what it is without the actual error message from PHP.

Long story short, create and set a custom error handler, like so:

Error: [$errno] $errstr"; > //set error handler set_error_handler("customError"); //trigger error $file = fopen($filename,"w") ?> 

In your case, I think that in order for it to be able to create new files in the given directory, you would want to add a ‘-R’ flag to chmod or chown and call it on the directory itself rather than the children — that way, if PHP has to create the file, it has permissions to do so.

Читайте также:  Javascript assign this in function

EDIT: Just curious about why this has a downvote — what is the «egregiously sloppy, no-effort-expended post, or an answer that is clearly and perhaps dangerously incorrect.»? I told the OP to examine the error messages and set their permissions in a properly recursive fashion.

Источник

File cannot be created in my PHP code

I am trying to debug the following PHP file, which is used to serve the check in request from the mobile application, so I cannot use print out to check the flow of the program, therefore I would like to create some files, so I can know how the programme is running. But I have no idea where no file is being created, even I have successfully inserted the record into the database, can anyone give help to me Here is my PHP code:

$_Type = $_POST['Type']; $_ClientID = $_POST['ClientID']; $_TechID = $_POST['TechID']; $_SiteID = $_POST['SiteID']; $content = "First check"; $fp = fopen("C:/xampp/htdocs/Me/checking.txt","wb"); fwrite($fp,$content); fclose($fp); function checkin($DB, $TechID, $ClientID, $SiteID) < $dbConnection = mysql_connect($DB['server'], $DB['loginName'], $DB['password']); if(!$dbConnection)< die('Error! ' . mysql_error()); >mysql_select_db($DB['database'], $dbConnection); $result1 = mysql_query("SELECT COUNT(*) FROM Log") or die('Error! ' . mysql_error()); $query = "SELECT `Type` FROM `Log` WHERE `TechID` = '".$TechID."' ORDER BY LogTime DESC LIMIT 1"; $result2 = mysql_query($query) or die('Error! ' . mysql_error()); while($row1 = mysql_fetch_array($result1)) < $count = $row1['COUNT(*)']; if(mysql_num_rows($result2) >0)< while($row2 = mysql_fetch_array($result2))< if(trim($row2['Type'])!="Checkin")< $count = $count+1; $timezone = "Asia/Hong_Kong"; if(function_exists('date_default_timezone_set')) date_default_timezone_set($timezone); $Time = date('Y/m/d H:i'); mysql_query("INSERT INTO Log (LogID, TechID, ClientID, SiteID, LogTime, Type) VALUES (".$count.", '".$TechID."', ".$ClientID.", ".$SiteID.", '".$Time."', 'Checkin')"); >else < mail('me@gmail.com', 'Test mail', 'If you can read this, everything was fine!'); $content1 = "First 1"; $fp1 = fopen("C:/xampp/htdocs/Me/checking1.txt","wb"); fwrite($fp1,$content1); fclose($fp1); >> >else < $count = $count+1; $timezone = "Asia/Hong_Kong"; if(function_exists('date_default_timezone_set')) date_default_timezone_set($timezone); $Time = date('Y/m/d H:i'); mysql_query("INSERT INTO `Log` (`LogID`, `TechID`, `ClientID`, `SiteID`, `LogTime`, `Type`) VALUES (".$count.", '".$TechID."', ".$ClientID.", ".$SiteID.", '".$Time."', 'Checkin')"); >> > checkin($DB, $_TechID, $_ClientID, $_SiteID); 

As you can see, this is a program used to insert the check in record, but if person want to check in again before he has checked out, a file checking1.txt should be created. However, none of the checking.txt or checking1.txt is created after the execution.

Источник

PHP — Unable to create file

I am trying to create a file on my web server with an option chosen from a web form. The file never gets created and I keep getting the «Can’t Open File» message.

 $file = "bootsstyle"; if(!file_exists($file)) < touch($file); chmod($file, 0777); >$openFile = fopen($file, "w+") or die("Can't open file"); fwrite($openFile, $boots); fclose($openFile); ?> 

I have been scouring the Internet for an hour and am not sure where I am going wrong. I have checked the permissions in the directory and they are 0777.

Try to check output of php_info, it might be that local file operations are disabled for security reasons?

Well; does the file_exists get called? Does that barf out? If the file exists you might not have access to the file anyway..

Since you use the w+ flag in open, touch is not necessary and chmod seems a bad idea to me. (at least with 777).

2 Answers 2

$boots = "your data"; $file = "bootsstyle"; if(!file_exists($file)) < touch($file); chmod($file, 0777); >$openFile = fopen($file, "w+") or die("Can't open file"); fwrite($openFile, $boots); fclose($openFile); $myfile=fopen($file, "r"); echo fread($myfile,filesize($file)); fclose($myfile); 

The problem is with the permissions for the directory in which you are trying to create the file. The directory in which the php script is located must be writable by the user group named www-data. There are two methods to solve this.

Method 1: Changing permission of directory

You can just change the permission of directory to 777. To do this, open a terminal and navigate to the directory in which your php script resides. Now execute the command

Method 2: Making www-data as owner of directory

Open a terminal and navigate to the directory containing the php script. Now, execute the command

sudo chown www-data ./ && sudo chmod 774 ./

You can learn more about Linux permissions and the difference between 774 and 777 from
https://linode.com/docs/tools-reference/linux-users-and-groups/

Note:
Opening a file in w+ mode create the file if it does not exist. So you do not have to touch it.

Note:
The above code is tested on kali linux 2017.3.2 running LAMP server.

Источник

PHP fopen() not creating file if it doesn’t already exist

As part of application logging, I’m attempting to open a local file, and if that file doesn’t already exist, to create the new one. Here’s what I have:

$path = '/home/www/phpapp/logs/myawesome_logfile.txt'; $f = (file_exists($path))? fopen($path, "a+") : fopen($path, "w+"); fwrite($f, $msg); fclose($f); chmod($path, 0777); 

I’ve double-checked, and the /logs directory is chmod 0777, and I even went the extra step of chown’ing it to apache:apache for good measure. Still, when the script goes to open the file, it gives me the warning that the file doesn’t exist and bombs out. No file is ever created. Do I need to suppress the fopen() warning to get it to create the file?

Do you really mean to have your path start at root? That won’t be your app’s root directory, that will be your system’s root directory.

6 Answers 6

When you’re working with paths in PHP, the context can matter a great deal. If you’re working with urls in a redirection context — then the root directory (‘/’) refers to your domain’s root. The same goes for paths for linking files or images and for include and require directives.

However, when you’re dealing with file system commands such as fopen , the root directory (‘/’) is the system root. Not your domain root.

To fix this, try giving the full path to the log file you want to open from the system root. For example: /var/www/phpapplication/logs/myLogFile.txt

Or you could use $_SERVER[‘DOCUMENT_ROOT’] as suggested in other answers to access your server’s stored value for the path to the document root. The /var/www part.

You can also use the __DIR__ magic constant in some cases. Note that __DIR__ will be the directory the current file is in, which is not necessarily the same as your application’s root. So for example, if your application’s root is /var/www/application and you’re working in /var/www/application/src/controllers/my_controller.php , then __DIR__ will be /var/www/application/src/controllers . See here in the PHP documentation.

Источник

Оцените статью