I'm trying to push a file to browser from SQL via the use of the file token which is created and assigned to the file during the upload process. But my SELECT SQL is not working for anything other then file id field which is the only one that seems to fire up SELECT request here is the code
$item = $_GET['item'];
$sql = 'SELECT * FROM `files` WHERE file_token = '.$item.'';
$result = mysql_query($sql);
if(!$result) {
echo '<div style="padding:8px;background-color:#fae3e3;border:2px solid #b25959;color:#313131;">Error!</div>';
} else {
while($obj = mysql_fetch_array($result)) {
$file_type = $obj['file_type'];
$file_size = $obj['file_size'];
$file_name = $obj['file_name'];
$file_hash = $obj['file_hash'];
$name = 'encrypted/'.$file_hash;
if (file_exists($name)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file_name));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($name));
ob_clean();
flush();
readfile($name);
exit;
}
}
}
mysql_query("UPDATE `files` SET file_views = file_views+1 WHERE file_token = '.$item.'");
mysql_close();
Is there something wrong with my SELECT statement? And tokens look like this in SQL
Example: 3ed3:3ba6:eb24:5816:6d8b:be06:79e1:b20b
mysql_*functions as they are deprecated. Look into using PDO ormysqliinstead. Additionally look in to SQL Injection. What if$GET['item']contained a malicious bit of SQL?