0
我試圖使用JavaScript和JSON中的變量進行表單工作。
一般的想法是將文件上傳到一個文件夾,並在cookie中定義路徑。我正在使用PHP。不知何故,我不能讓if語句返回TRUE。
上傳圖像並根據JSON變量在PHP中放置cookie
我的代碼如下所示:
從functions.php的
function SaveSelfieCam() {
$currentQuiz = GetCurrentQuiz();
if (!empty($_FILES[$currentQuiz->id])) {
$myFile = $_FILES[$currentQuiz->id];
if ($myFile["error"] !== UPLOAD_ERR_OK) {
echo "<p>Der skete en fejl.</p>";
exit;
}
$name = preg_replace("/[^A-Z0-9._-]/i", "_", $myFile["name"]);
$i = 0;
$parts = pathinfo($name);
while (file_exists(QR_DIR . $name)) {
$i++;
$name = $parts["filename"] . "-" . $i . "." . $parts["extension"];
}
$success =
move_uploaded_file($myFile["tmp_name"],QR_DIR . $name);
if (!$success) {
echo "<p>Det lykkedes ikke at gemme filen.</p>";
exit;
}
if ($success) {
echo "Alt er godt!";
setcookie($currentQuiz->id, QR_DIR . $name);
chmod(QR_DIR . $name, 0644);
}
}
}
HTML表(從我的index.php)
echo "<form id='form' action='' method='post'><input id='selfieCam' class='quizCam' type='file' capture='user' accept='image/*' name='" . $currentQuiz->id . "' onchange='submitSelfie()'><label for='selfieCam'>Til kameraet</label></form></div>";
SaveSelfieCam();
的JavaScript(提交表單):
function submitSelfie() {
document.getElementById("form").submit();
}
這奏效了!非常感謝你! –