gpt4 book ai didi

file-upload - session 上传进度是否适用于 nginx 和 php-fpm?

转载 作者:搜寻专家 更新时间:2023-10-31 21:39:50 25 4
gpt4 key购买 nike

我正在运行 nginx 1.2.3/php-fpm 5.4.6 并尝试使用 Session upload progress特征。在上传过程中,$_SESSION 从不包含任何上传数据。起初我假设编码错误,但即使是最简单/基本的上传进度测试也无法在 $_SESSION 中产生任何内容。我现在怀疑文件被直接发布到 nginx,它从头到尾完全处理上传,然后 nginx 将上传传递给 php-fpm 如此之快以至于没有真正的“进展”报告。我的评估是否正确?如果是这样,我该如何解决这个问题?

phpconfig 确认 session.upload 设置全部正确。

下面是当前的测试代码,借自this blog .

<?php
/* File upload progress in PHP 5.4 */

/* needs a 5.4+ version */
$version = explode( '.', phpversion() );
if ( ($version[0] * 10000 + $version[1] * 100 + $version[2]) < 50400 )
die( 'PHP 5.4.0 or higher is required' );

if ( !intval(ini_get('session.upload_progress.enabled')) )
die( 'session.upload_progress.enabled is not enabled' );

session_start();

if ( isset( $_GET['progress'] ) ) {

$progress_key = strtolower(ini_get("session.upload_progress.prefix").'demo');

if ( !isset( $_SESSION[$progress_key] ) ) exit( "uploading..." );

$upload_progress = $_SESSION[$progress_key];
/* get percentage */
$progress = round( ($upload_progress['bytes_processed'] / $upload_progress['content_length']) * 100, 2 );

exit( "Upload progress: $progress%" );
}
?>

<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>

<?php if ( isset($_GET['iframe']) ): /* thank you Webkit... */ ?>
<form action="" method="POST" enctype="multipart/form-data">
<input type="hidden" name="<?php echo ini_get("session.upload_progress.name"); ?>" value="demo">
<input type="file" name="uploaded_file">
<input type="submit" value="Upload">
</form>

<script type="text/javascript">
window.location.hash = ""; /* reset */
jQuery("form").bind("submit", function() { window.location.hash = "uploading"; });
</script>

<?php else: ?>

<iframe src="?iframe" id="upload_form"></iframe>
<script type="text/javascript">
jQuery(document).ready(init);

function init() {
/* start listening on submit */
update_file_upload_progress();
}

function update_file_upload_progress() {
if ( window.frames.upload_form.location.hash != "#uploading" ) {
setTimeout( update_file_upload_progress, 100 ); /* has upload started yet? */
return;
}
$.get( /* lather */
"?progress",
function(data) {
/* rinse */
jQuery("#file_upload_progress").html(data);
/* repeat */
setTimeout( update_file_upload_progress, 500 );
}
).error(function(jqXHR, error) { alert(error); });
}
</script>

<div id="file_upload_progress"></div>
<?php endif; ?>

最佳答案

php.net 文档页面中的一条注释说:

s.zarges 19-Jun-2012 09:32
Note, this feature doesn't work, when your webserver is runnig PHP via FastCGI. There will be no progress informations in the session array.

不幸的是,PHP 只在上传完成后才获取数据,无法显示任何进度。

关于file-upload - session 上传进度是否适用于 nginx 和 php-fpm?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12502775/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com