PHP:
// If the user accessed the page by form submittal
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$filename = $_FILES["file-input"]["name"];
$extension = pathinfo($filename, PATHINFO_EXTENSION);
$filesize = filesize($filename);
$max_filesize = 524288;
// Array to hold errors
$errors = array();
if (!$filename) {
array_push($errors, "You didn't actually upload anything!");
}
else if ($extension != "txt") {
array_push($errors, "That's not a valid file-type. Please only use txt files.");
}
else if (filesize($filename)) {
array_push($errors, "The txt file must not be empty. Give us a good story.");
}
HTML:
<div class="upload-form">
<form action="" method="post" enctype="multipart/form-data">
<div class="upload">
<input type="file" name="file-input">
<span class="input-filename">Select a file...
</span>
<input type="button" value="Browse">
</div>
<input type="submit" name="submit" value="Upload">
<span class="valid-formats">Valid input: .txt files <= 512 KB</span>
</form>
</div>
我得到错误:
警告:filesize(): stat failed for todo.txt in .../Dropbox/Projects/Website Projects/serverside/assignment2/index.php 在第 34 行
警告:filesize(): stat failed for todo.txt in .../Dropbox/Projects/Website Projects/serverside/assignment2/index.php 在第 46 行
警告:file_get_contents(todo.txt):无法打开流:第 77 行的 .../Dropbox/Projects/Website Projects/serverside/assignment2/index.php 中没有此类文件或目录