문제가 있습니다.
pdf 파일을 업로드 할 수있는 양식이 있습니다. 이 필드는 필수 항목이 아니며 비어 있으면 오류를 출력하고 싶지 않습니다. 그러나 비어 있지 않으면 파일 확장자가 .pdf인지 확인하고 싶지 않습니다.
어떻게 할 수 있습니까?
내 PHP 코드는 다음과 같습니다.
$pdf_dir = "pdf_datoteke/";
$pdf_file = $pdf_dir . basename($_FILES['news_link']['name']);
$pdf_type = pathinfo($pdf_file,PATHINFO_EXTENSION);
$pdf_allowed = array('pdf');
if (empty($_POST['news_link']) === false && in_array($pdf_type, $pdf_allowed) === false) {
$errors[] = '<h2">* To ni pdf datoteka!</h2><br>';
}
enybody가 같은 질문을하면 해결책을 찾았습니다.
이 코드를 넣어야했습니다.
if (is_uploaded_file($_FILES['news_link']['tmp_name'])) {
$name = $_FILES['news_link']['name'];
$ext = end((explode('.', $name)));
if ($ext != 'pdf'){
$errors[] = '<h2>* This file format is not .pdf!</h2>';
}
}
그리고 하나 이상의 형식이 제외 된 그림의 경우 다음 코드를 넣습니다.
if (is_uploaded_file($_FILES['news_picture']['tmp_name'])) {
$image_name = $_FILES['news_picture']['name'];
$image_size = $_FILES['news_picture']['size'];
$allowed_ext = array('jpg', 'jpeg', 'png', 'gif', 'mp4');
$image_ext = pathinfo($image_name, PATHINFO_EXTENSION);
$image_ext = strtolower($image_ext);
if (in_array($image_ext, $allowed_ext) === false) {
$errors[] = '<h2>* This image formati is not accepted</h2>';
}
if ($image_size > 33554432) {
$errors[] = '<h2>* This image is to big!</h2>';
}
}
나는 이것이 누군가에게 도움이되기를 바랍니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다