[WEB] PHP 파일 업로드/다운로드/삭제/찾기

임시 파일들 서버 폴더에 업로드하고 다운로드하고 삭제 하고 찾기.

임시 파일이라 DB/스토리지 사용하지 않고  서버 폴더로 사용.


파일 찾기

$dir_path = 폴더명/파일명;

$dir_path = "/var/www/html/xxxx/xxxxx/".$hName.".xlsx";

$fileExist = file_exists($dir_path);


파일 업로드

<input type="file" name="upload[]" multiple id="XXX" >(앞단)

$uploads_dir = '/var/www/html/xxxx/xxxxx';

$name = $_FILES['upload']['name'];

move_uploaded_file( $_FILES['upload']['tmp_name'], "$uploads_dir/$name");


파일 다운로드 (get 방식 A tag로 했음.)

$dir_path = 폴더명/파일명;

$dir_path = "/var/www/html/xxxx/xxxxx/$fname";

$filesize = filesize($dir_path);

header("Pragma: public");

header("Expires: 0");

header("Content-Type: application/octet-stream");

header("Content-Disposition: attachment; filename=$fname");

header("Content-Transfer-Encoding: binary");

header("Content-Length: $filesize");

ob_clean();

flush();

echo readfile($dir_path);


파일삭제

단일

$dir_path = 폴더명/파일명;

$dir_path = "/var/www/html/xxxx/xxxxx/$fname";

$ret = unlink($dir_path);

전체

$dir_path = 폴더명;

$dir_path = "/var/www/html/xxxx/xxxxx/";

$handle = opendir($dir_path); 


while ($file = readdir($handle)) {

  @unlink($dir_path.$file);

}

closedir($handle);

댓글

이 블로그의 인기 게시물

[DB] Mysql 숫자 기준으로 정렬하기.

[WEB] ASP.NET System.NullReferenceException: 개체 참조가 개체의 인스턴스로 설정되지 않았습니다.

[문서] excel 체크박스 삭제