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

PHP: Zend Form Element 파일 로 올 린 파일 을 어떻게 바 꿉 니까? 본문

개발 스크랩 메모/PHP

PHP: Zend Form Element 파일 로 올 린 파일 을 어떻게 바 꿉 니까?

렉사이 2020. 11. 27. 22:41

$form - > file - > getFileName () 을 호출 했 을 때 파일 이름 뿐만 아니 라 전체 경 로 를 되 돌려 줍 니 다.

어떻게 파일 이름 만 출력 합 니까?

//Answer: First, get an array of the parts of the filename: $pathparts = pathinfo($form->file->getFileName()); //then get the part that you want to use $originalFilename = $pathparts['basename']; 
  • 어떻게 파일 이름 을 내 가 원 하 는 파일 이름 으로 바 꿉 니까?이것 은 이름 바 꾸 기 필터 로 완성 할 수 있 습 니까?저 는 폼 에 목 표를 설정 해 놓 았 기 때문에 파일 이름 만 바 꾸 고 싶 습 니 다.

    내 가 표 에 목적 지 를 설정 하지 말 았 어야 했 나?필터 로 해도 안 돼.어쩌면 내 가 PHP 함수 로 이 걸 해 야 할 까?나 어 떡 하지?

    //Answer: Use the rename filter: $form->file->addFilter('Rename', 'new-file-name-goes-here.txt'); 
  • 최종 해결 방안:

    이것 이 내 가 마지막 으로 한 일이 다.

    public function foobarAction() { //...etc... if (!$form->isValid($request->getPost())) { $this->view->form = $form; return; } //the following will rename the file (I'm setting the upload dir in the form) $originalFilename = pathinfo($form->file->getFileName()); $newFilename = 'file-' . uniqid() . '.' . $originalFilename['extension']; $form->file->addFilter('Rename', $newFilename); try { $form->file->receive(); //upload complete! $file = new Default_Model_File(); $file->setDisplayFilename($originalFilename['basename']) ->setActualFilename($newFilename) ->setMimeType($form->file->getMimeType()) ->setDescription($form->description->getValue()); $file->save(); } catch (Exception $e) { //error: file couldn't be received, or saved (one of the two) } } 

    대답 하 다.

    질문 1 에 대답 하려 면 파일 이름 을 전체 경로 에서 가 져 와 야 합 니 다.

    basename 이나 pathinfo 을 사용 하 십시오.

    예 를 들 어 (문서 에서 복사, 붙 여 넣 기):

    $path = "/home/httpd/html/index.php"; $file = basename($path); // $file is set to "index.php" 

    나:

    $path_parts = pathinfo('/www/htdocs/index.html'); echo $path_parts['dirname'], "\n"; echo $path_parts['basename'], "\n"; echo $path_parts['extension'], "\n"; echo $path_parts['filename'], "\n"; // since PHP 5.2.0 

    파일 이름 을 바 꾸 고 이동 하려 면, rename 이 'ZeFrework solution' 이 아니 더 라 도 이 를 할 수 있 을 것 같 습 니 다.

    ZF 가 이 파일 을 이동 하지 않 고 임시 디 렉 터 리 에 있다 면, Move uploadd file 을 사용 해 야 합 니 다.

    그러나 setDestination 을 사용 하기 때문에 시스템 임시 디 렉 터 리 에 서 는 이 파일 을 사용 하지 않 는 다 고 생각 합 니 다.

    Comments