开发者

Is there a way to run some script before the upload starts in php?

开发者 https://www.devze.com 2022-12-24 05:47 出处:网络
Can you run some script before the uploading of a file starts in php?Example, I\'m POSTing to upload.php, and in that file I want to check their $_SE开发者_StackOverflow社区SSION first before I start

Can you run some script before the uploading of a file starts in php? Example, I'm POSTing to upload.php, and in that file I want to check their $_SE开发者_StackOverflow社区SSION first before I start wasting bandwidth on them and the file stats uploading to my server. I'm using php 5.2.11 on nginx.

<?php
  if ($_SESSION['admin'] == 'YES') {
    // do upload here
  } else {
    exit;
  }
?>


No. The request doesn't get to the PHP engine until after the file has been uploaded.


No, because the upload is part of the request and is done before PHP even comes into play.

There may be a way around this using the technique of those PHP-/Perl-based uploaders that somehow hook into the uploading process, but then, there also may not. Especially when you're using a different web server.

I would do a quick Ajax-based check for the right session, and bind that to the submit event of the form you are uploading the file with. It may take half a second, but is completely unobtrusive for the user.


You could make an ajax call on the page the form is submitted to check their session state before allowing the form to submit.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号