The Slim Framework support forum has moved to http://discourse.slimframework.com. This Tender forum is no longer maintained or monitored.

Fileupload

Lomion's Avatar

Lomion

23 May, 2012 01:29 PM

Hi,
I'm rebuilding my website with Slim, what a great framework, but at this time I don't know how I go forward.
The problem is a file upload. I have a form like this:
<form enctype="multipart/form-data" id="upload_form" action="{{ action_url }}" method="POST"> <input id="upload-file" type="file" name="file"/> <button type="submit" id="upload-button">Upload</button> </form>

The routing works like a charm and sends me to this:
$app->map('/upload', function () use ($app) { if($app->request()->isPost()) { $upload = new Upload($_FILES['file']); $filename = $upload->upload(); } $filename = "nope"; $app->render('upload.html', array('action_url' => '/upload', 'route' => '/', 'filename' => $filename)); })->via('GET','POST');

In the class Upload the file is processed and i will receive a filename.

...Testing...

Error: The transmitted file isn't there. What did I wrong?

Greetings Lomion

  1. Support Staff 1 Posted by Andrew Smith on 23 May, 2012 02:53 PM

    Andrew Smith's Avatar

    I would suggest checking your web server settings as to why file upload isn't working, also try doing this without using Slim and see if you get the same result.

    Looking at your code it would seem you keep sending 'nope' to the view anyway.

    $app->map('/upload', function () use ($app) { 
        if($app->request()->isPost()) {
            $upload = new Upload($_FILES['file']);
            $filename = $upload->upload(); <!-- The filename variable gets reset below.
        }
        $filename = "nope"; <-- Here
        $app->render('upload.html', array('action_url' => '/upload', 'route' => '/', 'filename' => $filename)); 
    })->via('GET','POST');
    
  2. 2 Posted by Lomion on 23 May, 2012 03:12 PM

    Lomion's Avatar

    Thanks for the reply,

    the webserver works fine. The upload and modify process worked in the old version:
    if($_GET['action'] == 'upload' && isset($_POST)) { $upload = new Upload($_FILES['file']); $filename = $upload->upload(); echo '<img src="upload/thumb/'.$filename.'" />'; }

    You are right with the variable reset, but the error was created in the Upload class (which wasn't changed).

  3. Support Staff 3 Posted by Andrew Smith on 23 May, 2012 03:18 PM

    Andrew Smith's Avatar

    Can you do a

    var_dump($_FILES):
    

    inside the if statement before the Upload class is initialized and see if the 'file' key is empty. If its not empty then I would think there is something wrong in the Upload class.

  4. 4 Posted by Lomion on 23 May, 2012 03:37 PM

    Lomion's Avatar

    var_dump in the routing gives me the following:
    array(5) { ["name"]=> string(10) "example.jpg" ["type"]=> string(0) "" ["tmp_name"]=> string(0) "" ["error"]=> int(1) ["size"]=> int(0) }

    In the __construct of the Upload class gives me the following:
    array(5) { ["name"]=> string(10) "august.jpg" ["type"]=> string(0) "" ["tmp_name"]=> string(0) "" ["error"]=> int(1) ["size"]=> int(0) }

    Why the hell is the array nearly empty?

  5. 5 Posted by Lomion on 23 May, 2012 03:56 PM

    Lomion's Avatar

    Ok, the error is sitting in front of the computer.

    _<' The testing image was corrupted.After using an other file the array was filled up with every content i wanted to have.

    Thank you for the help.

  6. Andrew Smith closed this discussion on 02 Aug, 2012 10:59 AM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac