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

어떻게 php 을 사용하여 파일을 다른 폴더로 이동합니까? 본문

개발 스크랩 메모/PHP

어떻게 php 을 사용하여 파일을 다른 폴더로 이동합니까?

렉사이 2020. 12. 25. 22:40

나는 현재 'temp' 이라는 제목으로 'temp' 이라는 제목의 그림을 올릴 수 있으며, 그들의 위치는 'SESION' uploaded u photos' 에 저장되어 있습니다.

사용자가 "다음 페이지" 단추를 누르면, 파일을 새 폴더로 이동하기를 희망합니다.

if(isset($_POST['next_page'])) { if (!is_dir('../images/uploads/listers/'.$_SESSION['loggedin_lister_id'])) { mkdir('../images/uploads/listers/'.$_SESSION['loggedin_lister_id']); } foreach($_SESSION['uploaded_photos'] as $key => $value) { $target_path = '../images/uploads/listers/'.$_SESSION['loggedin_lister_id'].'/'; $target_path = $target_path . basename($value); if(move_uploaded_file($value, $target_path)) { echo "The file ". basename($value). " has been uploaded
"; } else{ echo "There was an error uploading the file, please try again!"; } } //end foreach } //end if isset next_page

사용하고 있는 value 의 예례는:

../images/uploads/temp/IMG_0002.jpg

사용 중인 $target 경로 예제:

../images/uploads/listers/186/IMG_0002.jpg

temp 폴더에 있는 파일을 볼 수 있습니다.

이 두 경로가 나한텐 모두 좋습니다.

mkdir 함수를 확보하기 위해 아주 잘 만든 폴더를 확인하였습니다.

어떻게 php 을 사용하여 파일을 다른 폴더로 이동합니까?

대답

내가 너의 장면을 읽을 때, 너는 이미 업로드를 처리하고 파일을 'temp' 폴더로 이동하는데 현재 그들이 새 조작할 때 이동 파일 (다음 '단추를 누르십시오.

PHP 에서는 'temp' 파일이 업로드되지 않기 때문에 moveu uploaded 파일을 사용할 수 없습니다.

이름만 바꾸기:

if(isset($_POST['next_page'])) { if (!is_dir('../images/uploads/listers/'.$_SESSION['loggedin_lister_id'])) { mkdir('../images/uploads/listers/'.$_SESSION['loggedin_lister_id']); } foreach($_SESSION['uploaded_photos'] as $key => $value) { $target_path = '../images/uploads/listers/'.$_SESSION['loggedin_lister_id'].'/'; $target_path = $target_path . basename($value); if(rename($value, $target_path)) { echo "The file ". basename($value). " has been uploaded
"; } else{ echo "There was an error uploading the file, please try again!"; } } //end foreach } //end if isset next_page
Comments