9

I am new to slim php framework, I want to upload an image and put the file name in the database via POST, can some one kindly give me some example code.

GrandMasterFlush
  • 6,269
  • 19
  • 81
  • 104
Jasonsti
  • 121
  • 1
  • 1
  • 10
  • Maybe this helps: http://help.slimframework.com/discussions/questions/258-multiple-file-upload-with-slim-framework It is a code example to upload a file. Can you can add storing the name into the database yourself? – Jeroen Noten Jul 29 '14 at 13:22
  • not really i am not getting my head around slim yet, would you kindly do me an example cheers – Jasonsti Jul 29 '14 at 14:22
  • 1
    The Slim Framework doesn't have database functionality by default, so you can just use bare PHP for that. And if you do not know how that works, I suggest you to follow some introduction tutorials into PHP and MySQL. There are thousands of them (if not more). – Jeroen Noten Jul 29 '14 at 14:30

2 Answers2

13

Here's the router:

$app->post('/', 'uploadFile');

this will point to the function below:

function uploadFile () {
    if (!isset($_FILES['uploads'])) {
        echo "No files uploaded!!";
        return;
    }
    $imgs = array();

    $files = $_FILES['uploads'];
    $cnt = count($files['name']);

    for($i = 0 ; $i < $cnt ; $i++) {
        if ($files['error'][$i] === 0) {
            $name = uniqid('img-'.date('Ymd').'-');
            if (move_uploaded_file($files['tmp_name'][$i], 'uploads/' . $name) === true) {
                $imgs[] = array('url' => '/uploads/' . $name, 'name' => $files['name'][$i]);
            }

        }
    }

    $imageCount = count($imgs);

    if ($imageCount == 0) {
       echo 'No files uploaded!!  <p><a href="/">Try again</a>';
       return;
    }

    $plural = ($imageCount == 1) ? '' : 's';

    foreach($imgs as $img) {
        printf('%s <img src="%s" width="50" height="50" /><br/>', $img['name'], $img['url']);
    }
}

If anyone have better answer, please be welcome to alter mine.

Mickäel A.
  • 9,012
  • 5
  • 54
  • 71
Muhaimin
  • 1,643
  • 2
  • 24
  • 48
  • Thanks for this answer! For others' benefit, it should be pointed out that the only thing Slim-specific about this approach is that all the code is placed within a router. Beyond that, this is a generic PHP solution. So, if it's not exactly what you're looking for, consider searching for [PHP file upload](https://www.google.com/search?q=php+file+upload+site%3Astackoverflow.com&oq=php+file+upload+site%3Astackoverflow.com&aqs=chrome..69i57.5974j0j4&sourceid=chrome&ie=UTF-8) and simply placing that code within a router. – rinogo Sep 30 '16 at 18:19
  • can i upload image via `$app->post('/', 'uploadFile');` route?? – Syed Arif Iqbal Nov 12 '16 at 09:51
  • sure mate. That is what I thought – Muhaimin Dec 10 '16 at 02:09
  • Hi can you give me example for this like ...username,password,user image three values i want to pass.I am storing the image in separate folder i need to update the database column image name..I know its possible can you give me example..I am struggling past two weeks – Ramesh sambu Feb 17 '17 at 09:32
6

The creator of Slim has made a library to handle file uploading through Slim: https://github.com/brandonsavage/Upload

Mickäel A.
  • 9,012
  • 5
  • 54
  • 71