Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Usuwanie pliku przez użytkownika
tosiek
post
Post #1





Grupa: Zarejestrowani
Postów: 142
Pomógł: 0
Dołączył: 7.07.2008

Ostrzeżenie: (0%)
-----


Witam. Mam prosty zamiar dać użytkownikowi możliwość usunięcia pliku.

Aktualnie zapisuję sobie:
  1. <?php
  2. $czas = time();
  3. $nazwa = $czas . $_FILES['userfile']['name'];
  4. file_put_contents(dirname(__FILE__) . "/../../../../wynikowe/" . $nazwa, $plik);
  5. ?>


No i podaję bezpośredni link do pobrania. I link do usunięcia który działa w ten sposób:
  1. <?php
  2. if(isset($_GET['usun']))
  3. {
  4. unlink(dirname(__FILE__) . "/../../../../wynikowe/" . urldecode($_GET['usun']));
  5. echo "plik usuniety";
  6. }
  7. echo '<a href="http://tosiek.pl/converter/?usun=' . urlencode($nazwa) . '">Usuń plik: ' . $_FILES['userfile']['name'] . '</a>';
  8. ?>


I teraz pytanie czy przez to unlink da się usunąć inne pliki, poza katalogiem wynikowe ? I jak to zabezpieczyć ?
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
leovandamon
post
Post #2





Grupa: Zarejestrowani
Postów: 41
Pomógł: 3
Dołączył: 5.01.2008
Skąd: Czwarty Świat

Ostrzeżenie: (0%)
-----


Witam!

Napisałem kiedyś funkcyjkę... Może ci się przyda. Ja używam jej do czyszczenia zmiennej z nazwą includowanego pliku.

  1. <?php
  2. function clear($text,$mode='easy') {
  3.    switch($mode) {
  4.        case 'full' : // For filenames, etc. Very secure clearing
  5.            $text = str_replace('~','',$text); $text = str_replace('!','',$text);
  6.            $text = str_replace('`','',$text); $text = str_replace('@','',$text);
  7.            $text = str_replace('#','',$text); $text = str_replace('(','',$text);
  8.            $text = str_replace('$','',$text); $text = str_replace(')','',$text);
  9.            $text = str_replace('%','',$text); $text = str_replace('_','',$text);
  10.            $text = str_replace('^','',$text); $text = str_replace('-','',$text);
  11.            $text = str_replace('&','',$text); $text = str_replace('+','',$text);
  12.            $text = str_replace('*','',$text); $text = str_replace('=','',$text);
  13.            $text = str_replace('{','',$text); $text = str_replace('|','',$text);
  14.            $text = str_replace('}','',$text); $text = str_replace(':','',$text);
  15.            $text = str_replace('[','',$text); $text = str_replace(';','',$text);
  16.            $text = str_replace(']','',$text); $text = str_replace('"','',$text);
  17.            $text = str_replace('','',$text); $text = str_replace('&#092;'','',$text);
  18.            $text = str_replace('<','',$text); $text = str_replace('.','',$text);
  19.            $text = str_replace(',','',$text); $text = str_replace('?','',$text);
  20.            $text = str_replace('>','',$text); $text = str_replace('/','',$text);
  21.            $text = str_replace('ą','a',$text); $text = str_replace('ł','l',$text);
  22.            $text = str_replace('ć','c',$text); $text = str_replace('ń','n',$text);
  23.            $text = str_replace('ę','e',$text); $text = str_replace('ó','o',$text);
  24.            $text = str_replace('ś','s',$text); $text = str_replace('ż','z',$text);
  25.            $text = str_replace('ź','z',$text); $text = str_replace('Ź','z',$text);
  26.            $text = str_replace('Ą','A',$text); $text = str_replace('Ł','L',$text);
  27.            $text = str_replace('Ć','C',$text); $text = str_replace('Ń','N',$text);
  28.            $text = str_replace('Ę','E',$text); $text = str_replace('Ó','O',$text);
  29.            $text = str_replace('Ś','S',$text); $text = str_replace('Ż','Z',$text);
  30.            break;
  31.        case 'normal' : // Normal clearing
  32.            $text = str_replace('&','&',$text);
  33.            $text = str_replace('=','=',$text); $text = str_replace('!','!',$text);
  34.            $text = str_replace('`','`',$text); $text = str_replace('@','@',$text);
  35.            $text = str_replace('#','#',$text); $text = str_replace('(','(',$text);
  36.            $text = str_replace('$','$',$text); $text = str_replace(')',')',$text);
  37.            $text = str_replace('%','&percent;',$text); $text = str_replace('_','_',$text);
  38.            $text = str_replace('^','^',$text); $text = str_replace('-','-',$text);
  39.            $text = str_replace('+','+',$text); $text = str_replace('~','~',$text);
  40.            $text = str_replace('{','{',$text); $text = str_replace('|','|',$text);
  41.            $text = str_replace('}','}',$text); $text = str_replace(':',':',$text);
  42.            $text = str_replace('[','[',$text); $text = str_replace(';',';',$text);
  43.            $text = str_replace(']',']',$text); $text = str_replace('"','"',$text);
  44.            $text = str_replace('','&#092;',$text); $text = str_replace(''','´',$text);
  45.            $text = str_replace('<','<',$text); $text = str_replace('.','.',$text);
  46.            $text = str_replace(',',',',$text); $text = str_replace('?','?',$text);
  47.            $text = str_replace('>','>',$text); $text = str_replace('/','⁄',$text);
  48.            $text = str_replace('ą','a',$text); $text = str_replace('ł','l',$text);
  49.            $text = str_replace('ć','c',$text); $text = str_replace('ń','n',$text);
  50.            $text = str_replace('ę','e',$text); $text = str_replace('ó','o',$text);
  51.            $text = str_replace('ś','s',$text); $text = str_replace('ż','z',$text);
  52.            $text = str_replace('ź','z',$text); $text = str_replace('Ź','z',$text);
  53.            $text = str_replace('Ą','A',$text); $text = str_replace('Ł','L',$text);
  54.            $text = str_replace('Ć','C',$text); $text = str_replace('Ń','N',$text);
  55.            $text = str_replace('Ę','E',$text); $text = str_replace('Ó','O',$text);
  56.            $text = str_replace('Ś','S',$text); $text = str_replace('Ż','Z',$text);
  57.            break;
  58.        case 'easy' : // Base clearing
  59.            $text = str_replace('','',$text); $text = str_replace('&#092;'','',$text);
  60.            $text = str_replace('./','',$text);
  61.            break;
  62.        default : echo '<br />Fatal error: wrong param defined for function clear on line '.__LINE__.'<br />';
  63.    }
  64.    return $text;
  65. }
  66. ?>


i użycie:

  1. <?php
  2. $nazwa = clear(urlencode($_GET['usun']));
  3. ?>


Możesz podać drugi parametr (easy, normal, full). Easy jest domyślnym i jest "akurat" do nazw plików. Normal zamienia większość znaków na encje HTML i do tego polskie znaki diakrytyczne na te bez "kropek" i "ogonków". Full całkiem usuwa wszystkie bardziej "niebezpieczne" znaki.

Na pewno możnaby to zrobić lepiej, jednak pisałem to parę lat temu. Mnie wystarcza.

Poza tym, niedawno pisałem "eksplorator", być może coś ci się przyda.

http://dragonsheart.eu/-/php/manager.txt

Część kodu to funkcje bezpośrednio to użycia w moim programie, więc nie zwracaj uwagi. (IMG:http://forum.php.pl/style_emoticons/default/winksmiley.jpg)

Pozdrawiam,
Leo van Damon
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 27.12.2025 - 21:35