The OpenNET Project / Index page

[ новости /+++ | форум | теги | ]




Версия для распечатки Пред. тема | След. тема
Новые ответы [ Отслеживать ]
Помогите с .htaccess , !*! Bek, 25-Сен-03, 23:26  [смотреть все]
Есть папка для загрузки посетителями своих картинок, атр. папки - Write all. Необходимо разрешить загрузку опр. типа файлов .jpg .gif, .png и запретить остальные (именно запись, а не доступ к папке).
Перешерстил инет, везде примеры с разрешением и запретом именно доступа. Неужели нельзя это сделать с записью файла?
  • Помогите с .htaccess , !*! Dif, 15:49 , 26-Сен-03 (1)
    Это не в .htaccess прописывается, а в самом скрипте загрузки.
    вот код на ПХП. переделаешь под себя и все чики-пики:) Бай.

    <?php
    /*
    * File uploads made easy
    */
                            
    $my_max_file_size     = "1073741824";     # in bytes
    $image_max_width    = "1600";     # in pixels
    $image_max_height    = "1600";    # in pixels
    $the_path        = "/usr/www";


    $registered_types = array(
        "application/x-gzip-compressed"     => ".tar.gz, .tgz",
        "application/x-zip-compressed"         => ".zip",
        "application/x-tar"            => ".tar",
        "text/plain"                => ".html, .php, .txt, .inc (etc)",
        "image/bmp"                 => ".bmp, .ico",
        "image/gif"                 => ".gif",
        "image/pjpeg"                => ".jpg, .jpeg",
        "image/jpeg"                => ".jpg, .jpeg",
        "application/x-shockwave-flash"     => ".swf",
        "application/msword"            => ".doc",
        "application/vnd.ms-excel"        => ".xls",
        "application/octet-stream"        => ".exe, .fla (etc)"
    ); # these are only a few examples, you can add as many as you like


    $allowed_types = array("application/msword","application/vnd.ms-excel");


    ///////////////////////////////////////////////
    function form($error=false) {
    global $PHP_SELF,$my_max_file_size;
        if ($error) print $error . "<br><br>";
        
        print "\n<form ENCTYPE=\"multipart/form-data\"  action=\"" . $PHP_SELF . "\" method=\"post\">";
        print "\n<INPUT TYPE=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"" . $my_max_file_size . "\">";
        print "\n<INPUT TYPE=\"hidden\" name=\"task\" value=\"upload\">";
        print "\n<P>Upload a file";

    // Real max file size is ($my_max_file_size / 1024) - We are trying to put pepople off adding bigger files

        print "\n<BR>NOTE: Max file size is " . 200 . "KB";
        print "\n<br><INPUT NAME=\"the_file\" TYPE=\"file\" SIZE=\"35\"><br>";
        print "\n<input type=\"submit\" Value=\"Upload\">";
        print "\n</form>";

    } # END form

    if (!ereg("^4",phpversion())) {
        function in_array($needle,$haystack) { # we have this function in PHP4, so for you PHP3 people
            for ($i=0; $i < count($haystack); $i++) {
                if ($haystack[$i] == $needle) {    
                    return true;
                }
            }
        }
    }
    //////////////////////////////////////////////////////

    function validate_upload($the_file) {

    global $my_max_file_size, $image_max_width, $image_max_height,$allowed_types,$the_file_type,$registered_types;
        
    $start_error = "\n<b>Error:</b>\n<ul>";
        
        if ($the_file == "none") { # do we even have a file?
        
            $error .= "\n<li>Вы не выбрали ни какого файла!</li>";
        
        } else { # check if we are allowed to upload this file_type
        
            if (!in_array($the_file_type,$allowed_types)) {
                $error .= "\n<li>Тип файла, который Вы хотите закачать, ".
                    "является недопустимым, Вы можите закачитавь только файлы типов:\n<ul>";
                while ($type = current($allowed_types)) {
                    $error .= "\n<li>" . $registered_types[$type] . " (" . $type . ")</li>";
                    next($allowed_types);
                }
                $error .= "\n</ul>";
            }
        
            if (ereg("image",$the_file_type) && (in_array($the_file_type,$allowed_types))) {
            
                $size = GetImageSize($the_file);
                list($foo,$width,$bar,$height) = explode("\"",$size[3]);
        
                if ($width > $image_max_width) {
                    $error .= "\n<li>Your image should be no wider than " .
                        $image_max_width . " Pixels</li>";
                }
                
                if ($height > $image_max_height) {    
                    $error .= "\n<li>Your image should be no higher than " .
                        $image_max_height . " Pixels</li>";
                }
            
            }
            
            if ($error) {
                $error = $start_error . $error . "\n</ul>";
                return $error;
            } else {
                return false;
            }
        }
    } # END validate_upload
    //////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////
    function upload($the_file) {
    global $the_path,$the_file_name;
        
        $error = validate_upload($the_file);
        if ($error) {
            form($error);
        } else { # cool, we can continue
    //do_insert();
            if (!@move_uploaded_file($the_file, $the_path . "/" . $the_file_name)) {
                form("\n<b>There was an error, check the path to and ".
                "the permissions for the upload directory</b>");
            } else {
    //            list_files();
    //            form();
            }
        }
    } # END upload
    //////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////

    switch($task) {
        case 'upload':
            upload($the_file);
        break;
        default:
            form();
    }

    ?>

    • Помогите с .htaccess , !*! Bek, 16:08 , 26-Сен-03 (2)
      >Это не в .htaccess прописывается, а в самом скрипте загрузки.
      >вот код на ПХП. переделаешь под себя и все чики-пики:) Бай.
      >

      Спасибо!!!!!

      • Помогите с .htaccess , !*! Bek, 17:44 , 26-Сен-03 (3)
        А все таки, это наверно можно сделать с помощью .htaccess, по типу:

        Запрет на группу файлов по маске:
        <Files "\.(inc|sql|...другие расширения...)$">
        order allow,deny
        deny from all
        </Files>

        Только запретить File upload по маске, а не доступ к папке. Может нужно использовать mod_rewrite ?

        • Помогите с .htaccess , !*! Dif, 18:11 , 26-Сен-03 (4)
          Не получилось что-то с кодом, который я дал?

          забыли исправить

          $allowed_types = array("application/msword","application/vnd.ms-excel");

          на

          $allowed_types = array("image/gif",image/pjpeg",".jpg, .jpeg","image/jpeg");

          Или еще что-то?


          • Помогите с .htaccess , !*! Bek, 18:35 , 26-Сен-03 (5)
            >Не получилось что-то с кодом, который я дал?
            >
            >забыли исправить
            >
            >$allowed_types = array("application/msword","application/vnd.ms-excel");
            >
            >на
            >
            >$allowed_types = array("image/gif",image/pjpeg",".jpg, .jpeg","image/jpeg");
            >
            >Или еще что-то?


            Нет-нет, с этим все впорядке, исправил точно так, скрипт работает...
            Просто, чень интересен вариант с .htaccess...




Партнёры:
PostgresPro
Inferno Solutions
Hosting by Hoster.ru
Хостинг:

Закладки на сайте
Проследить за страницей
Created 1996-2024 by Maxim Chirkov
Добавить, Поддержать, Вебмастеру