2017-05-23 7 views
0


私はJavaScriptとJSONの変数からフォームを作成しようとしています。
一般的な考え方は、ファイルをフォルダにアップロードし、そのパスをクッキーに定義することです。私はPHPで働いています。どういうわけか私はちょうどifステートメントをTRUEに戻すことはできません。
PHPのJSON変数に基づいて画像をアップロードしてクッキーを配置

私のコードは次のようになります。
の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(); 
} 

答えて

0

はトリックをしたこの

<form id='form' action='' method='post' enctype='multipart/form-data'> 
+0

のように属性値としてフォームにのenctype =「マルチパート/フォームデータ」を追加しよう!どうもありがとうございました! –

関連する問題