Postingan lainnya
Tipe file yang diizinkan untuk diupload
Sore gan. Saya mau nanya gimana caranya membatasi tipe file/ekstensi yang boleh diupload. Misalkan hanya boleh upload file bertipe .jpg .png, dsb.
ini function simpan/upload data saya:
public function store(Request $request)
{
$file = $request->file('owner_photo');
$fileName = $file->getClientOriginalName();
$file->move("uploads/file", $fileName);
$file2 = $request->file('dealer_photo');
$fileName2 = $file2->getClientOriginalName();
$file2->move("uploads/file", $fileName2);
$credentials = [
'dealer_name' => $request->dealer_name,
'username' => $request->username,
'password' => $request->password,
'office_address' => $request->office_address,
'phone_number' => $request->phone_number,
'owner_photo' => $fileName,
'dealer_photo' => $fileName2,
];
$user = Sentinel::registerAndActivate($credentials);
$role = Sentinel::findRoleBySlug('finance');
$role->users()->attach($user);
return back()->with('create', 'A new dealer added!');
}
Berdasarkan code diatas, ekstensi file apapun yang saya upload tetap bisa disimpan. Sedangkan kebutuhan program hanya boleh mengupload file gambar.
Mohon bantuannya. Terima kasih.
3 Jawaban:
coba pake ini gan
<pre> $extensi = pathinfo($dir, PATHINFO_EXTENSION); </pre>
<a href='php.net '>php.net </a>
Agan gk divalidate ya? Di validate dulu gan sebelum distore. Ntar tinggal uji aja mimes typenya jpg, png atau gif(sesuai yg agan mau) <pre> //contoh $fileImg = $request->validate( ['images' => 'mimes:jpg|png|gif']);</pre>
<pre> $allowed_ext = array('doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'pdf', 'rar', 'zip'); $file_name = $_FILES['file']['name']; $file_name_exp = explode(".", $file_name); $file_ext = strtolower(array_pop($file_name_exp)); // $file_ext = strtolower(end(explode('.', $file_name))); $file_size = $_FILES['file']['size']; $file_tmp = $_FILES['file']['tmp_name']; </pre> ganti aja sama png atau jpeg arraynya semoga membantu