تحميل الملفات باستخدام php إليك برنامج نصي PHP محسّن يتضمن أفضل الممارسات ويحل الخطأ الذي واجهته:
$ target_dir = "تحميل/" ؛ // تهيئة صفيف فارغ لأنواع الملفات المسموح بها $ leghtTypes = ['JPG' ، 'png'] ؛ // تحقق مما إذا كان قد تم تقديم النموذج if (isset ($ _ post ['submit'])) { // استرجع تفاصيل الملف $ target_file = $ target_dir. basename ($ _ files ['fileToupload'] ['name']) ؛ $ file_type = strtoLower (pathinfo ($ target_file ، pathinfo_extension)) ؛ // التحقق من صحة نوع الملف if (! in_array ($ file_type ، $ thertypes)) { صدى "نوع الملف غير صالح. فقط ملفات JPG و PNG مسموح بها." ؛ } // تحقق مما إذا كان الملف موجودًا بالفعل elseif (file_exists ($ target_file)) { صدى "ملف موجود بالفعل. الرجاء اختيار ملف مختلف." ؛ } // تحقق من حجم الملف (يفترض حد 5 ميجابايت) elseif ($ _files ['fileToupload'] ['size']> 5000000) { صدى "ملف كبير جدًا. الحد الأقصى لحجم الملف هو 5 ميجابايت." ؛ } آخر { // حاول نقل الملف إلى الدليل المستهدف if (move_uploaded_file ($ _ files ['fileToupload'] ['tmp_name'] ، $ target_file)) { صدى "ملف تم تحميله بنجاح!" ؛ } آخر { صدى "فشل تحميل الملف. يرجى المحاولة مرة أخرى." ؛ } } } ؟>
// Declare the target directory for uploaded files $target_dir = "uploads/"; // Initialize an empty array for allowed file types $allowedTypes = ['jpg', 'png']; // Check if the form has been submitted if (isset($_POST['submit'])) { // Retrieve the file details $target_file = $target_dir . basename($_FILES['fileToUpload']['name']); $file_type = strtolower(pathinfo($target_file, PATHINFO_EXTENSION)); // Validate the file type if (!in_array($file_type, $allowedTypes)) { echo "Invalid file type. Only JPG and PNG files are allowed."; } // Check if the file already exists elseif (file_exists($target_file)) { echo "File already exists. Please choose a different file."; } // Check file size (assumes a 5MB limit) elseif ($_FILES['fileToUpload']['size'] > 5000000) { echo "File is too large. Maximum file size is 5MB."; } else { // Attempt to move the file to the target directory if (move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $target_file)) { echo "File uploaded successfully!"; } else { echo "File upload failed. Please try again."; } } } ?>
تنصل: جميع الموارد المقدمة هي جزئيًا من الإنترنت. إذا كان هناك أي انتهاك لحقوق الطبع والنشر الخاصة بك أو الحقوق والمصالح الأخرى، فيرجى توضيح الأسباب التفصيلية وتقديم دليل على حقوق الطبع والنشر أو الحقوق والمصالح ثم إرسالها إلى البريد الإلكتروني: [email protected]. سوف نتعامل مع الأمر لك في أقرب وقت ممكن.
Copyright© 2022 湘ICP备2022001581号-3