This is a migrated thread and some comments may be shown as answers.

Upload file and save filename to DB

6 Answers 407 Views
Upload
This is a migrated thread and some comments may be shown as answers.
Tony
Top achievements
Rank 1
Tony asked on 29 Nov 2012, 11:31 PM
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

6 Answers, 1 is accepted

Sort by
0
T. Tsonev
Telerik team
answered on 03 Dec 2012, 12:56 PM
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!
0
Tony
Top achievements
Rank 1
answered on 04 Dec 2012, 08:17 PM
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
0
T. Tsonev
Telerik team
answered on 07 Dec 2012, 09:08 AM
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!
0
Tony
Top achievements
Rank 1
answered on 07 Dec 2012, 06:35 PM
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
0
Prima
Top achievements
Rank 1
answered on 23 Jul 2015, 06:43 AM
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!
0
Dimiter Madjarov
Telerik team
answered on 24 Jul 2015, 11:39 AM

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!
 
Tags
Upload
Asked by
Tony
Top achievements
Rank 1
Answers by
T. Tsonev
Telerik team
Tony
Top achievements
Rank 1
Prima
Top achievements
Rank 1
Dimiter Madjarov
Telerik team
Share this question
or