إعلانيمكن أن تكون علامتك التجارية هناعلامتك التجارية هنافي كل صفحة من Darslab · طلاب وأولياء أمور ومعلمونinfo@darslab.comاسأل عن الأسعارالأسعارinfo@darslab.com
☰ خطة العام · تكنولوجيا المعلومات والاتصالات (بالإنجليزية)

The Add Term code

الأسبوع 14 · الوحدة ٣ «إنشاء صفحات الموقع» (٧) الموضوع الثالث صفحة Add_Term.php: الكود المختصر وما ينقصه، الكود الأدق بالفحص والرسائل، حفظ السجل والتأكد من الجدول · 3 دروس
2

Checking the form and the picture

ICT Secondary 2 Term 1, Unit 3, Topic 3: pp. 100–113
GoalCheck the form values and the picture before saving.
New words
validation · checking data before using itempty field · a field in which the visitor typed nothing, or only spacesbasename · a PHP function that keeps only the file name and drops any foldersfile extension check · allowing only the endings we expect, such as webp, png or jpg
Explanation

Never trust what arrives from a form. First, the empty field. PHP has a function empty(), but it treats the text "0" as empty, so a term “0” would be refused. Safer is to trim the spaces and compare with an empty string: trim($term) !== ''. Second, a missing entry. If no file was chosen, $_FILES['picture']['name'] may not exist; the operator ?? gives a default: $_FILES['picture']['name'] ?? ''. The sign @, which some books use, only hides error messages; it is not a variable and it does not solve anything. Third, the picture name. A visitor’s file may be called ../../x.php; basename keeps only x.php. Then check the extension against a short list. Do not accept any file just because it is a file. A server also limits the size of an upload; if an example size is needed, 1 MB means 1,024 KB.

CheckCodeWhy
field not emptytrim($term) !== ''empty("0") would refuse a term 0
entry may be missing$_FILES['picture']['name'] ?? ''no warning if no file was chosen
safe file namebasename($name)drops folders such as ../../
expected endingin_array($ext, [...])only picture files
Worked examples
Php
1<?php
2var_dump(empty("0"), empty(""), empty("a"), isset($nothing));
3?>
Output
bool(true)
bool(true)
bool(false)
bool(false)
empty() is not always the right test. The text "0" counts as empty, an empty string too, and the letter a does not. isset of a variable that was never set is false.
Php
1<?php
2$term = trim("  router ");
3$trans = trim("   ");
4var_dump($term !== '', $trans !== '');
5?>
Output
bool(true)
bool(false)
A better empty test. After trim, a term with spaces around it is kept, and a value with only spaces counts as empty.
Php
1<?php
2echo basename("../../x.php"), " ", basename("pic/router.webp");
3?>
Output
x.php router.webp
basename makes the dangerous name harmless: the folders are dropped, so the file can only land in the pic folder.
Php
1<?php
2$name = "router.webp";
3$ext = strtolower(pathinfo($name, PATHINFO_EXTENSION));
4var_dump(in_array($ext, ["webp", "png", "jpg"]));
5?>
Output
bool(true)
An extension test with a short list of allowed endings. The name router.webp ends in webp, which is in the list, so the test is true.
Php
1<?php
2$_FILES = [];
3$name = $_FILES['picture']['name'] ?? '';
4var_dump($name);
5?>
Output
string(0) ""
The ?? operator gives a default when the entry is missing. With no file chosen, the name is an empty string and there is no warning.
Class activity

For each value below say what happens in the three checks (trim, basename, extension list): “ router ”, “ ”, “../pic/cat.webp”, “run.exe”. Then write the PHP condition that must be true before a term with a picture is saved.

Practice
1
Why is empty("0") a poor test for a term?
2
What does basename("../../x.php") return?
3
What does the sign @ do in front of a PHP line?
4
Why check the file extension against a short list?