【发布时间】:2014-05-08 17:20:31
【问题描述】:
我有一个接受用户发送的文件的基本 PHP 脚本。这段代码只是一个简单的 API,用户通过 POST 请求向我们发送文件。
我想知道如何让这个程序同时处理超过 3000 个用户发送文件?我需要使用线程吗?最好的解决方案是什么?
在用户网站上:(www.example.com)
<form action="http://www.mywebsite.com/file.php" method="post" enctype="multipart/form-data">
Your Photo: <input type="file" name="photo" size="25" />
<input type="submit" name="submit" value="Submit" />
</form>
这是我服务器上的代码(mywebsite.com)(file.php):
//if they DID upload a file...
if($_REQUEST['photo']['name'])
{
//if no errors...
if(!$_REQUEST['photo']['error'])
{
//now is the time to modify the future file name and validate the file
$new_file_name = strtolower($_REQUEST['photo']['tmp_name']); //rename file
if($_REQUEST['photo']['size'] > (1024000)) //can't be larger than 1 MB
{
$valid_file = false;
$message = 'Oops! Your file\'s size is to large.';
}
//if the file has passed the test
if($valid_file)
{
//move it to where we want it to be
move_uploaded_file($_REQUEST['photo']['tmp_name'], 'uploads/'.$new_file_name);
$message = 'Congratulations! Your file was accepted.';
}
}
//if there is an error...
else
{
//set that to be the returned message
$message = 'Ooops! Your upload triggered the following error: '.$_REQUEST['photo']['error'];
}
}
提前致谢
【问题讨论】:
-
注意安全。该名称没有被正确清理,例如,我可以在文件前加上 ../ 并将文件(我想要的)放在我有权限的任何地方。您应该查看您的 php 设置、内存设置、网络服务器选择和设置以及硬件选择。
-
是的,我明白你的意思了,谢谢Ronni,我可以稍后对其进行清理,我需要解决多个用户同时发送我的文件的问题,我该如何处理?
-
您需要执行压力测试以查看您一次可以处理多少用户,并计划您如何处理峰值(本地或通过第三方,有很多类似这样的服务)值。可能您已经可以处理您想要(或不想要)的用户数量,但无论如何测试都会为您提供更好的指标。您还可以选择一个为您自动扩展的托管服务提供商。
-
是的,我认为你是对的,我需要做压力测试,但我该怎么做呢?你有什么推荐给我吗
-
stackoverflow.com/questions/7492/… 或相关链接之一。另一种选择是一些可扩展的云托管。最后,有很多服务可以进行压力测试(第 3 方),例如 loader.io