2011-11-21 2 views
11

사용자가 업로드 한 이미지를 'temp'라는 폴더에 업로드하고 업로드 위치를 $ _SESSION [ 'uploaded_photos'] 배열에 저장할 수있는 업로드 양식이 있습니다. 일단 사용자가 '다음 페이지'버튼을 누르면, 그 폴더 바로 앞에서 동적으로 생성 된 새 폴더로 파일을 옮기고 싶습니다.어떻게 PHP를 사용하여 파일을 다른 폴더로 옮길 수 있습니까?

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<br />"; 
    } else{ 
     echo "There was an error uploading the file, please try again!"; 
    } 

    } //end foreach 

} //end if isset next_page 

사용되고있는 $ 값의 예는 다음

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

그리고 $의 target_path의 일례 사용되는 즉 :

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

temp 폴더에있는 파일을 볼 수 있습니다.이 두 경로가 모두 나에게 잘 보이고 mkdir 기능이 실제로 잘 된 폴더를 만들 었는지 확인했습니다.

어떻게하면 php를 사용하여 파일을 다른 폴더로 옮길 수 있습니까?

답변

20

시나리오를 읽었을 때 업로드를 처리하고 파일을 '임시'폴더로 이동 한 것처럼 보입니다. 이제 파일이 새로운 작업을 수행 할 때 파일을 이동하려고합니다 (다음 버튼 클릭).).

PHP와 관련하여 'temp'파일은 더 이상 업로드되지 않으므로 더 이상 move_uploaded_file을 사용할 수 없습니다.

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<br />"; 
    } else{ 
     echo "There was an error uploading the file, please try again!"; 
    } 

    } //end foreach 

} //end if isset next_page 
:

당신이해야 할 모든 사용 rename입니다

관련 문제