Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6193197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:04:39+00:00 2026-05-24T03:04:39+00:00

im having small problem within my file creator class . im a little bit

  • 0

im having small problem within my file creator class .
im a little bit new to OOP so i think i made a fault
here is the class

<?php 
class Files{
    public $filename ;
    public $content ;
    function Files($filename)
    {
        $this->filename = $filename;
    }
    function createfile()
    {
        $file = fopen($this->filename, "w+") or die('cant create file ');
        return $file ;
    }
    function writetofile($content)
    {
        fwrite($this->createfile,$content) ;
    }
    function closecon()
    {
        fclose($this->createfile);
    }
}
?>

and here is how i use it

<?php 
include 'classes/class.files.php';
$create = new files('tmp/index.html');
$content = '<html>
<body>

<h1>My First Heading</h1>

<p>My first paragraph.</p>

</body>
</html>';
$create->createfile() ;
$create->writetofile($content) ;
$create->closecon() ;
?>

when i call test.php file it give me this error

Warning: fwrite(): supplied argument is not a valid stream resource in C:\AppServ\www\cms\classes\class.files.php on line 16

Warning: fclose(): supplied argument is not a valid stream resource in C:\AppServ\www\cms\classes\class.files.php on line 20
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T03:04:40+00:00Added an answer on May 24, 2026 at 3:04 am

    You need to store the file pointer resource in a property (instead of calling createfile everytime). Also, you’re not even calling createfile but referencing a non-existent property (for which you should get a notice). Try something like this:

    class Files{
        public $fp;
        public $filename ;
        public $content ;
        function Files($filename)
        {
            $this->filename = $filename;
        }
        function createfile()
        {
            $this->fp = fopen($this->filename, "w+") or die('cant create file ');
        }
        function writetofile($content)
        {
            fwrite($this->fp, $content) ;
        }
        function closecon()
        {
            fclose($this->fp);
        }
    }
    

    Also, your code is not very PHP5-ready. Your constructor should be called __construct (not Files) and your methods should have explicit visibility. I also recommend you use the exact case when instanciating your classes:

    class Files{
        public $fp;
        public $filename ;
        public $content ;
    
        public function __construct($filename)
        {
            $this->filename = $filename;
        }
    
        public function createfile()
        {
            $this->fp = fopen($this->filename, "w+") or die('cant create file ');
        }
    
        public function writetofile($content)
        {
            fwrite($this->fp, $content) ;
        }
    
        public function closecon()
        {
            fclose($this->fp);
        }
    }
    
    $create = new Files('tmp/index.html');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a small issue here: <div id=navbar> <ul id=navtabs class=floatcontainer> <li <?php if
I'm having small problem (I guess) of showing the JTable panel. I have class
im having a problem with a small issue within this query $update = mysql_query(UPDATE
I am new to ajax and am having what i think is a small
I am having a small problem geting to some data within my MVC C#
i am having a small problem here. The table which dbml (LinqToSql designer) is
i'm having a small problem here. I'm using a simple rule to redirect all
I'm having a small problem with my setup. I'm quite new to the Django
Im having a small problem with storage of special characters like quotes, double quotes
I'm having a small problem in Java. I have an interface called Modifiable. Objects

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.