FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Can a php page generate a file?





rvbarreto
I'm trying to make a page where you write a file name in a text box, like "test", a content to the file in other text box, like "<html></html>", and then the php file generate a file

test.html

with the tags

<html>
</html>

inside. Is it possible to be done? The file can be generated in my computer? And the file can be generated inside my frihost directory, like "http_docs/test.html" ?

Thanks,

Ricardo Viana
imagefree
i cant give you the solution but it is possible and easy. Someone else may provide you the solution.
ashok
Code:
if($fp = fopen($filepath, 'w+'))
{
        fwrite($fp, $content);
        fclose($fp);
}


$filepath is the path where the file should be stored, $content is the array which contains the content to be written into the file. Hope this helps Smile
mahirharoon
yes , the code is here
Code:

<?php
$filename = test.html; // or use $_REQUEST["fname"] here
$somecontent = <html> test </html>;

if (is_writable($filename)) {

    if (!$handle = fopen($filename, 'a')) {
         echo "Cannot open file ($filename)";
         exit;
    }


    if (fwrite($handle, $somecontent) === FALSE) {
        echo "Cannot write to file ($filename)";
        exit;
    }

    echo "Success, wrote ($somecontent) to file ($filename)";

    fclose($handle);

} else {
    echo "The file $filename is not writable";
}
?>

and answer to your question
Quote:
Is it possible to be done? The file can be generated in my computer? And the file can be generated inside my frihost directory, like "http_docs/test.html" ?

php could not run on a computer unless its run on a server with php installed on it
ok , if you have a server
it cannot be run on client's pc but your server
it could be generated on your frihost htdocs
a funny thing happened to me today
my server is in the network , 1 month ago i made a script that excecuted a window on the server while i thought it was on user's pc
rvec
here is a quick tutorial on how to set up a test server on your own pc in case you want to run the script on your pc.
http://frihost.broadkast.net/forums/viewtopic.php?f=8&t=18
AOP Web Development
You can easily work it by fopen and fwrite function. just try to search on it.
mahirharoon
and if you want to use images for the thing use this
Code:
<?php   
#define variables
$name = test ;
$im = imagecreate(315, 15);
$bg = imagecolorallocate($im, 255, 255, 255);
$textcolor = imagecolorallocate($im, 0, 0, 0);
#write the content
imagestring($im, 3, 0, 0, "$name", $textcolor);
# output png
imagepng($im);
#define header
header("Content-type: image/png");
?>

and use img tags in test.html
ashok
AOP Web Development wrote:
You can easily work it by fopen and fwrite function. just try to search on it.


Exactly. Simple Code I've posted above.

Code:
if($fp = fopen($filepath, 'w+'))
{
        fwrite($fp, $content);
        fclose($fp);
}



$filepath is the path where the file should be stored, $content is the array which contains the content to be written into the file.
SlowWalkere
If all you're doing is writing the contents of a string to a file, I'd use file_put_contents over fopen/fwrite/fclose.

Code:
$filename = "filetowrite.html";
$text = "String to Write to File...";

file_put_contents($filename, $text);


It wraps all of the file calls into one function. You don't need to worry about opening/closing the handle, or specifying the amount of bytes to be written. It does all the work for you.

- Walkere
Related topics
Tutorial: PHP Installed Modules Dynamic Reference Tool
PHP function(); &file=""?
Online file storage
PHP image
PHP Test File
Never Mind Problem Resolved
is there any way to trigger a .php file without refreshing?
PHP Page
help - download pop up on php page
how do i make a window/iframe in a table within php page..??
php page flickering, maybe caused by flash item
send variable from php tp php file
How to make a php page act differently
PHP error page, die()
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.