嗨,我用这个简单的代码上传"txt"文件(index.html+upload.php)。它运行良好,上传的txt文件保存在服务器上,我可以看到txt内容。问题是只有当我想在脚本中使用该文件作为输入,只过滤文本中的一些单词时。我使用的脚本是:http://pastebin.com/U9yJL0t0我知道剧本很好。我通过终端用手动创建的txt文件测试了它,效果很好。问题只是上传的文件使用HTML和PHP。
index.html
<html>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>
upload.php
<?php
$target_dir = "/var/www/tmp/test/$mid.txt";
$target_file = $target_dir;
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "txt") {
echo "Sorry, only txt files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
问题是,当PHP创建文件时,文件的所有者和组很可能是www数据。您可以使用sudo运行脚本,或者更改文件的所有者或权限。出于安全考虑,我建议更改所有权。
以下是更改所有权的命令:
chown username.username filename.txt