【发布时间】:2018-05-02 21:52:00
【问题描述】:
我对 PHP 比较陌生,并且正在开发一个网站,该网站应该接受用户上传 pdf、doc 或 docx 文件时的功能。我能够在线查找代码并查看 html 表单的外观(不是我的代码,因为我只是想看看现在上传的裸功能是否有效)以及处理上传的代码。我将代码放在下面,我认为代码本身没有任何问题,但可能与我将文件上传到的目录的权限有关。 注意:问题可能太长,请跳到结尾以获得明确的问题并查看我到目前为止尝试过的内容)
这是表单的 HTML (sample.php):
<!DOCTYPE html>
<html>
<body>
<form action="docs.php" method="post" enctype="multipart/form-data">
Select document to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Document" name="submit">
</form>
</body>
</html>
这是用于将文件上传到指定目录的PHP代码(docs.php):
<?php
$target_dir = "../../UPLOADS/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$documentFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = filesize($_FILES["fileToUpload"]["tmp_name"]);
if($check > 0) {
echo "File is a document";
$uploadOk = 1;
} else {
echo "File is not a document.";
$uploadOk = 0;
}
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 100000000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($documentFileType != "pdf" && $documentFileType != "doc" &&
$documentFileType != "docx") {
echo "Sorry, only PDF, DOC, & DOCX 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 代码中指定的路径(“../../UPLOADS/”)。但是,如果我尝试更改此路径,我在网页上得到的所有输出都是“文件是文档抱歉,上传文件时出错。”
现在,我给出的路径而不是“../../UPLOADS/”只是“test/”。我在我的网页所在的目录中创建了另一个文件夹,并将其命名为“test”
我是一名大学生,因此我在学校的 AFS 服务器下使用我的 Linux 帐户。在我 ssh 进入我的帐户后,我必须 cd 进入我的 public_html 目录。一旦我在那里,它包含:
UPLOADS/
resport/
download/
目录“resport”包含我的网页所在的子目录,所以如果我 cd 进入 resport,我会看到:
login/
student/
目录“学生”包含子目录“上传”和其他网页,所以如果我 cd 进入学生,我会看到:
sample.php
docs.php
test/
问题(清晰而简单): 我希望这样我可以将文件上传到“resport”目录中的测试,而不是“public_html”目录中的上传。我可以将文件上传到“public_html/UPLOADS”(相对于 sample.php 和 docs.php 的路径是“../../UPLOADS/”),但我需要将文件上传到“public_html/resport/” student/test”(相对于 sample.php 和 docs.php 的路径是“test/”)
我尝试过的: 我检查了 UPLOADS 目录的权限,它是 drwxrwxrwx,所以我也为 test 目录设置了相同的权限,试了一下,但没有任何改变。我仍然看到“文件是文档抱歉,上传文件时出错。”我也尝试将“test”重命名为“UPLOADS”并再次更改权限,但仍然没有。我不确定问题可能是什么。非常感谢任何帮助,谢谢!
更新 放置一个完整的路径 test/ 而不是相对路径。像这样的:
/afs/univ/u/c/r/ucid/public_html/resport/student/test/
但仍然没有运气 而不是回显
echo "Sorry, there was an error uploading your file.";
我尝试将其更改为:
echo " Not uploaded because of error #".$_FILES["file"]["error"];
但我现在得到的只是:
"File is a document Not uploaded because of error #"
后面没有数字
.$_FILES["file"]["error"]
【问题讨论】:
-
"但我需要将文件上传到 "public_html/resport/student/test"" - 这不是您用作上传文件夹
$target_dir = "../../UPLOADS/";的文件。 -
抱歉,我应该更具体一些。 “../../UPLOADS/”是从包含网页的目录到位于 public_html/ 中的 UPLOADS 文件夹的相对路径
-
然后使用完整的服务器/系统路径。
-
也试过了,还是不行。测试目录的绝对路径是:/afs/univ/u/c/r/ucid/public_html/resport/student/test/ 但是还是不行
标签: php html linux file-upload permissions