I need help - Downloads

If you aren't speaking german, you can ask for support or post your request here.
Antworten
Schwartz
Beiträge: 112
Registriert: 26. Dez 2016, 15:19

I need help - Downloads

Beitrag von Schwartz »

Hello ADMIDIO people ( or whoever can/will help me )

I want to see who makes downloads and the amount.

I have made a table for this ( well - it was made for another purpose ) TEXBOD

In the get_file.php, I have made an include
---------------------------------------------------------------------
header('Cache-Control: private');
header('Pragma: public');

include 'gotfilephp.php';

// Datei ausgeben.
echo readfile($completePath);
----------------------------------------------------------------------------------------------------
In the gotfilephp.php I have :

<?php
$servername = "localhost";
$username = "xxxxxxxx";
$password = "xxxxxxxxxx";
$dbname = "xxxxxxxxxx";

try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO TEXBOD (Download, Fil, Tekst, billede)
VALUES ('Filename', $fileSize, CURRENT_TIMESTAMP,'userid')";
// use exec() because no results are returned
$conn->exec($sql);
echo "New record created successfully";
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}

$conn = null;
?>

----------------------------------------------------------------------------------------------------------
It functions. Each time I download a file, a record is written to the table ( Datei ).
I can get the filesize - this functions OK.

BUT - I can't have the filename or the user to function. I have tried many things - then I thought : This should be a piece of cake for an expert. Unfortunately I am not an expert.

How to get the correct syntax for the filename ( filename or full path or both ) and how to specify the userid ( ID or logged in name ) ? What if a user is not logged in - will it then be a zero value ? I have allowed zero for this field.

I really hope someone can/will help me with this.

I know this is not so elegant. I think it could have been done with 2 lines in the get_file.php but I had to take it stepwise. First I had my gotfilephp.php to write records to the table - then I needed the informations as filesize so I made it with the include this also functioned - but then I was stuck.

Kind regards
Schwartz
XimeX
Developer
Beiträge: 1148
Registriert: 12. Jul 2015, 13:21
Wohnort: Austria

Re: I need help - Downloads

Beitrag von XimeX »

You should get the filename (inkl path) out of "$completePath" and only the filename out of "$filename".
The user from: $gCurrentUser->getValue('usr_id')
Schwartz
Beiträge: 112
Registriert: 26. Dez 2016, 15:19

Re: I need help - Downloads

Beitrag von Schwartz »

Hello Ximex

Very glad You will help.
I had already tried with $completePath and $filename without luck BUT when You told me that it WAS the right way, then I tried again. With no luck. But then I surrounded the variables with a ' .
This did the job. why $fileSize works without the ' is strange to me.
But now I have the filename(s) and the filesize to function.

But not the user. When I use the $gCurrentUser->getValue('usr_id') with the ' then no record is written ( there must be an error, but I cannot se an error message.
When I don't use quotes around the $gCurrentUser->getValue('usr_id')then a record is written, but the value in the field is just : usr_id

I allow a null value in the field, but it does no matter if I am logged in or not.

Any idea what could be wrong ?

I am so close now - I really hope You can help me.

Kind regards
Schwartz
XimeX
Developer
Beiträge: 1148
Registriert: 12. Jul 2015, 13:21
Wohnort: Austria

Re: I need help - Downloads

Beitrag von XimeX »

It's about the " and '

Normaly you should use ' for strings in PHP. With the " PHP also detects variables in the string what _could_ be wrong.
You should end a string and concat it with the variable with the . (dot)

So change:
$sql = "INSERT INTO TEXBOD (Download, Fil, Tekst, billede)
VALUES ('Filename', $fileSize, CURRENT_TIMESTAMP,'userid')";
to:
$sql = 'INSERT INTO TEXBOD (Download, Fil, Tekst, billede)
VALUES (' . $completePath . ', ' . $fileSize . ', CURRENT_TIMESTAMP,' . $gCurrentUser->getValue('usr_id') . ')';

Better (more secure) would be to use prepared statements (we use them intern in admidio starting with v3.3)
Schwartz
Beiträge: 112
Registriert: 26. Dez 2016, 15:19

Re: I need help - Downloads

Beitrag von Schwartz »

Hello Ximex

Again - I am really glad that You will help me with this.

I can have it to function EXCEPT for the user. I don't mind if it is the name of the user as : Admin or Schwartz or it is the number-id as 2 or 3. I just want an identifikation on the user and I can't have it to work. I have changed the script to :
--------------------------------------------------------------------------------------------------
<?php
$servername = "localhost";
$username = "xxxxxx";
$password = "xxxxxxxxx";
$dbname = "xxxxxxxxxx";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO TEXBOD (Download, Fil, Tekst, billede)
VALUES ('$filename', $fileSize, CURRENT_TIMESTAMP, 'navnpåuser')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>
------------------------------------------------------------------------------------------

It is the 'navnpåuser' I have problems with.

I have tried : ( all with and without the ' )
. $gCurrentUser->getValue('usr_id') .
\''.$gCurrentUser->getValue('usr_id').'\'
$gCurrentUser
'usr_id'

I had also in the get_file.php tried to make ( I have slashed it again - it resulted in an error )
// $gCurrentUser->getValue('usr_id')
include 'gotfilephp.php';

Nothing helps.
Either I get an error ( http 500 ) or no error but also no record-writing

Could I do something in the get_file.php to have a variable I can use in the sql in the gotfilephp.php ?
Or do You have another thing I could try ?

it is "only" an identifikation of the (logged-in) user I need.

I really hope You can help.

Kind regards
Schwartz
Schwartz
Beiträge: 112
Registriert: 26. Dez 2016, 15:19

Re: I need help - Downloads

Beitrag von Schwartz »

hello Ximex

It seems that I have had it to function :P

I now have the user-id as a number. I also managed to get the filename with the path, without all the unneccessary stuff.

I made this :

$jmsnavn = $gCurrentSession->getValue('ses_usr_id');
$jmssti = substr($completePath,80);

I am not sure what the difference is between the ses_usr_id and the usr_id, but the ses_usr_id seem to do the job - the usr_id did not function.
And the full path is now 80 unneccessary characters smaller.

Thank You for taking Your time to help me with this - please let me know if there is some traps in using the ses_usr_id.

Kind regards
Schwartz
XimeX
Developer
Beiträge: 1148
Registriert: 12. Jul 2015, 13:21
Wohnort: Austria

Re: I need help - Downloads

Beitrag von XimeX »

As i mentioned above, use ' instead of ".

Code: Alles auswählen

<?php
$servername = 'localhost';
$username = 'xxxxxx';
$password = 'xxxxxxxxx';
$dbname = 'xxxxxxxxxx';

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die('Connection failed: ' . $conn->connect_error);
}
$sql = 'INSERT INTO TEXBOD (Download, Fil, Tekst, billede)
VALUES (' . $completePath . ', ' . $fileSize . ', CURRENT_TIMESTAMP,' . $gCurrentUser->getValue('usr_id') . ')';
if ($conn->query($sql) === true) {
echo 'New record created successfully';
} else {
echo 'Error: ' . $sql . '<br/>' . $conn->error;
}

$conn->close();
?>
Antworten