Ich habe HTML-Formular mit Dateieingabe und senden Sie es mit AJAX, dann in PHP Ich mache Curl Anfrage und lade die Datei auf einen anderen Server, und wenn die Datei hochlädt, sende ich eine andere Ajax Anfrage und erhalten Sie die Locke Fortschritt, und zeigen Sie es auf HTML, es funktioniert derzeit und kein Problem damit. aber wenn ich die Seite aktualisieren, wenn die Datei noch hochladen, die Curl-Anfrage nicht geschlossen und immer noch im Hintergrund läuft, möchte ich beim Aktualisieren der Seite, die Curl-Anfrage zu schließen.php curl request nicht schließen, wenn die Seite aktualisieren
das ist mein HTML-Code in index.php
<!DOCTYPE html>
<html>
<head>
<title>sad</title>
</head>
<body>
<form action="up.php" method="post">
<input type="file" name='file'></input>
<button type="submit">Upload</button>
</form>
<progress id="progress" value="0" max="100"></progress>
<div id="myResultsDiv"></div>
<script type="text/javascript" src="jquery-1.12.3.min.js"></script>
<script type="text/javascript" src="jquery.form.min.js"></script>
<script type="text/javascript">
var progressSetInterval = null;// Global
$(document).ready(function() {
var time = Date.now();
// bind submit handler to form
$('form').on('submit', function(e) {
e.preventDefault(); // prevent native submit
$(this).ajaxSubmit({
url: 'up.php?t=' + time,
target: '#myResultsDiv'
});
progressSetInterval = setInterval(function(){
$.ajax({
type: "POST",
url: "progressFile_" + time + '.txt?t=' + Date.now(),
success: function(data){
$('#progress').attr('value' , data);
if(data >= 100) {
clearInterval(progressSetInterval);
}
}
});
}, 3000);
});
});
</script>
</body>
</html>
und es auf meinem PHP-Code up.php
<?php
Class Upload {
public $progressFile = '';
public function up($file) {
flush();
$ch = curl_init();
$localfile = $file['tmp_name'];
$path = 'ftp://dl.mywebsite.com/public_html/' . time() . '_' . $file['name'];
$fp = fopen($localfile, 'r');
curl_setopt($ch, CURLOPT_URL, $path);
curl_setopt($ch, CURLOPT_USERPWD, 'username:password');
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localfile));
curl_setopt($ch, CURLOPT_NOPROGRESS, false);
curl_setopt($ch, CURLOPT_PROGRESSFUNCTION,function($resource,$dltotal, $dlnow, $ultotal, $ulnow){
$progress = @round($ulnow/$ultotal * 100);
file_put_contents($this->progressFile, $progress);
flush();
}
);
curl_exec($ch);
$error_no = curl_errno($ch);
curl_close($ch);
if ($error_no == 0) {
echo $path;
} else {
echo false;
}
}
}
$file = $_FILES['file'];
$class = new Upload();
$class->progressFile = 'progressFile_' . $_GET['t'] . '.txt';
$class->up($file);
Was Sie versuchen zu erreichen, ist nicht möglich, da beim Aktualisieren der Webseite der Dateiupload unterbrochen wird. – khandelwaldeval