I have this piece of code:
$fp = fopen("/path/to/file", "a+");
if (!$fp) {
fwrite($fp, "somedata\n");
fclose($fp);
} else {
echo "cannot open";
}
and the result is an empty file and the message cannot open. I have SELinux disabled and the permissions seem OK, otherwise the file wouldn't be created, I'd say.
Any ideas?
Obvious mistake:
if (!$fp) {
should be
if ($fp) {
Related
I have a page on my website that have a really long execution time, i solved the problem of the fact that it was too slow by flushing contents while writing, but now i need to load it from another website, if i had to do it from my website a include would have been enough, but since i need to load it from another website, i thought of using file_get_contents, the problem is that in this way the flushs don't work, and the page take a lot of time to load, i heard that the solution was using fsockopen and fread, so i tried to use some examples, and i get a thing like this
<?php
ob_start();
$fp = fsockopen("www.mysite.com", 80);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
fwrite($fp, "Data sent by socket");
$content = "";
while (!feof($fp)) {
$content .= fread($fp, 1024);
ob_flush();
}
fclose($fp);
echo $content;
}
?>
The problem is that if i do this it work, but when i try something like
<?php
ob_start();
$fp = fsockopen("www.mysite.com/subfolder/index.php", 80);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
fwrite($fp, "Data sent by socket");
$content = "";
while (!feof($fp)) {
$content .= fread($fp, 1024);
ob_flush();
}
fclose($fp);
echo $content;
}
?>
It says me something like:
Warning: fsockopen(): php_network_getaddresses: getaddrinfo failed: No such host is known. on line 2
What am I mistaking?
UPDATE: Solved by myself: the correct code is
<?php
function HTTP_Post($URL) {
ob_start();
$URL_Info=parse_url($URL);
$fp = fsockopen($URL_Info["host"],80);
fwrite($fp, "GET ".$URL_Info["path"]." HTTP/1.0\r\n" );
fwrite($fp, "Host: ".$URL_Info["host"]."\r\n");
fwrite($fp, "Connection: Close\r\n\r\n");
while(!feof($fp)) {
echo fgets($fp, 1024);
ob_flush();
flush();
}
fclose($fp);
}
ini_set('max_execution_time', 300);
HTTP_Post("http://www.corriere.it/cronache/");
?>
for writing i using below code this is very nice and simple work great now i have problem with this we already know that in window 260 character allow for file name problem start from here in code there is option if file not write stop and terminate process (or die("can't open file");) now i want simple if some reason file not write show echo not write and process continue.I try it myslef but it gives me error
my write function
function write($post, $myFile){
$fh = fopen($myFile, 'a+') or die("can't open file");
fwrite($fh, $post);
fclose($fh);
}
If there's a limit on the length of something then you could always check the length of the thing beforehand, and if you don't want your code to exit, then don't use die().
function write($post,$myFile){
if( strlen($myfile) > 260 ) {
echo "Filename too long";
return false;
} else {
if( ! $fh = fopen($myFile, 'a+') ) {
echo "can't open file";
return false;
}
fwrite($fh, $post);
fclose($fh);
return true;
}
}
Try this,
function write($post,$myFile)
{
$fh = #fopen($myFile, 'a+');
if($fh)
{
fwrite($fh, $post);
fclose($fh);
}
else
{
echo "can't open file";
}
}
i have this php code:
<?php
echo ("Setting up data...");
$today = date("YmdHi");
$wtoday = $today
$im = $_GET["im"];
$fim = "tips/$today/im.txt";
$fwtoday = "tips/$today/today.txt";
?>
<?php
$fp = fopen ($fwtoday, "w"); # w = write to the file only, create file if it does not exist, discard existing contents
if ($fp) {
fwrite ($fp, $wtoday);
fclose ($fp);
echo ("Today written");
}
else {
echo ("Today was not written");
}
?>
<?php
$fp = fopen ($fim, "w"); # w = write to the file only, create file if it does not exist, discard existing contents
if ($fp) {
fwrite ($fp, $im);
fclose ($fp);
echo ("Im written");
}
else {
echo ("Im was not written");
}
?>
Finaly Today and Im was not written, where is my error ???
i dont think that have to do with file permissions.
i forgot to write about $fwtoday = "tips/$today/today.txt"; in the post, still not working.
Insert these lines to the front of your file, and share given errors:
error_reporting(E_ALL);
ini_set('display_errors','On');
$wtoday = $today
Missing semicolon, parse error.
That asice, you appear to be attempting to open a filename stored in the variable $fwtoday, which you don't seem to have defined anywhere.
I don't see the definition of $fwtoday. Also you don't need to do this:
?>
<?php
I am working on to send request to VSP200 device, my device is connected to com port8 of windows machine. I am using fopen() of PHP to open the com port, but I am getting an error
Warning: fopen(COM8:) [function.fopen]: failed to open stream
can you please tell me, what is wrong in my code,
$fp = fopen ("COM8:", "w+");
if (!$fp) {
echo 'not open';
}
else{
echo 'port is open for write<br/>';
$string .= '<STX>C30C10178C10100C103110606C103081000C10100C10101C100<ETX>';
fputs ($fp, $string );
echo $string;
fclose ($fp);
}
$fp = fopen ("COM8:", "r+");
if (!$fp) {
echo 'not open for read';
}
else{
echo '<br/> port is open for read<br/>';
$buffer = fread($fp, 128 );
echo $buffer;
fclose ($fp);
}
You should not include the trailing colon in the port name:
$fp = fopen ("COM8", "w+");
I am getting problem while using fwrite in php. the following code works in my local computer but gives error in server.
$fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);
if(!$fp) {
echo 'Error: '.$errno.', '.$errstr;
} else {
fwrite($fp, 'kool');
}
There is no error with fsockopen. it passes and gives no error. fwrite is not being able to write. it fails and returns no error only false
This is a permissions issue with the Apache/Nobody user accessing a remote file that it doesn't have permission to modify/read/write/execute.
You should also print the error message(s) for debugging
$fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);
if(!$fp) {
echo "Error No: ".$errno."<br />\n";
echo "Error Str: ".$errstr."<br />\n";
} else {
fwrite($fp, 'kool');
}
If you're on a shared host, most likely your server does not allow outbound connections on port 80. Usually only inbound connections are allowed.