PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, January 11, 2022

[FIXED] Create File object with CakePHP

 January 11, 2022     cakephp, file, php     No comments   

Issue

Im doing a pŕoject using CakePHP. On this project i need to read a file of ips to add it to my data base. Using just PHP i can read a file and etc, but when i try to use CakePHP its doenst work.

Code from my .ctp file:

echo $this->Form->Create(null, ['type' => 'file', 'url' => ['controller' => 'Ips', 'action' => 'file']]);
echo '<br>' . '<br>';
?>

<div>
<div class="btn indigo darken-3"> 
Informe o Arquivo:
</div>
<div class="btn indigo darken-3">
    <?php
    echo $this->Form->file('file') . '  ';
    ?>
</div>

<?php
echo $this->Form->button('Enviar', ['class' => 'btn indigo darken-3']);
echo $this->Form->end();
?>

Code from my controller:

    public function file() {

       $file_ip = $this->request->data['file'];
       $file_cake = new File($file_ip, true);
       print_r($file_cake);
       exit();
    }

It returns:

Cake\Filesystem\File Object
(
    [Folder] => Cake\Filesystem\Folder Object
       (
        [path] => /*/*/*/rbl_check/tmp/
        [sort] => 
        [mode] => 493
        [_fsorts:protected] => Array
            (
                [name] => getPathname
                [time] => getCTime
            )

        [_messages:protected] => Array
            (
            )

        [_errors:protected] => Array
            (
            )

        [_directories:protected] => 
        [_files:protected] => 
    )

[name] => 
[info] => Array
    (
        [dirname] => /*/*/*/rbl_check
        [basename] => tmp
        [filename] => tmp
        [filesize] => 
        [mime] => 
    )

[handle] => 
[lock] => 
[path] => /*/*/*/*/tmp/
)

How can i create a file object and how i read it? I couldnt use new SplFileObject(), cake return an error.


Solution

$file_cake = new File($file_ip, true);

creates a file object. its a cake convenience class. make sure you use

use Cake\Filesystem\File;

i assume you are, since the object is created. to use the object, try these methods

$contents = $file_cake->read(); //dumps the file's content in a string
$file_cake->append('moo'); //append text at the end of the file
$file_cake->write('moo'); //overwrites the files content...
$file_cake->delete(); // deletes the file
$file_cake->close(); //... you get it...


Answered By - Ralph Thomas Hopper
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing