عرض ادب
با تشکر از استاد عزیز جناب صالحی و تمام منتورهای دوره بابت زحماتتون.
یک سوال داشتم. اسکریپت اپلود فایل کاملا کار میکنه اما زمانی که اینپوت فایل رو خالی میزارم واپلود رو میزنم به جای اینکه با پیغام "فایل انتخاب کنید" مواجه بشم با یک پیغام دیگه مربوط به یک شرط دیگه مواجه میشم.
یعنی چیزی که متوجه شدم شرط اول, $_FILES رو خالی نمیدونه.
برای اطمینان از FILES پرینت گرفتم که خروجیش رو براتون میزارم.
session_start();
$allowedTypes = array('zip', 'jpg', 'jpeg', 'rar', 'png');
$msg = null;
// 1. change method to receive file
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (isset($_POST["uploadBtn"]) && $_POST["uploadBtn"] == "Upload") {
if (isset($_FILES["fileUploaded"]) && !empty($_FILES["fileUploaded"])){
print_r($_FILES['fileUploaded']);
//2. get the uploaded file data from $_FILES
$fileInitName = $_FILES['fileUploaded']['name'];
$fileSize = $_FILES['fileUploaded']['size'];
$fileTmpPath = $_FILES['fileUploaded']['tmp_name'];
$fileType = $_FILES['fileUploaded']['type'];
//3. make sure the extension of the file is lower case
$fileArrName = explode('.', $fileInitName);
$fileExtension = strtolower(end($fileArrName));
//4. get a unique name for each file that has been uploaded
$fileName = md5(time() . $fileArrName[0]) . '.' . $fileExtension;
echo in_array($fileExtension, $allowedTypes);
//5. make sure the extension of the file is allowed
if (in_array($fileExtension, $allowedTypes)) {
//6. make dir to save uploaded files
//7. set notifs
move_uploaded_file($fileTmpPath, "upload/$fileName");
echo $msg = "File recevied.";
} else {
echo $msg = "You are not allowed to upload these files!!";
}
} else {
echo $msg = "Please select a file to upload.";
}
}
}
$_SESSION['msg'] = $msg;
Array ( [name] => [full_path] => [type] => [tmp_name] => [error] => 4 [size] => 0 ) You are not allowed to upload these files!!