개발! 딱 깔끔하고 센스있게!

slim php 프레임 그림 업로드 put 데이터베이스 본문

개발 스크랩 메모/PHP

slim php 프레임 그림 업로드 put 데이터베이스

렉사이 2020. 12. 24. 02:37

저는 slim php 프레임의 초보자입니다.

사진을 올리고 싶습니다.

POST 를 통해 데이터베이스에 파일을 올려 주시면 실례 코드도 있습니다.

대답

이것은 경로기:

$app->post('/', 'uploadFile'); 

다음 함수를 가리키기:

function uploadFile () { if (!isset($_FILES['uploads'])) { echo "No files uploaded!!"; return; } $imgs = array(); $files = $_FILES['uploads']; $cnt = count($files['name']); for($i = 0 ; $i < $cnt ; $i++) { if ($files['error'][$i] === 0) { $name = uniqid('img-'.date('Ymd').'-'); if (move_uploaded_file($files['tmp_name'][$i], 'uploads/' . $name) === true) { $imgs[] = array('url' => '/uploads/' . $name, 'name' => $files['name'][$i]); } } } $imageCount = count($imgs); if ($imageCount == 0) { echo 'No files uploaded!! 

Try again'; return; } $plural = ($imageCount == 1) ? '' : 's'; foreach($imgs as $img) { printf('%s
', $img['name'], $img['url']); } }

더 좋은 답안이 있다면, 수정을 환영합니다.

Comments