Hey guys..
This is the code of a
string test_func(resource $handle) which is supposed to return the size of
resource as a string.
PHP_FUNCTION(test_func)
{
zval *arg1;
php_stream_statbuf stat_ssb;
char *input_stream_len_str;
php_stream *input_stream;
*if* (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &arg1) ==
FAILURE) {
return;
}
PHP_STREAM_TO_ZVAL(input_stream, &arg1);
*if* (php_stream_stat(input_stream, &stat_ssb)) *{*
RETURN_FALSE;
*}*
sprintf(input_stream_len_str,"%d",stat_ssb.sb.st_size);
RETURN_STRING(input_stream_len_str, 1);
}
I'm testing the above code by using test.php
<?php //test.php
$fp = $fopen("file.txt","r");
$str = "samplestr";
$str = test_func($fp);
var_dump($str);
?>
but when I hit http://localhost/test.php Instead of showing the string, my
browser is offering test.php for download. If I click save, its saving a
zero byte test.php to my disk.
If I comment out the function call to test_func() its showing the var_dump
of $str.
I couldn't understand whats going wrong. Please help.
PHP_FUNCTION(test_func)
{
These additional asterisks (*) make the code hard to read.
but when I hit http://localhost/test.php Instead of showing the string, my
browser is offering test.php for download. If I click save, its saving a
zero byte test.php to my disk.
Take a look at your server's error log, it will tell you that the apache
child segfaulted. Use a debugger on the apache process or avoid
complexity and use PHP CLI with a debugger to find the cause.
johannes