ฉันกำลังอ่านรูปภาพจากไดเร็กทอรี 'images' ซึ่งมีหลายโฟลเดอร์ที่มีรูปภาพ ฉันต้องการแสดงทุกภาพในรูปแบบ div และทำเครื่องหมายในช่องเพื่อตัดสินใจว่าเป็นแฟชั่นหรือไม่ ฉันใช้รหัสต่อไปนี้เพื่ออ่านไฟล์จากไดเร็กทอรีและแสดงในเบราว์เซอร์ div เป็นแบบหนึ่งต่อจากนี้:
<?php
function listFolderFiles($dir){
$html = '';
// Init the session if it did not start yet
if(session_id() == '') session_start();
$html .= '<ol>';
foreach(new DirectoryIterator($dir) as $folder){
if(!$folder->isDot()){
if($folder->isDir()){
// $html .= '<li>';
$user = $dir . '/' . $folder;
foreach (new DirectoryIterator($user) as $file) {
if($file != '.' && $file != '..'){
$image = $user . '/' . $file;
// Check the pictures URLs in the session
if(isset($_SESSION['pictures']) && is_array($_SESSION['pictures'])){
// Check if this image was already served
if(in_array($image, $_SESSION['pictures'])){
// Skip this image
continue;
}
} else {
// Create the session variable
$_SESSION['pictures'] = array();
}
// Add this image URL to the session
$_SESSION['pictures'][] = $image;
// Build the form
//echo $image, "</br>";
$html .= '
<style>
form{
font-size: 150%;
font-family: "Courier New", Monospace;
display: inline-block;
align: middle;
text-align: center;
font-weight: bold;
}
</style>
<form class = "form" action="' . action($image) . '" method="post">
<div>
<img src="' . $image . '" alt="" />
</div>
<label for="C1">FASHION</label>
<input id="fashion" type="radio" name="fashion" id="C1" value="fashion" />
<label for="C2"> NON FASHION </label>
<input id="nfashion" type="radio" name="nfashion" id="C2" value="nfashion" />
<input type="submit" value="submit" />
</form>';
// Show one image at a time
// Break the loop
break 2;
}
}
// $html .= '</li>';
}
}
}
$html .= '</ol>';
return $html;
}
//##### Action function
function action($img){
$myfile = fopen("annotation.txt", "a");
if (isset($_POST['fashion'])) {
fwrite($myfile, $img." -- fashion\n");
// echo '<br />' . 'fashion image';
}
else{
fwrite($myfile, $img." -- nonFashion\n");
// echo '<br />' . ' The submit button was pressed<br />';
}
}
echo listFolderFiles('images//');
?>
เมื่อฉันทำการเรียกการกระทำ($image) จากแท็กแบบฟอร์มภายใน ฉันสังเกตเห็นว่าค่าเริ่มต้นของการส่งนั้นจะถูกแยกวิเคราะห์ไปยังฟังก์ชัน ดังนั้นจึงเก็บชื่อของรูปภาพด้วยค่าเริ่มต้นและค่าที่เลือกไว้เป็นค่าแรก รูปภาพจะถูกแยกวิเคราะห์เป็นรูปภาพที่สอง ค่าที่เลือกของรูปภาพที่สองจะถูกจัดเก็บไว้ในไฟล์ที่มีรูปภาพที่สามเป็นต้น เมื่อฉันโทรเข้าแท็กส่งอินพุตด้านข้าง มันจะเริ่มจัดเก็บลงในไฟล์จากรูปภาพที่สองโดยมีค่าของรูปภาพแรกเป็นต้น ฉันไม่สามารถจัดเก็บไว้ในไฟล์ความสอดคล้องที่ถูกต้องระหว่างรูปภาพกับค่าที่เลือกได้ ฉันควรเรียกใช้ฟังก์ชันการกระทำที่ไหนเพื่อให้ดำเนินการได้อย่างถูกต้อง?
แก้ไข: เมื่อฉันโทรออกภายในแบบฟอร์ม ฟังก์ชั่นการกระทำจะถูกเรียกทันทีก่อนที่ผู้ใช้จะกดปุ่มส่ง ฉันจะเรียกใช้ฟังก์ชันของฉันได้อย่างไรเมื่อส่งแบบฟอร์ม ฉันพยายามใช้ if(strtoupper($_SERVER['REQUEST_METHOD']) === 'POST') {}
:
if(strtoupper($_SERVER['REQUEST_METHOD']) === 'POST') {
action($image);
}
ฉันเพิ่มโค้ดด้านบนหน้าแท็ก html อย่างไรก็ตาม ฉันได้ผลลัพธ์เหมือนกับในกรณีที่ฉันใส่ฟังก์ชัน action ไว้ในแท็กอินพุต send มีความคิดวิธีการแยกวิเคราะห์ภาพที่ถูกต้องเพื่อเขียนค่าหรือไม่?