Upload file and save filename to DB

7 posts, 0 answers
  1. Tony
    Tony avatar
    33 posts
    Member since:
    Dec 2011

    Posted 29 Nov 2012 Link to this post

    Hi,

    I'm building several forms where I need to upload files to a directory and save the file name of the uploaded file to a mysql database. I'm using PHP and have the latest KendoUI Complete build.

    I thought I saw a tutorial on this but can't find it anywhere. So, can you please send or post an example in PHP/MYSQL of how to setup a file upload that uploads the file to a directory and saves the filename in the database?

    I think this would be a great tutorial to post on the site docs....or better yet, build the functionality into the next release, in PHP, of course.

    Thanks for the help and I'm looking forward to seeing how these Premium forums work. I love the idea!

    Thanks,
    Tony
  2. T. Tsonev
    Admin
    T. Tsonev avatar
    2772 posts

    Posted 03 Dec 2012 Link to this post

    Hello,

    We have an async upload demo in PHP on GitHub. It shows how to set up the basic save/remove handlers.

    Although it doesn't deal with DB of any kind, it should make a good foundation.

    Kind regards,
    Tsvetomir Tsonev
    the Telerik team
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  3. Kendo UI is VS 2017 Ready
  4. Tony
    Tony avatar
    33 posts
    Member since:
    Dec 2011

    Posted 04 Dec 2012 Link to this post

    Hi,

    Thanks for the reply, however, it does not help solve my problem at all. This php code only saves the file in a directory. I guess I thought that this new Premium Support forum was going to be more helpful in terms of actual support rather than pointing to examples that don't help solve problems.

    In any case, I can write my own implementation. I just need to know how to access the uploaded file name in the php example page assuming one file at a time is uploaded, not multiple file uploads. For example in the code below from the save.php example file, is the file name contained in the $files variable? Or is it in the $name variable? I'll write the php code the update the DB once I know what variable holds the actual file name of the uploaded file. Better yet, can you provide a php example for a single file upload as well?


    <?php
        $fileParam = "files";
        $uploadRoot = "uploads/";
        $files = $_FILES[$fileParam];

        if (isset($files['name']))
        {
            foreach ($files["name"] as $i => $name) {
                $error = $files['error'][$i];

                if ($error == UPLOAD_ERR_OK) {
                    $targetPath = $uploadRoot . basename($files["name"][$i]);
                    $uploadedFile = $files["tmp_name"][$i];

                    if (is_uploaded_file($uploadedFile)) {
                        if (!move_uploaded_file($uploadedFile, $targetPath)) {
                            echo "Error moving uploaded file";
                        }
                    }
                } else {
                    // See http://php.net/manual/en/features.file-upload.errors.php
                    echo "Error code " . $error;
                }
            }
        }

        // Return an empty string to signify success
        echo "";
    ?>

    Thanks,

    Tony
  5. T. Tsonev
    Admin
    T. Tsonev avatar
    2772 posts

    Posted 07 Dec 2012 Link to this post

    Hi,

    I refrained from giving specific DB-related code as it will depend on the specific method you use in your project - PDO, ORM ,etc.

    Anyway, a simple example using PDO would look like this:

    $stmt = $dbh->prepare("INSERT INTO FILES (name) VALUES (:name)");
    $stmt->bindParam(':name', $targetPath);
    $stmt->execute();


    Note that the actual file name is available in $targetPath.

    The example will work with one file as well.

    All the best,
    Tsvetomir Tsonev
    the Telerik team
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  6. Tony
    Tony avatar
    33 posts
    Member since:
    Dec 2011

    Posted 07 Dec 2012 Link to this post

    Hi,

    Thanks! That's perfect, though I won't be using PDO. I really just needed to know that $tagetPath holds the file name for single and multi uploaded files. Now I can setup the update and delete routines for the uploaders.

    Thanks and I'll post my example when it's done so you can have a complete demo for PHP.

    Tony
  7. Prima
    Prima avatar
    1 posts
    Member since:
    Jul 2015

    Posted 23 Jul 2015 Link to this post

    Hy Toni, I thought that you have succeded in developing upload file to database. would you mind to help me how to upload file using kendoUI to database. thank you!
  8. Dimiter Madjarov
    Admin
    Dimiter Madjarov avatar
    2156 posts

    Posted 24 Jul 2015 Link to this post

    Hello Prima,

    I would suggest to check the example provided in the first post, which demonstrates how to configure the save/remove handlers. Regarding the saving of the file to DB, this is a general topic and I am sure many examples, demonstrating it, could be found.

    Regards,
    Dimiter Madjarov
    Telerik
     
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
     
Back to Top
Kendo UI is VS 2017 Ready