2017-10-01 8 views
0

이 기능이 왜 업로드 된 파일을 어디로 옮기지 않는지 말해 줄 수 있습니까?wordpress wp_handle_upload 파일을 이동하지 않습니다

function handle_logo_upload($option){ 
    if(!function_exists('wp_handle_upload')) 
{ 

      require_once(ABSPATH .'wp-admin/includes/file.php'); 
            } 


      if(!empty($_FILES["site_logo_custom"])){ 

$theFile=$_FILES["site_logo_custom"]; 

$overrides=array('test_form'=>false); 

$urls=wp_handle_upload($theFile,$overrides); 
$temp=$urls["url"]; 
return $temp; 

            } 


            return $option; 
           } 

정말 wp_handle_upload 기능에 대해별로 알 수 없습니다. Thankss !!!

+0

그리고이 기능이 유지되는 위치는 어디에서 볼 수 있습니까? 이 함수를 폼 액션으로 어떻게 실행하고 있습니까? –

+0

안녕하세요 Sajjadur, 내 테마의 함수 템플릿입니다. 설정 페이지의 필드입니다. 잠시 씩 인터넷 검색을 한 후 일부 자습서에서이 방법을 발견했습니다. 나는 jQuery를 기반으로하는 다른 접근법을 발견했지만, 나는 PHP에서 데이터의 콜렉션을 두는 것을 선호한다. – domjanzsoo

답변

0

함수가 완벽하게 호출됨을 추측합니다.

function handle_logo_upload($option){ 
    if(!function_exists('wp_handle_upload')) 
    { 

     require_once(ABSPATH .'wp-admin/includes/file.php'); 
    } 
    //you are using empty version make sure your php version is higher than 5.2 
    if(!empty($_FILES["site_logo_custom"])){ 
     $move_logo = wp_handle_upload($_FILES["site_logo_custom"], array('test_form' => false)); 
     if ($move_logo && !isset($move_logo['error'])) { 
      $wp_upload_dir = wp_upload_dir(); 
      $attachment = array(
       'guid' => $wp_upload_dir['url'] . '/' . basename($move_logo['file']), 
       'post_mime_type' => $move_logo['type'], 
       'post_title' => preg_replace('/\.[^.]+$/', '', basename($move_logo['file'])), 
       'post_content' => '', 
       'post_status' => 'inherit' 
      ); 
      $logo_attach_id = wp_insert_attachment($attachment, $move_logo['file']); 
      $image_attributes = wp_get_attachment_image_src($logo_attach_id); 
      if ($image_attributes) { 
       return $image_attributes[0]; 
      } 
      else 
      { 
       return $option; 
      } 
     }else{ 
      return $option; 
     } 
    }else{ 
     return $option; 
    } 

}   

+0

안녕하세요 Akshay, 조금만 제대로 들여다 볼게요. 고마워 .. – domjanzsoo

+0

다시 함수를 사용하여 Arkshay 일부 변경이 발생했지만 여전히 유용하지 않습니다. 내 PHP 버전은 7.1.1입니다. 이 함수는 다음 문자열을 반환합니다 : 88974.ngsversion.1463419670135.adapt.676.1.jpg. 그것은 옵션으로 저장하는 것 같습니다. get_option ('site_logo_custom')을 사용하여 호출 할 수 있습니다. 하지만 옵션 데이터베이스 테이블에서 찾을 수 없습니다. 파일은 여전히 ​​어디에도 이동되지 않습니다. – domjanzsoo

+0

$ image_attributes, print r 해당 배열에서 전체 이미지 경로를 가져 오는 중입니까? –

1

내가 양식은 다음과 같이 좀 포맷 가정 코드로 작성 내 의견을 읽어 보시기 바랍니다 :

form action="" enctype="multipart/form-data" method="post"> //action is current post 
<input type="file" name="file"> 
<input type="submit" name="submit"> 
</form> 

그리고) (wp_handle_upload를 사용하여 워드 프레스 업로드 폴더에 파일을 업로드 할; 함수를 사용하면 코드 아래에서 사용할 수 있습니다.

function handle_logo_upload($file){ 

require_once(ABSPATH.'wp-admin/includes/file.php'); 
$uploadedfile = $file; 

$movefile = wp_handle_upload($uploadedfile, array('test_form' => false)); 

if ($movefile){ 
    echo $movefile['url']; 
    //or return 
    return $movefile['url']; 
    } 

} 
if (isset($_POST['submit'])) { 
    handle_logo_upload($_FILES['file']); 
} 
+0

을 확인하십시오. 보안 검사를 추가로 수행하려면 function.i에서 보안 검사를 추가하지 않아도됩니다. –

+0

안녕하세요 Sajjadur, 내 코드는 동일합니다. 설정 API에서 만든 양식에서 사용하고 있습니다. 내 의도는 테마 설정 페이지에 로고 업로드 버튼을 설정하는 것입니다. – domjanzsoo

+0

다음 wordpress 미디어 업 로더를 사용하여 이미지 URL을 가져 와서 {prefix} _settings 데이터베이스에 저장하십시오 .... –