-1

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 &lt;= 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 中没有此类文件或目录

4

4 回答 4

2

该文件不在$_FILES['file-input']['name']. 这将告诉您上传时客户端计算机上文件的文件名。PHP 将文件存储在$_FILES['file-input']['tmp_name']服务器上。因此,pathinfo将不起作用(它需要文件在磁盘上)。

您可以$_FILES['file-input']['name']按点拆分,并获得最后一个值:

$parts = explode(".", $_FILES['file-input']['name']);
$extension = end($parts);
于 2013-02-20T13:40:22.827 回答
0

请注意在不可写的文件/目录上使用此功能:您将收到如下警告:

警告:filesize() [function.filesize]: stat failed for /var/www/xxx/yyy.php in /var/www/xxx/yyy.php on line 123

于 2013-02-20T13:43:47.490 回答
0

上传的文件路径保存在 中$_FILES["file-input"]["tmp_name"];,如果要对文件进行操作,需要使用该路径。

您当前使用$_FILES["file-input"]["name"]的不是您想要的。它包含用户机器上的原始文件名。

于 2013-02-20T13:46:02.637 回答
0

试试这个。

       move_uploaded_file($_FILES['file-input']['tmp_name'],"file_upload_location/".$_FILES['file-input']['name']);

它可能会起作用。

于 2013-02-20T13:46:36.807 回答