This may be marked as duplicate, but everything else I tried here didn't work. I am trying to create a file in PHP using this:
<?php
$con = $sup;
$fp = fopen($_SERVER['DOCUMENT_ROOT'] . "/var/www/HDdeltin/" . $sup , "wb");
fwrite($fp,$con);
fclose($fp);
?>
I am using ubuntu(xfce) What is wrong with this?
$fp = fopen($_SERVER['DOCUMENT_ROOT'] . "/var/www/HDdeltin/" . $sup , "wb");
is incorrect: normally $_SERVER['DOCUMENT_ROOT']
is already /var/www/
. Try
$fp = fopen($_SERVER['DOCUMENT_ROOT'] . "/HDdeltin/" . $sup , "wb");
or
$fp = fopen("/var/www/HDdeltin/" . $sup , "wb");
depending on whether you want your file under the document root or under a specific location.
This is what I came up with:
<?php
$text = "This will be placed in your file.";
$con = "test.txt";
$fp = fopen($_SERVER['DOCUMENT_ROOT']. "/HDdeltin/$con", "wb");
fwrite($fp,$text);
fclose($fp);
?>
This seems to works if the folder HDdeltin exists, otherwise it will return an error.
$text
would be the replacement of your $sub
because you didn't declare one. And I removed the /var/www/
part since this is exactly what $_SERVER['DOCUMENT_ROOT'];
should return.
<?php
$con = $sup;
$fp = fopen($_SERVER['DOCUMENT_ROOT'] . "/var/www/HDdeltin/" . $sup , "wb");
fwrite($fp,$con);
fclose($fp);
?>
I don't know if you can use the last parameter 'wb' for the fopen function. Try to use only 'w' option then let us know if it works.