使用 PHP 上传音频文件

发布于 2024-11-08 02:34:28 字数 62 浏览 0 评论 0原文

我需要建立一个允许注册用户上传音频文件的网站。 我想知道是否有关于安全的防弹实践。 该网站是用 PHP 构建的

I need to make a website that will allow registered users to upload audio files.
I wonder is there any bullet proof practice regarding security.
The site is built in PHP

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

微凉 2024-11-15 02:34:28

检查上传文件的 mime 类型

mp3 -> audio/mpeg

更多信息请参见:http://www.w3schools.com/media/media_mimeref。 ASP

Check mime type of uploading file

mp3 -> audio/mpeg

More here: http://www.w3schools.com/media/media_mimeref.asp

十秒萌定你 2024-11-15 02:34:28

您需要仔细检查文件类型。这意味着只是在文件名上执行子字符串来获取扩展名。扩展名并不是文件实际内容的具体指示。

正如 Danzan 所说,您需要使用如下代码来检查文件的 MIME 类型:

if ($_FILES["audioUpload"]["type"] == "audio/mpeg") {
//proceed with upload procedure
} else {
echo "Only mp3's are allowed to be uploaded.";
}

这可以将用户将恶意 PHP 代码上传到您的上传目录的机会基本上降低到零。

You will want to check the file type carefully. This means not just doing a substring on the file name to get the extension. The extension is not a concrete indicator of what the file actually is.

As Danzan said, you will want to check the MIME type of the file, using some code like this:

if ($_FILES["audioUpload"]["type"] == "audio/mpeg") {
//proceed with upload procedure
} else {
echo "Only mp3's are allowed to be uploaded.";
}

This reduces the chances of a user uploading, say, malicious PHP code into your upload directory to basically zero.

痕至 2024-11-15 02:34:28

通过 getimagesize、fileinfo 扩展和 mime_content_type 函数的组合提供防弹文件类型检查(Nette Framework 属性):

// $file is absolute path to the uploaded file
$info = @getimagesize($file); // @ - files smaller than 12 bytes causes read error
if (isset($info['mime'])) {
   return $info['mime'];
} elseif (extension_loaded('fileinfo')) {
   $type = preg_replace('#[\s;].*$#', '', finfo_file(finfo_open(FILEINFO_MIME), $file));
} elseif (function_exists('mime_content_type')) {
   $type = mime_content_type($file);
}
return isset($type) && preg_match('#^\S+/\S+$#', $type)
    ? $type 
    : 'application/octet-stream';

您不能信任来自客户端的任何数据,因为它们很容易被伪造。

Bullet-proof file type check is provided via combination of getimagesize, fileinfo extension and mime_content_type function (Nette Framework property):

// $file is absolute path to the uploaded file
$info = @getimagesize($file); // @ - files smaller than 12 bytes causes read error
if (isset($info['mime'])) {
   return $info['mime'];
} elseif (extension_loaded('fileinfo')) {
   $type = preg_replace('#[\s;].*$#', '', finfo_file(finfo_open(FILEINFO_MIME), $file));
} elseif (function_exists('mime_content_type')) {
   $type = mime_content_type($file);
}
return isset($type) && preg_match('#^\S+/\S+$#', $type)
    ? $type 
    : 'application/octet-stream';

You can not trust any data coming from the client, because they can be easily forged.

ぺ禁宫浮华殁 2024-11-15 02:34:28

您可以使用 PHP 上传任何内容。这是一个示例: http://www.tizag.com/phpT/fileupload.php

关于安全性,您必须验证是否只允许某些人上传内容,并且您验证他们上传的内容(文件大小、文件类型等)。

You can upload anything with PHP. Here's an example: http://www.tizag.com/phpT/fileupload.php

Regarding security, you have to verify that only certain people are allowed to upload stuff and that you verify the contents of what they're uploading (file size, file type, etc).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文