A WordPress post is supposed to process sample data (uploaded as PDF files) and displays the data (after some manipulations and reformatting) on the same post just below.
...
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="filepdf" />
<input type="submit" name="submit" value="Upload data samples (.pdf file)" />
</form>
...
I use the PHP code snippets (Insert PHP)
plugin to include PHP code.
...
[insert_php]
if(isset($_FILES['filepdf'])) {
$mTime = number_format(microtime(true), 3, '.', '');
$file_name = $_FILES['filepdf']['name'];
$file_size = $_FILES['filepdf']['size'];
$file_tmp = $_FILES['filepdf']['tmp_name'];
$file_ext = strtolower(end(explode('.', $_FILES['filepdf']['name'])));
...
$cmd = "./mybinary '$file_name'";
$outfile = tempnam("/tmp", "cmd");
$errfile = tempnam("/tmp", "cmd");
$descriptorspec = array(
0 => array("pipe", "r"),
1 => array("file", $outfile, "w"),
2 => array("file", $errfile, "w")
);
move_uploaded_file($file_tmp, $file_name);
$proc = proc_open($cmd, $descriptorspec, $pipes);
...
[/insert_php]
Hours of troubleshooting, but I don't find why this happens ... when I upload a PDF file, the binary mybinary
in proc_open() fires twice - sometimes (spaced by +/- 200 ms). Let's say 95% of all requests work fine, but 5% fail due to these double triggers. It seems (?!) that mobile Android devices using Chrome are more affected than other operating systems and/or browsers (not sure at all about that though). This is at least what Apache's access.log says. The present situation is unacceptable since the binary uses millisecond timecode generated by the PHP code (see above), and this messes up the entire process.
Questions:
1. Is my assumption correct that Chrome sends two requests?
2. If yes, why?
3. How can I avoid that?
I googled about that, and indeed, it seems that proc_open() sometimes fires twice, but I didn't find any solution.
Also, I don't know if this is WordPress related, or a PHP subject.
A WordPress post is supposed to process sample data (uploaded as PDF files) and displays the data (after some manipulations and reformatting) on the same post just below.
...
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="filepdf" />
<input type="submit" name="submit" value="Upload data samples (.pdf file)" />
</form>
...
I use the PHP code snippets (Insert PHP)
plugin to include PHP code.
...
[insert_php]
if(isset($_FILES['filepdf'])) {
$mTime = number_format(microtime(true), 3, '.', '');
$file_name = $_FILES['filepdf']['name'];
$file_size = $_FILES['filepdf']['size'];
$file_tmp = $_FILES['filepdf']['tmp_name'];
$file_ext = strtolower(end(explode('.', $_FILES['filepdf']['name'])));
...
$cmd = "./mybinary '$file_name'";
$outfile = tempnam("/tmp", "cmd");
$errfile = tempnam("/tmp", "cmd");
$descriptorspec = array(
0 => array("pipe", "r"),
1 => array("file", $outfile, "w"),
2 => array("file", $errfile, "w")
);
move_uploaded_file($file_tmp, $file_name);
$proc = proc_open($cmd, $descriptorspec, $pipes);
...
[/insert_php]
Hours of troubleshooting, but I don't find why this happens ... when I upload a PDF file, the binary mybinary
in proc_open() fires twice - sometimes (spaced by +/- 200 ms). Let's say 95% of all requests work fine, but 5% fail due to these double triggers. It seems (?!) that mobile Android devices using Chrome are more affected than other operating systems and/or browsers (not sure at all about that though). This is at least what Apache's access.log says. The present situation is unacceptable since the binary uses millisecond timecode generated by the PHP code (see above), and this messes up the entire process.
Questions:
1. Is my assumption correct that Chrome sends two requests?
2. If yes, why?
3. How can I avoid that?
I googled about that, and indeed, it seems that proc_open() sometimes fires twice, but I didn't find any solution.
Also, I don't know if this is WordPress related, or a PHP subject.
Share Improve this question edited Nov 7, 2018 at 6:59 geohei asked Oct 23, 2018 at 15:09 geoheigeohei 1112 bronze badges 5 |1 Answer
Reset to default 1To complete this ... I initially used PHP code snippets (Insert PHP) plugin (version 2.0.6 - latest). The php page was in fact only called once, but executed twice. I didn't have any explanation for all this described above (same temp filename, but code called twice, ...).
After I changed the PHP code WordPress integration method into the Shortcode concept, all worked fine (till now). So I believe that this mentioned plugin was the reason for the double triggers?!
And no double clicks - I checked that already right from the beginning. So probably the PHP code itself was not the culprit.
[insert_php]
shortcode you're using is incredibly dangerous, it's one of the most dangerous things you can do to a site. Instead just implement a shortcode and put the PHP code inside an actual PHP file. Again, I cannot understate just how dangerous what you're doing is, and how stupendously dangerous plugins that let you do this are. – Tom J Nowell ♦ Commented Oct 23, 2018 at 15:32[insert_php]
then. I was completely unaware of that risk (WP newbie in PHP coding). Never wrote my own shortcode. Do I need a plugin to write/use my own shortcode? Is this a good site to get started? link. If not, please give me a starting point. Thanks! – geohei Commented Oct 23, 2018 at 19:52insert_php
shortcode and execute anything on the server – Tom J Nowell ♦ Commented Nov 6, 2018 at 20:35