文件上传时延迟执行 [英] Delay execution while file uploading
问题描述
我的表单上传了要添加到使用 PHPMailer 创建的邮件中的文件.
不幸的是,邮件没有被发送,我认为这可能是因为它在执行过程中发送得太快了.所以我想做的是添加一个小循环来有效地暂停进一步的执行,直到文件上传完毕:
while (!move_uploaded_file($_FILES['upload'][$first_name.'CV'], $target_path)){睡眠(1);if (move_uploaded_file($_FILES['upload'][$first_name.'CV'],$target_path)){回声文件".basename( $_FILES['upload'][$first_name.'CV'])."已上传";}别的{echo "上传文件时出错,请重试!";}}
这是我想出来的,但我不确定它在这里做什么.
请为我澄清以下内容:
!move_uploaded_file...
的声明是在循环开始时上传文件吗?- 如果是,文件是否在循环的每次迭代中上传?
- 循环中的
if
语句中的move_uploaded_file...
声明是否也在上传文件,还是只是检查文件是否已上传?立> - 处理此类事情的最佳方法是什么?我确定这不是...
提前致谢!
PHP 脚本在文件上传完成之前不会执行.您正在尝试解决错误的问题.
Web 服务器处理请求,包括处理和等待文件数据上传.只有在 Web 服务器收到完整的请求后,它才会调用您的 PHP 脚本.(也就是说,除非您使用一些不寻常的网络服务器.)$_FILES
中存在的任何内容都保证现在.>
My form uploads a file to be added to a mail created with PHPMailer.
Unfortunately, the mail isn't being sent and I think that might be because it's being sent too soon in execution. So what I wanted to do was to add in a small loop to effectively pause further execution until the file has been uploaded:
while (!move_uploaded_file($_FILES['upload'][$first_name.' CV'], $target_path))
{
sleep(1);
if (move_uploaded_file($_FILES['upload'][$first_name.' CV'], $target_path))
{
echo "The file ". basename( $_FILES['upload'][$first_name.' CV'])." has been uploaded";
}
else
{
echo "There was an error uploading the file, please try again!";
}
}
This is what I've come up with but I'm not exactly sure what it's doing here.
Please clarify on the following for me:
- Is the declaration of
!move_uploaded_file...
uploading the file at the start of the loop? - If yes, is the file being uploaded on every iteration through the loop?
- Is the declaration of
move_uploaded_file...
in theif
statement within the loop also uploading the file or is it just checking that the file has been uploaded? - What's the best way to handle this sort of thing? I'm sure this isn't it...
Thanks in advance!
The PHP script will not execute before the file upload is finished. You are trying to solve the wrong problem.
The web server handles the request, which includes handling and waiting for the file data to upload. Only after the complete request has been received by the web server will it invoke your PHP script. (That is, unless you're using some unusual web server.) Anything present in $_FILES
is guaranteed to be there now.
这篇关于文件上传时延迟执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!