2014-02-08 6 views
0

index.php를PHP 텍스트 편집기

 <h1>View text files in a directory</h1> 

     <form action="" method="POST"> 
     Directory name: <input name="folderName4" type="text" required="required"> <br> 
     <input type="submit" value="View Directories Text Files"> 
     </form> 

     <br> 

     <?php 
     if (isset($_POST['folderName4'])){ 
     $foldername = $_POST["folderName4"]; 

     $directory = "upload"."/".$foldername."/";; 

     $files = glob($directory . "*.txt"); 

     foreach($files as $file) 
     { 
     echo "<a href=edit.php>".$file."</a>"; 
     echo "<br>"; 
     } 
     } 
     ?> 

edit.php

<?php 

$url = 'http://127.0.0.1/ccb/edit.php'; 

$file = ; 

if (isset($_POST['text'])) 
{ 
    file_put_contents($file, $_POST['text']); 

    // redirect to the form, avoids refresh warnings from the browser 
    header(sprintf('Location: %s', $url)); 
    printf('<a href="%s">Moved</a>.', htmlspecialchars($url)); 
    exit(); 
} 

$text = file_get_contents($file); 

?> 

<form action="" method="post"> 
<textarea name="text"><?php echo htmlspecialchars($text) ?></textarea> 
<input type="submit"> 
</form> 

좋아요 그래서 index.php를 모두 나열 텍스트 파일을. edit.php의 $ file 변수는 텍스트 파일의 경로입니다. 일단 클릭하면 링크의 텍스트와 경로를 동일하게 만드는 방법은 무엇입니까? 일단 텍스트 파일 링크를 클릭하면 편집기에서 열리는 것이 좋습니다. 도움이된다면 고맙겠습니다. 감사합니다.

+0

어떤 쿼리 문자열에 넣어 어떻습니까? GET 매개 변수로 보내는 것이 가장 쉬운 방법입니다. ('echo "".$file."";'). 우선이 절차에서 보안 문제를 고려할 것입니다. – jtheman

+0

보안에 대한 실질적인 걱정은 없습니다. 이 코드는 대학 과제를위한 것입니다. 필자 자신에게는 PHP가 처음이다. 도와 주셔서 감사합니다. – ech0

답변

0

이 작업을 수행하려했는지 확실하지는 않지만 URL에 파일 이름을 추가 할 수있는? file = $ 파일은 URL의 데이터를 GET 엔티티로 전달합니다. 아래 편집 된 edit.php 파일에서 호출됩니다.

index.php를

<form action="" method="POST"> 
    Directory name: <input name="folderName4" type="text" required="required"> <br> 
    <input type="submit" value="View Directories Text Files"> 
    </form> 

    <br> 

    <?php 
    if (isset($_POST['folderName4'])){ 
    $foldername = $_POST["folderName4"]; 

    $directory = "upload"."/".$foldername."/";; 

    $files = glob($directory . "*.txt"); 

    foreach($files as $file) 
    { 
    echo "<a href='edit.php?file=".$file."'>".$file."</a>"; // <-- Filename part of URL 
    echo "<br>"; 
    } 
    } 
    ?> 

edit.php

<?php 

$url = 'http://127.0.0.1/ccb/edit.php'; 

$file = $_GET['file']; // <-- Inserted GET here 

if (isset($_POST['text'])) 
{ 
    file_put_contents($file, $_POST['text']); 

    // redirect to the form, avoids refresh warnings from the browser 
    header(sprintf('Location: %s', $url)); 
    printf('<a href="%s">Moved</a>.', htmlspecialchars($url)); 
    exit(); 
} 

$text = file_get_contents($file); 

?> 

<form action="" method="post"> 
<textarea name="text"><?php echo htmlspecialchars($text) ?></textarea> 
<input type="submit"> 
</form> 
+0

그게 정확히 내가하려는 일이야. 감사. – ech0

+0

환영합니다. 기꺼이 도와 드리겠습니다! – Kai

+0

'urlencode ($ file)'에 urlencode라는 파일명을 urlencode 한 다음 edit.php에 urldecode를 다시 넣을 것입니다. http://se1.php.net/urlencode – jtheman