Access upload progress through http request in PHP - javascript

Is it possible to measure file upload progress by accessing the http request in PHP? And if so how do i do it while uploading a file into a MySQL DB?
require('../connect_db.php');
//Gather all required data
$name = $dbc->real_escape_string($_FILES['uploaded_file']
['name']);
$data = $dbc->real_escape_string(file_get_contents($_FILES['uploaded_file']
['tmp_name']));
$size = intval($_FILES['uploaded_file']['size']);
//create the sql query
$query = "INSERT INTO `".$user."`(
name,size,data,created
)
VALUES (
'$name','$size','$data',NOW()
)";

For file upload with client side progress I would suggest to use something like this with jQuery plugin-
http://runnable.com/UZKDAYo3XEw2AACX/how-to-upload-a-file-using-jquery-for-php
And I don't think storing files in DB, especially MySQL is good idea.

Related

How to send files to Laravel controller as Normal File elementby using FilePond?

I want to send file to Laravel Controller along with other form fields data, but I am using FilePond.js which sends file as an Async request.
I used FilePondPluginFileEncode plugin and I got file's data as Json object, But I am unable to store data because I didn't find any temp/location of the file.
$file = $request->input('file');
$name = json_decode($file[0])->name;
$data = substr($file[0], strpos($file[0], ',') + 1);
$data = base64_decode($data);
$storage = Storage::disk('local')->put($name, $data);
It stores empty files (with correct name and format)
I want to get file with in Laravel's Controller

Upload blob audio file to aws ec2-instance using php

I am creating a web application where user can record audio and then can upload it to the server.
I am able to record the audio and also was able to send the blob data to the server. But i cannot push file into the ec2-instance (aws hosting enviornment). I am attaching the php code.
I am not sure which path should i use.
Here is the Php Code.
<?php
print_r($_FILES);
$uploaddir = '/var/www/html/public/src/audio/';
$uploadfile = $uploaddir . basename($_FILES['audio_data']['name']);
$size=$_FILES['audio_data']['size'];
$input = $_FILES['audio_data']['tmp_name']; //temporary name that PHP gave to the uploaded file
$output = $_FILES['audio_data']['name'].".wav";
if(move_uploaded_file($input, $$uploadfile)){
echo "uploaded";
}
else{
echo $uploadfile;
}?>
Is there a way to push data and create file on hosted aws-ec2 instance? Is there any changes required in the php file?
This is the image of response from server

Dropzone check max. files PHP side

I'm sending some files through the dropzone.js to my PHP server. I'm having a problem validating the max files being sent PHP side. Even though I will use the maxFiles param for dropzone.js I would like to make sure nothing odd is being sent more times than needed. The main problem is that dropzone can send the file per POST request, so I cannot simply count($_FILES) because there is always gonna be 1.
So I decided to store the temporary uploads in the database using the session id and the field hash and select the count of uploads by that hash.
Unfortunately, that doesn't work as intended, since another request is being sent too fast.
Code is:
if ($request->ajax())
{
$hash = md5(Session::getId() . '_myupload');
$count = DB::query("SELECT COUNT(id) FROM temp_uploads WHERE hash = ?")->param($hash)->execute();
echo $count; // 0
// Store the file
DB::query("INSERT INTO temp_uploads (key) VALUES (?)")->param($hash)->execute();
$request->file('myupload')->store('uploads');
}
I'm uploading two files and the second request is being sent before the INSERT query even finished executing.
How can I properly validate the max files from multiple request in PHP?

mySQLi to Json to Js file

Hello I am trying to output my mysqli database to a js file after encoding it, I have no trouble encoding it with json_encode but how can I get it into a js file (updating every time the mysqli data is updated)
$mysqli = new mysqli('localhost','user','password','myDatabaseName');
$myArray = array();
if ($result = $mysqli->query("SELECT * FROM tablename")) {
$tempArray = array();
while($row = $result->fetch_object()) {
$tempArray = $row;
array_push($myArray, $tempArray);
}
echo json_encode($myArray);
}
$result->close();
$mysqli->close();
Any help or insight would be great! thanks
To return a json file you will have to set json headers at the top of your PHP code:
header('Content-Type: application/json');
If you just want to write the json code into an external file you would have to use PHPs fwrite().
However you can't automatically update the file, when the database is updated. You need to call your PHP file in order to update the json file.
Maybe you can solve this by using a MySQL trigger in your database, more information here.

MySql connection from chrome extension

I have a Chrome extension which communicates with an Arduino Uno with a serial connection and i want to retrieve commands from an online mysql database. I'm a newbie in javascript programming and i can't figure how to access my database through the extension.
This is my php code (it shows the message stored in the database):
if (!empty($_POST)) {
if (empty($_POST['label'])) {
$response["success"] = 0;
$response["message"] = "Inserisci la pass!";
die(json_encode($response));
}
$pass = '***';
if ($_POST['label'] == $pass) {
$cn = mysql_connect("localhost", "***", "***");
mysql_select_db("***", $cn);
$result = mysql_query("SELECT Messaggio FROM tesinafrax ORDER BY numero DESC LIMIT 1");
while($row = mysql_fetch_array($result))
{
$queried_mex = $row['Messaggio'];
echo json_encode($queried_mex);
} } else { $response["success"] = 0;
$response["message"] = "Inserisci la pass!";
die(json_encode($response));
}
exit;
}else{
?>
<h1>Inserisci pass</h1>
<form method="post" action="leggimex.php">
<input name = "label" type="text"><button name="vedi"
>vedi</button></form>
<?php
}
?>
So how do i open the php, write the password in the textbox and parse the json response in order to obtain a string variable on my local chrome extension?
You cant (directly) do that. What you can do is set up a REST API in PHP and use Ajax from within your Chrome extension to interact with your API.
From a high level the flow would be: (client side) Chrome extension needs string from your database -> send an Ajax request to your PHP API (eg. yourserver/getstring) -> (backend) Your API receives the request and gets the string from your DB -> PHP API returns a JSON response -> (client side) Ajax request completes and now you have the string as a javascript variable.
This might seem like a lot, but it is probably the best way. The good news is that REST API/Javascript is a hugely popular topic and so there are a LOT of great resources out there, it will just take some research.
For your PHP backend I would checkout SlimPHP. For the Ajax portion you could use jQuery or just vanilla Javascript.

Categories

Resources