Rundiz / upload

PHP file upload class for single or multiple files with many validations.
http://rundiz.com/web-resources/php-upload-v2
MIT License
26 stars 9 forks source link

MySql Insertion when uploaded #5

Closed xkpx64 closed 3 years ago

xkpx64 commented 3 years ago

Hello i'am glad that i find that awesome class to upload files, it Works Awesome. I'am developing school site for uploading files, but we need to make when user send file to upload in on database by category. Can you show me some example how to use it. And if possible how to make download.php func or something

I try some query but its not working i dont know how to get proper variables to insert them into db

$data = [ 'name' => //$handle->file_dst_name, $uploaded_data['full_path_new_name'], 'size' => //$handle->file_src_size, 'path' => //$handle->file_dst_pathname,
]; $sql = "INSERT INTO files (fname, fsize, fpath) VALUES (:name, :size, :path)"; $stmt= $link->prepare($sql); if ($stmt->execute($data)) { echo '

Inserted in Database.

'; } else { echo '

Database query failure

'; }

ve3 commented 3 years ago

The example of usage is in tests/via-http folder. It doesn't have download.php. Sorry but that is nothing related to this class.

To get uploaded data, you have to call to getUploadedData() method. It is already shown in repository's readme ( https://github.com/Rundiz/upload ). You have to try access those uploaded data without anything update/insert to DB to understand that what each data is. But they are already explained in readme either. I can't know what you want to use because each array key from getUploadedData() method is different, you have to play with it and understand what you really need before continue on your work.

xkpx64 commented 3 years ago

I just wanted to do something like this from vfileup $sql = "insert into table (file_upload, file_size, file_type) VALUES ('".$upload_path."/".$data['file_name']."', ".$data['file_size'].", '".$data['file_type']."')";

I find something that shows but i dont know how good it is. for ($i=0; $i < count($uploaded_data); $i++) { $name =$uploaded_data[$i]['name']; $size =$uploaded_data[$i]['size']; echo $name; echo $size; //mysqli_query($conn,"INSERT INTO files (name, size) VALUES ($name,$size)"); }

ve3 commented 3 years ago

getUploadedData()

The data from getUploadedData() method contains array. Each array contain 7 keys (as version 2.x).

Each key meaning.


I assume that you want full_path_new_name for fpath column, new_name for fname column, size for fsize column.

Then you can do this...

$uploadedData = $Upload->getUploadedData();
if (is_array($uploadedData)) {
    foreach ($uploadedData as $index => $item) {
        $sql = 'INSERT INTO `files` (`fname`, `fsize`, `fpath`) VALUES (:name, :size, :path)';
        // debug to make sure that your sql statement is correct.
        $sql = str_replace(':name', $item['new_name'], $sql);// replace for debug only.
        $sql = str_replace(':size', $item['size'], $sql);// replace for debug only.
        $sql = str_replace(':path', $item['full_path_new_name'], $sql);// replace for debug only.
        echo $sql . '<br>' . PHP_EOL;
        // I suggest do not un-comment the code below until you sure that it is correct.
        //$stmt= $link->prepare($sql);
        // your insert code.
    }
    unset($index, $item);// free memory.
}

You maybe use your for..loop style. That's looking good, it should run with no problem.

xkpx64 commented 3 years ago

God bless you Sir!