2014-10-06 2 views
-2

파일을 업로드하지 않고 PHP로 HTML 양식에서 XML 파일의 내용을 읽을 수있는 방법은 무엇입니까? 이것은 내 양식입니다 :PHP HTML 입력 파일에서 파일 읽기

<form action="readxml.php" method="post" enctype="multipart/form-data"> 
    <label for="file">Filename:</label> 
    <input type="file" name="file" id="file"><br> 
    <input type="submit" name="submit" value="Submit"> 
</form> 

나는 사용자가 따라서 실제로 파일을 업로드 할 필요가 없습니다, 데이터베이스에 그 값을 저장하는 SimpleXML을을 사용하여 선택하는 XML을 처리 할.

감사합니다.

+1

"파일을 업로드하지 않고"는 무엇을 의미합니까? xml을 문자열로 전달하고 파싱 (값을 얻기)하려는 것입니까? –

+1

서버에서 파싱하려면 * 업로드해야합니다! 이것은 말이되지 않습니다. –

+0

PHP가 서버에서 실행되기 때문에 먼저 파일을 업로드해야합니다. – Cyclonecode

답변

0

수 없습니다. 클라이언트 측에서도 자바 스크립트로 파일을 파싱하는 것만으로는 불가능하다고 생각합니다. 그러나 나는 틀릴지도 모른다. 서버에 업로드하여 임시 폴더에 저장 한 다음 삭제할 수 있습니다.

+0

그 방법을 말해 줄 수 있습니까? –

+0

회신에 링크가 추가되었습니다. –

3

실제로 서버에 파일이 없으면 PHP에서이를 수행 할 수 없습니다. 당신은 파일을 업로드해야하거나 다음 중 하나에 의해 업로드하지 않고 클라이언트 측에서이를 처리 할 수 ​​있습니다

  • 사용 HTML5 FileReader API 만 크롬, FF 및 IE 10 +
  • ActiveX를 사용하는 일이를 처리하는 파일을 클라이언트 측에서 IE를 제외하고는 작동하지 않습니다.
  • SilverLight를 사용하여 클라이언트 측에서 파일을 처리합니다.
  • 클라이언트 측에서 파일을 처리하기위한 ActionScript입니다.

그러나 임시 위치에서 파일을 업로드하고 처리 한 다음 삭제하는 것이 좋습니다. 더 간단하고 서버에서와 같이 어디에서나 작동합니다.

는이 파일을 업로드하는 방법입니다 : 이 파일을 삭제하는 방법입니다 PHP File Upload

: PHP unlink() Function

이 파일을 읽는 방법입니다 : PHP File Open/Read/Close

+0

나는 "그러나"부분에 강력하게 동의합니다 ... –

+0

나는 이해합니다. 파일을 업로드하고 읽으며 삭제하는 방법에 대한 조언을 해 줄 수 있습니까? –

+0

업로드, 읽기 및 삭제를 위해 내 답장에 링크가 추가되었습니다. –

-1

이에 내 html/php file ... 나에게 쉽다는 이유로 자체적으로 제출하도록 설정했다.

<?php 

if (isset($_FILES['file'])) { 
    $file = $_FILES['file']['tmp_name']; 

    $catalog = simplexml_load_file($file); 

    echo '<table style="border-spacing: 10px;">'; 
    echo '<tr><th>Title</th><th>Author</th></tr>'; 

    foreach ($catalog->book as $b) { 
     echo '<tr><td>'.$b->title.'</td><td>'.$b->author.'</td></tr>'; 
    } 
    echo '</table>'; 
} 
else { 
?> 
<!-- change the filename below --> 
<form action="filename.html" method="post" enctype="multipart/form-data"> 
    <label for="file">Filename:</label> 
    <input type="file" name="file" id="file"><br> 
    <input type="submit" name="submit" value="Submit"> 
</form> 
<?php } ?> 

여기에 ... 나는이 양식 업로드 할 파일에 사용되는 XML입니다

<?xml version="1.0"?> 
<catalog> 
    <book id="bk101"> 
     <author>Gambardella, Matthew</author> 
     <title>XML Developer's Guide</title> 
     <genre>Computer</genre> 
     <price>44.95</price> 
     <publish_date>2000-10-01</publish_date> 
     <description>An in-depth look at creating applications 
     with XML.</description> 
    </book> 
    <book id="bk102"> 
     <author>Ralls, Kim</author> 
     <title>Midnight Rain</title> 
     <genre>Fantasy</genre> 
     <price>5.95</price> 
     <publish_date>2000-12-16</publish_date> 
     <description>A former architect battles corporate zombies, 
     an evil sorceress, and her own childhood to become queen 
     of the world.</description> 
    </book> 
    <book id="bk103"> 
     <author>Corets, Eva</author> 
     <title>Maeve Ascendant</title> 
     <genre>Fantasy</genre> 
     <price>5.95</price> 
     <publish_date>2000-11-17</publish_date> 
     <description>After the collapse of a nanotechnology 
     society in England, the young survivors lay the 
     foundation for a new society.</description> 
    </book> 
</catalog> 

그것은 단지 중 하나가 표시 업로드 양식이나 책의 테이블

Title      Author 
XML Developer's Guide  Gambardella, Matthew 
Midnight Rain    Ralls, Kim 
Maeve Ascendant   Corets, Eva 

또한 아래 링크에서 언급했듯이 ... 이동하거나 임시 파일의 이름을 바꾸지 않으면 PHP 스크립트가 끝날 때 삭제됩니다. php:: how long to tmp files stay?