我一直在尋找相同的代碼兩個小時,現在我無法弄清楚問題所在。它必須是愚蠢的,因爲我得到一個未定義的索引錯誤,但我只是沒有看到它。 請給它一些新鮮的眼睛!帶有文件上傳字段無法上傳的PHP/HTML表單,無法弄清楚爲什麼
實際的錯誤:
Notice: Undefined index: paper_attach in [redacted] on line 104
Notice: Undefined index: paper_attach in [redacted] on line 105 Error: No file uploaded
的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'];
}
什麼索引是未定義的,在哪一行? –
你從那個php得到的輸出是什麼? – xception
對不起,我編輯了這個問題,因爲SO在我身上打了一個。我希望現在一切都得到解決。 – margaritam