-1

このフォーム内のデータベースに記事をアップロードするためのフォームがあります。ファイル アップロード フィールドがあります。ファイル アップロードの目的は次のとおりです。

  1. ファイル拡張子を画像形式に制限する
  2. ファイルの名前をランダムなものに変更する
  3. データベースへのエントリの ID に基づいて新しいディレクトリを作成します
  4. アップロードされたファイルの拡張子を保持する

送信ボタンを押すたびに、許可されている拡張子のリストにないファイルをアップロードしようとしたとフォームが判断しているように見え、このエラーが出力されます (ただし、フィールドはデータベースにアップロードされます)。

wrong files format , allowed only "Array"

正しいファイル形式を入力していることはわかっているので、なぜそうなるのかはよくわかりません。

    public function insert ($field) {

            if ($stmt = $this->mysqli->prepare("INSERT INTO articles (title, story, storyb, storyc, author, date_created, section, youtubeid) VALUES (?, ?, ?, ?, ?, ?, ?, ?)")) {

                        /* Set our params */

                         $title = isset($_POST['title']) ? $this->mysqli->real_escape_string($_POST['title']) : '';
                         $story = isset($_POST['story']) ? $this->mysqli->real_escape_string($_POST['story']) : '';
                         $storyb = isset($_POST['storyb']) ? $this->mysqli->real_escape_string($_POST['storyb']) : '';
                         $storyc = isset($_POST['storyc']) ? $this->mysqli->real_escape_string($_POST['storyc']) : '';
                         $author = isset($_POST['author']) ? $this->mysqli->real_escape_string($_POST['author']) : '';
                         $date_created = isset($_POST['date_created']) ? $this->mysqli->real_escape_string($_POST['date_created']) : '';
                         $section = isset($_POST['section']) ? $this->mysqli->real_escape_string($_POST['section']) : '';
                         $youtubeid = isset($_POST['youtubeid']) ? $this->mysqli->real_escape_string($_POST['youtubeid']) : '';

                        /* Bind our params */
                        $stmt->bind_param('ssssssss', $title, $story, $storyb, $storyc, $author, $date_created, $section, $youtubeid);

                        /* Execute the prepared Statement */
                        $stmt->execute();

                        /* Echo results */
                        echo "Inserted {$title} into database\n";

                        /* Close the statement */
                        $stmt->close();
                        }
                        else {
                        /* Error */
                        printf("Prepared Statement Error: %s\n", $mysqli->error);
                        }

            // Handling file upload

            $extensions = array(".jpg",".jpeg",".gif",".png", ".JPG", ".JPEG", ".PNG", ".GIF");
            $extension = strrchr($_FILES['uploadImage']['name'], '.');

            $path =  "../files/uploads/articles_gallery/" . $this->mysqli->insert_id;
            $filename = uniqid(rand(), true);            


                if (!in_array($extension, $extensions))
                    {
                        echo'<center>wrong files format , allowed only <strong>"'.$extensions.'"</strong></center>';
                    }  else {

                            if (!is_dir($path))
                          {
                            die('Error: ' . $mysqli->error());
                          }

                                    echo "<h3>1 record added</h3>";
                                    mkdir($path, 0777);
                                    move_uploaded_file($_FILES['uploadImage']['tmp_name'], $path, $filename);
                    } // File Upload End

        }

Insert.php

<div id="form">
<form action="insert.php" method="post" name="insert" id="articleform">
            <input type="input" name="title" id="title" class="detail" id="title"/>
            <textarea name="story" id="story" class="detail" placeholder="Insert article here"></textarea>
            <input id="uploadImage" type="file" name="uploadImage" onchange="PreviewImage();" class="" />
    <img id="uploadPreview" style="width: 250px; height: 200px;" />
            <textarea name="storyb" id="storyb" class="detail" spellcheck="true" placeholder="Insert article here"></textarea>        
            <textarea name="storyc" id="storyc" class="detail" spellcheck="true" placeholder="Insert article here"></textarea>
            <input type="input" name="author" id="author" class="detail"/>
            <? $today = date("l j M Y");  // Monday 13 April 2013 ?>
    <input type="hidden" name="date_created" id="date_created" class="detail" value="<? echo $today;?>" />            
            <input type="hidden" name="section" id="section" class="detail" value="game"/>           
            <input type="input" name="youtubeid" class="detail" id="youtubeid" />
            <input type="submit" id="submit" name="submit" value="Submit Article    " />
        </form>
4

4 に答える 4

0

strchr()は、文字 (あなたの場合は ".") が文字列に存在する場合に true/false を返します。したがって、ファイル拡張子を取得するには、次のようなものを使用できます。

$extension = end(explode('.', $filename));

..しかし、この場合にもpathinfo()のような関数が役立つかもしれません。

さらに、エラー メッセージでは$extensions. これは「配列」型の var であるため、文字列表現は「配列」です。私はあなた$extensionがそこに(Sなしで)意味していたと思います。または、次のようにすべての正しい拡張子を一覧表示することもできますimplode(',', $extensions)

拡張機能のチェックは、存在するコンテンツの種類をチェックする安全な方法ではないことに注意してください。名前を .jpg に変更するだけで、.exe を簡単にアップロードできます。

ああ、ディレクトリ/ファイルをモード777にchmodすることは、セキュリティ上の理由から強くお勧めしません。

于 2013-04-17T13:28:57.343 に答える
0

利用可能な拡張子を表示したい場合は、配列をメッセージと連結することはできません。すべての値を反復する必要があります。

于 2013-04-17T13:29:06.663 に答える
0

拡張子を取得するコードが間違っています。

$extension = strrchr($_FILES['uploadImage']['name'], '.');

に変更します

$extension = strtolower(substr($_FILES['uploadImage']['name'],-3,3));

拡張子が 3 文字の長さであるか、explode() の場合は、最後のパラメーターを取得します。とにかくこれは良い方法ではありません.MIMEタイプをチェックすることを好みます

$extension = strtolower(end(explode('.', $_FILES['uploadImage']['name'])));

拡張配列を次のように変更します

$extensions = array("jpg","jpeg","gif","png");

フォームにも追加

enctype="multipart/form-data"

文字列の代わりに配列をエコーし​​たいため、エラーメッセージは間違っています implode() を使用して配列に参加します

implode(";",$extensions)
于 2013-04-17T13:29:07.633 に答える