加上一個數字自動遞增的ID給你的表:
ALTER TABLE `tbl_files`
ADD `id` INT NOT NULL AUTO_INCREMENT FIRST,
ADD PRIMARY KEY (`id`) ;
然後,而不是產生與rand(1000,100000)
使用自動生成的ID作爲文件名隨機數。你得到插入後這個ID與mysql_insert_id:
upload.php
// [snip]
// fetch original file extension
$extension = pathinfo($final_file, PATHINFO_EXTENSION);
$allowedExtensions = ["jpg", "jpeg", "png", "gif", "pdf", "doc", "docx"];
// check if the file extension is allowed
if (! in_array($extension, $allowedExtensions))
{
// report error and abort
}
// use a transaction to rollback the insert
// in case move_uploaded_file fails
mysql_query("BEGIN");
// insert file into database
$sql = "INSERT INTO tbl_files(file,type,size) VALUES('".mysql_real_escape_string($final_file)."','".mysql_real_escape_string($file_type)."','".mysql_real_escape_string($new_size)."')";
mysql_query($sql);
// fetch generated id
$id = mysql_insert_id();
// move file to $folder and rename it to "$id.$extension"
$fileMoved = move_uploaded_file($file_loc,$folder.$id.".".$extension);
if ($fileMoved)
mysql_query("COMMIT");
else
// deletes file entry from the db
mysql_query("ROLLBACK");
我花時間來逃避添加到您的插入,以防止SQL注入。如果可以的話,你真的不應該再使用舊的mysql接口,而是切換到PDO和prepared statements。 view.php?id=1337
<?php
$id = filter_input(INPUT_GET, "id", FILTER_VALIDATE_INT);
if (! $id)
header("HTTP/1.1 400 Bad Request");
$result = mysql_query("SELECT * FROM tbl_files WHERE id = ".$id);
// $id is of type int here, so no sql injection possible
if (! $result)
header("HTTP/1.0 404 Not Found");
$file = mysql_fetch_assoc($result);
// fetch original file extension or store it the database
$extension = pathinfo($file["file"], PATHINFO_EXTENSION);
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' . $file["file"] . '"');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
@readfile("uploads/$id.$extension");
?>
如果您直接訪問文件是可能的,你應該使用它性能方面的原因:
// [snip]
// fetch original file extension or store it the database
$extension = pathinfo($file["file"], PATHINFO_EXTENSION);
// relocate to the pdf file to have apache/nginx/whatever
// serve the file instead of the php interpreter
header("Location: uploads/$id.$extension");
對於其他讀者:
使用id即成您的文件。如果你不不想授予直接訪問權限,但仍然關心性能,可以使用X-Sendfile來提供文件。 nginx本地提供了這個功能。對於Apache而言,有一個模塊可悲地不會出貨。
修復的最終腳本
<?php
include_once 'config_db.php';
if(isset($_POST['btn-upload']))
{
$allowedExtensions = ["jpg", "jpeg", "png", "gif", "pdf", "doc", "docx"];
$file = $id."-".$_FILES['file']['name'];
$file_loc = $_FILES['file']['tmp_name'];
$file_size = $_FILES['file']['size'];
$file_type = $_FILES['file']['type'];
$folder="uploads/";
// new file size in KB
$new_size = $file_size/1024;
// new file size in KB
// make file name in lower case
$new_file_name = strtolower($file);
// make file name in lower case
$final_file=str_replace(' ','-',$new_file_name);
$extension = pathinfo($final_file, PATHINFO_EXTENSION);
// check if the file extension is allowed
if (! in_array($extension, $allowedExtensions))
{
// report error and abort
echo "<script>",
"alert('invalid file extension');",
"window.location.href='index.php?fail'",
"</script>";
}
else
{
$sql = "INSERT INTO tbl_ficheiros(file,type,size) VALUES('".mysql_real_escape_string($final_file)."','".mysql_real_escape_string($file_type)."','".mysql_real_escape_string($new_size)."')";
mysql_query($sql);
// fetch generated id
$id = mysql_insert_id();
// move file to $folder and rename it to "$id.$extension"
$fileMoved = move_uploaded_file($file_loc,$folder.$id.".".$extension);
if ($fileMoved)
{
mysql_query("COMMIT");
echo "<script>",
"alert('successfully uploaded');",
"window.location.href='index.php?success'",
"</script>";
}
else
{
// deletes file entry from the db
mysql_query("ROLLBACK");
echo "<script>",
"alert('error while uploading file');",
"window.location.href='index.php?fail'",
"</script>";
}
}
}
?>
警告:你的代碼不僅容易[SQL注入](http://en.wikipedia.org/wiki/SQL_injection),但它也很容易受到[HTML注入/ XSS(HTTPS ://www.owasp.org/index.php/HTML_Injection)攻擊。 – Phylogenesis
對不起,我可以解釋嗎?我對此很陌生。 在此先感謝;) – Slaxer13
提供的兩個鏈接提供了有關這兩種類型的漏洞的信息。基本上,用戶可以上傳一個專門命名的文件,該文件可以生成不想運行的SQL代碼,或者將不需要的HTML插入到列出上載文件的頁面中。 – Phylogenesis