1

同じコードを 2 時間見ていて、何が問題なのかわかりません。未定義のインデックスエラーが発生するため、何かばかげている必要がありますが、表示されません。新鮮な目をしてください!

実際のエラー:

Notice: 未定義のインデックス: paper_attach in [redacted] 104 行目

Notice: 未定義のインデックス: paper_attach in [redacted] 行 105 エラー: ファイルがアップロードされていません

HTML:

    <label for="paper_attach">Attach the paper:</label> <input type="file" name"paper_attach" class="paper_metadata"><br />
       <label class="textarea" for="comments">Comments:</label><br /> <textarea name="comments"><?php if (isset($comments)) { echo $comments;} ?></textarea><br /><br />

    <input type="submit" value="Save">

</form>

PHP:

//Сheck that we have a file
        if(!empty($_FILES['paper_attach'])) {
            //Check if the file is pdf, doc or docx and it's size is less than 20MB
            $filename = basename($_FILES['paper_attach']['name']);
            $ext = substr($filename, strrpos($filename, '.') + 1);

            if ((($ext == "pdf") && ($_FILES["paper_attach"]["type"] == "application/pdf")) or  (($ext == "doc") && ($_FILES["paper_attach"]["type"] == "application/msword")) or (($ext == "docx") && ($_FILES["paper_attach"]["type"] == "application/vnd.openxmlformats-officedocument.wordprocessingml.document")) 
                && ($_FILES["paper_attach"]["size"] < 20000000)) {
                //Determine the path to which we want to save this file
                $attachment_url = 'uploads/'.$filename;
                //Check if the file with the same name already exists on the server
                if (!file_exists($attachment_url)) {
                    //Attempt to move the uploaded file to it's new place
                    if ((move_uploaded_file($_FILES['paper_attach']['tmp_name'],$attachment_url))) {
                        echo "It's done! The file has been saved as: ".$attachment_url;

                        // ** VALIDATIONS PENDING
                        $query = "SELECT [redacted]";
                        if ($query_run = mysql_query($query)) {
                            $query_num_rows = mysql_num_rows($query_run);
                            assert($query_num_rows<= 1);

                            if ($query_num_rows === 0) {
                                // There's no row with this pmid, so we can add it
                                $query = "INSERT [redacted]";

                                if ($query_run = mysql_query($query)) {
                                    header('Location: success.php');
                                }

                            } elseif ($query_num_rows === 1) {
                                echo 'There already is a paper with the PMID: '.$pmid.' in the database.';

                            }
                        }

                    } else {
                        echo "Error: A problem occurred during file upload!";
                    }

                } else {
                    echo "Error: File ".$_FILES["paper_attach"]["name"]." already exists";
                }
            } else {
                echo "Error: Only .doc, .docx or .pdf files under 20MB are accepted for upload.";
            }

        } else {
            echo $_FILES['paper_attach'];
            echo "Error: No file uploaded <br />".$_FILES['paper_attach']['error'];

        }
4

2 に答える 2

1

あなたはあなたの=を忘れた<input type="file">はずです:

<input type="file" name="paper_attach" class="paper_metadata" />

あなたの代わりに

<input type="file" name"paper_attach" class="paper_metadata">
于 2012-10-11T15:25:36.907 に答える
1

アップロードが実際に成功したという検証が不足しており、すべての処理コードはすべてが正常に行われたことを前提としています。たとえば、最低限必要なもの:

if ($_FILES['paper_attach']['error'] !== UPLOAD_ERR_OK) {
   die("Upload failed with error code " . $_FILES['paper_attach']['error']);'
}

同様に、他の問題:

  1. コードのどこにも$pmidが定義されていませんが、挿入クエリとhttpリダイレクトを使用しています。
  2. ユーザー提供の属性を使用してファイルタイプの検証を行っている['type']ため、悪意のあるユーザーがあらゆる種類のファイルをサーバーにアップロードできます。
于 2012-10-11T15:26:22.167 に答える