Welcome Guest, Not a member yet? Register   Sign In
[Solved]Write_file question
#1

[eluser]mrwilson1[/eluser]
I have data that is being written from a form and going into a db, but I am trying to make a txt backup of it as well.
All data validates, get written to the db but the data will not write to the file. The post_backups directory is under applications with the controller, models and views.

Code:
// field name, error message, validation rules
        $this->form_validation->set_rules('post_title', 'title', 'trim|required|htmlspecialchars');
        $this->form_validation->set_rules('post', 'Post', 'trim|required|min_length[5]');    
        $this->form_validation->set_rules('source', 'source', 'trim');
        $this->form_validation->set_rules('owner', 'No owner given', 'trim|required');
        $this->form_validation->set_rules('post_cat', 'Category', 'trim|required');
        $this->form_validation->set_rules('artist', 'Artist', 'trim|max_length[50]');    
        $this->form_validation->set_rules('link', 'link', 'trim|max_length[300]');
        $this->form_validation->set_rules('new_rel', 'New Release', 'trim|max_length[20]');
        $this->form_validation->set_rules('new_artist', 'New artist', 'trim|max_length[20]');
        $this->form_validation->set_rules('album', 'Album', 'trim|htmlspecialchars');
        $this->form_validation->set_rules('social', 'Social', 'trim|htmlspecialchars|max_length[15]|min_length[6]');
        $this->form_validation->set_rules('album', 'Album', 'trim');
        $this->form_validation->set_rules('tag', 'Tags', 'trim');
        $this->form_validation->set_rules('post_status', 'Status', 'required|trim|callback_draft');
        
        if ($this->form_validation->run() == TRUE){        
        $this->post_model->add_record($data);
        $this->post_model->add_record1($data1);
        $this->post_model->add_record2($data2);
        
// problem starts here
        $this->path = "system" . DIRECTORY_SEPARATOR . "application" . DIRECTORY_SEPARATOR . "post_backups" . DIRECTORY_SEPARATOR;
        $post_title = $this->input->post('post_title');
        $this->file = $this->path . $post_title . 'txt';
        $string = $this->input->post('post');
        if(write_file($this->file, $string))
        {
        redirect('post/display_posts');
        }else {
            echo "File not written";
        }
    }

This seems awfully simple but Im not seeing where Im going wrong.
Thanks
#2

[eluser]tomcode[/eluser]
You probably need to change the write permission for the folder post_backups.

Also I would use the CI constant APPPATH :
Code:
$this->path = APPPATH  ."post_backup/";

You can also add a check to verify Your path:
Code:
echo 'is_dir ' .(int) is_dir($this->path);
#3

[eluser]mrwilson1[/eluser]
Thanks, I wondered why I was not getting any kind of an error from this. lemme try what you got.

Thanks again
#4

[eluser]mrwilson1[/eluser]
I get a "is dir() from the test using both the way it was and the APPATH method, the folder is 777

Is this correctly written?

<code>$this->file = $this->path . $post_title . 'txt';</code>
#5

[eluser]tomcode[/eluser]
Do You get 'is_dir 0' or 'is_dir 1' ?

If it's 0, then the path is not correct.

If it's 1, try to write a file with a different name, You use htmlspecialchars in the validation, sounds weird to me to use that for file names, try with a plain ASCII name whether that works.

Quote:Is this correctly written?

$this->file = $this->path . $post_title . ‘txt’;

Yeah.

I'd forget about the post stuff and just try to get the file writing part going with some fake data / filename.

If that works, move on, one by one.
#6

[eluser]mrwilson1[/eluser]
I get the is_dir0 so evidently the path isnt right. that must be why its not writing. I will make sure Im getting to the directory with some simple data then adjust it to the posting in the morning.

Thanks ever so much, I appreciate you
#7

[eluser]mrwilson1[/eluser]
I have everything fixed except for one small problem. When I try to create the filename, the $post title appends itself with the txt.

From
Code:
$this->file = $this->path . $post_title."txt";

I get the title plus the txt. Example titletxt.

What I am trying to get is obviously title.txt. How can I write the $post_title.txt to make it come out right?

Solved

$this->file = $this->path . $post_title.".txt";
#8

[eluser]tomcode[/eluser]
Ah sorry,

I was not exact, it must be :
Code:
$this->file = $this->path . $post_title.".txt"; // ".txt" instead of "txt"
#9

[eluser]mrwilson1[/eluser]
LOL thanks Tomcode, I figured it out about the time you posted. Thank you so much for your help. if I knew how to mark this resolved I would




Theme © iAndrew 2016 - Forum software by © MyBB