logo
Ask your Symfony questions! Pay money and get answers fast! (more info)

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

$1
Get contents of a file

Hi,

I have a simple form with a file select box and submit button. All I need is for the user to select a text file with my form, click submit, and my code will verify the file is not harmful, and put the contents of the text file into a string. That's it. I don't need to store the file, I don't need to keep it, rename it, or move it. This shouldn't be that hard.

jlvollmer | 06/03/14 at 5:19pm Edit
Tutorial: How to assign prize money


(4) Responses

See a threaded view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    06/03/14
    5:28pm
    Ben says:

    so what is the question? Or are you trying to get somebody to write that code for you for $1?

  • avatar
    Last edited:
    06/03/14
    6:02pm
    Florian Klein says:

    you'll need

    http://symfony.com/doc/current/reference/forms/types/file.html
    and
    http://symfony.com/doc/current/reference/constraints/File.html

  • avatar
    Last edited:
    06/03/14
    9:46pm
    Milena Dimitrova says:

    What is specific in your case is that you do not want to save the file at all. So, the action will look like this:

    use Symfony\Component\HttpFoundation\File\UploadedFile;

    public function uploadAction()
    {
    // ...

    if ($form->isValid()) {

    // instead of saving the file to the server
    // $someNewFilename = ...
    // $form['attachment']->getData()->move($dir, $someNewFilename);

    // you just read the file contents as a string
    $file = $form['attachment']->getData();
    $extension = $file->guessExtension();
    if ($extension == "txt") { // check if the file extension is as required; you can also check the mime type itself: $file->getMimeType()
    $contents = file_get_contents($file->getPathname());
    // do something with the file contents, e.g. store it to the database
    // ...
    }

    // ...
    }

    // ...
    }


    Hope this helps.

  • avatar
    Last edited:
    06/05/14
    2:10am
    jlvollmer says:

    Thank you, that is just what I was looking for. I was almost there.

This question has expired.





Current status of this question: Community pot



Please log in to add additional discourse to this page.





Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.