问题
Has anybody been able to implement this functionality?
http://php.net/manual/en/session.upload-progress.php
The tl;dr is that when POSTing a form containing a file, if you include an input with the "name" attribute set to the value of session.upload_progress.name in php.ini, then PHP will populate $_SESSION with data about the status of the upload.
Form:
<form action="upload.php" method="POST" enctype="multipart/form-data">
<input type="hidden" name="<?php echo ini_get("session.upload_progress.name"); ?>" value="123" />
<input type="file" name="file1" />
<input type="file" name="file2" />
<input type="submit" />
</form>
upload.php
(not important but obviously this is where you would normally give the file its actual name and move the uploaded file from the temp to a real directory)
<?php
print_r($_FILES);
?>
session.php:
Run this separate PHP file during the file upload to check for progress
<?php
$key = ini_get("session.upload_progress.prefix") . $_POST[ini_get("session.upload_progress.name")];
var_dump($_SESSION[$key]);
?>
I have tried every fathomable variation in these scripts and in php.ini settings and no matter what the $_SESSION array is never populated with any upload data. Session_start() is called by default. I tried in both v. 5.4 and 5.6. I have tried everything suggested in the comments in the PHP documentation including running with and without FastCGI. I have in general tried everything.
This is important because I am working on a desktop application that uses POST requests to communicate with .php scripts on a web server, I am not using the browser so JS/HTML5 solutions are not part of the picture. It is also impossible to write your own progress measuring functionality in PHP because the script which handles a file upload (upload.php) doesn't actually run until the upload (to the temp directory with a random name) is complete. There is no way to find out what actual file a temp is associated with until the upload is finished and the upload script runs, allowing access to $_FILES. Please help, I have tried every fathomable possibility. Thanks.
回答1:
Form: //Looks Ok
<form action="upload.php" method="POST" enctype="multipart/form-data">
<input type="hidden" name="<?php echo ini_get("session.upload_progress.name"); ?>" value="123" />
<input type="file" name="file1" />
<input type="file" name="file2" />
<input type="submit" />
</form>
Upload.php //Looks Ok too
<?php
print_r($_FILES);
?>
Session.php //recommend some changes here
<?php
session_start();//leave this out if you are session is starting automatically
$key = ini_get("session.upload_progress.prefix") ."123"; //123 value of hidden input
var_dump($_SESSION[$key]);
?>
The major challenge is in your form html view their is no link or call to the session.php. (i dont like the name session.php I have this part of me thats scared it may tamper with $_SESSION scoper). Ideally you can(most folks do) use JS/Ajax call to query Session.php to get result. In which case I'd recommend:
Form:(remember to import jquery before this)
<form action="upload.php" method="POST" enctype="multipart/form-data">
<input type="hidden" name="<?php echo ini_get("session.upload_progress.name"); ?>" value="123" />
<input type="file" name="file1" />
<input type="file" name="file2" />
<input type="submit" />
</form>
<script>
$(document).ready(function(){
$('form').on('submit',function(){
$.get( "session.php", function( response ) {
console.log( response ); // server response-$_SESSION dump
});
})
})
</script>
session.php:
<?php
session_start();//leave this out if you are session is starting automatically
$key = ini_get("session.upload_progress.prefix") ."123"; //123 value of hidden input
var_dump($_SESSION[$key]);//or echo $_SESSION[$key]["bytes_processed"]. I am not sure how the array would playout inside the returned JSON
?>
来源:https://stackoverflow.com/questions/40388885/php-file-upload-progress