在PHP中,move_uploaded_file将文件上传到一个目录,但无法上传到其他目录?

时间:2021-02-13 16:03:56

I am relatively new to PHP, and am working on a website which should accept functionality for when user's upload pdf, doc, or docx files. I was able to look up code online and see what the html form looks like (not my code, as I am simply trying to see if bare functionality for uploading works right now) and also the code for processing the upload. I will put the codes below, and I don't think there's anything wrong with the code itself, but maybe something to do with permissions for the directory I'm uploading the files to. Note: question might be too long, skip to end for clear question and to see what I've tried so far)

我是PHP的新手,我正在开发一个网站,该网站应该接受用户上传pdf,doc或docx文件时的功能。我能够在线查找代码并查看html表单的外观(不是我的代码,因为我只是想看看上传的功能是否正常工作)以及处理上传的代码。我将把代码放在下面,我不认为代码本身有任何问题,但可能与我上传文件的目录的权限有关。注意:问题可能太长,跳到最后是一个明确的问题,看看到目前为止我尝试了什么)

Here is the HTML (sample.php) for the form:

以下是表单的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>

Here is the PHP code (docs.php) that is used to upload the file to the specified directory:

以下是用于将文件上载到指定目录的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.";
    }
}
?>

So, with the code above, I am successfully able to upload a file to the path specified in the PHP code ("../../UPLOADS/"). However, if I try to change this path, all I get as output on the webpage is "File is a documentSorry, there was an error uploading your file."

因此,通过上面的代码,我成功地将文件上传到PHP代码中指定的路径(“../../UPLOADS/”)。但是,如果我尝试更改此路径,那么我在网页上输出的所有信息都是“文件是文档存档,上传文件时出错”。

Now, the path I am giving instead of "../../UPLOADS/" is just "test/". I created another folder in the directory where my webpages are, and named it "test"

现在,我给出的路径而不是“../../UPLOADS/”只是“test /”。我在我的网页所在的目录中创建了另一个文件夹,并将其命名为“test”

I am a university student, and so I am using my Linux account under my school's AFS servers. After I ssh into my account, I have to cd into my public_html directory. Once I am there, it contains:

我是一名大学生,所以我在学校的AFS服务器下使用我的Linux帐户。在我进入我的帐户后,我必须进入我的public_html目录。一旦我在那里,它包含:

UPLOADS/   
resport/
download/

The directory "resport" contains subdirectories for where my webpages are in, so if I cd into resport, I see:

目录“resport”包含我的网页所在的子目录,所以如果我进入resport,我会看到:

login/
student/

The directory "student" contains the subdirectory "UPLOADS" and also other webpages, so if I cd into student, I see:

目录“student”包含子目录“UPLOADS”以及其他网页,所以如果我进入学生,我会看到:

sample.php
docs.php
test/

The problem (clear and simple): I'd like it to be so that I can upload files into test in the "resport" directory and not UPLOADS in the "public_html" directory. I am able to upload files into "public_html/UPLOADS" (the path relative to where sample.php and docs.php is "../../UPLOADS/") but I need to upload the files into "public_html/resport/student/test" (the path relative to where sample.php and docs.php is "test/")

问题(清楚和简单):我希望它能够将文件上传到“resport”目录中的测试而不是“public_html”目录中的UPLOADS。我能够将文件上传到“public_html / UPLOADS”(相对于sample.php和docs.php所在的路径是“../../UPLOADS/”),但我需要将文件上传到“public_html / resport /” student / test“(相对于sample.php和docs.php所在的路径为”test /“)

What I've tried: I have checked permissions for the UPLOADS directory and it is drwxrwxrwx so I also set the same permissions for the test directory, and gave it a go, but nothing changed. I was still seeing "File is a documentSorry, there was an error uploading your file." I have also tried renaming "test" to "UPLOADS" and changing permissions again, but still nothing. I'm not sure what the issue could be. I'd greatly appreciate any help, thanks!

我试过的:我已经检查了UPLOADS目录的权限,它是drwxrwxrwx,所以我也为测试目录设置了相同的权限,并给它一个去,但没有任何改变。我仍然看到“文件是一个文件,但上传文件时出错”。我也尝试将“test”重命名为“UPLOADS”并再次更改权限,但仍然没有。我不确定问题是什么。非常感谢任何帮助,谢谢!

UPDATE Put a full path to test/ instead of a relative one. Something like this:

更新放置测试/而不是相对路径的完整路径。像这样的东西:

/afs/univ/u/c/r/ucid/public_html/resport/student/test/

but still no luck Instead of echoing

但仍然没有运气而不是呼应

echo "Sorry, there was an error uploading your file.";

I tried changing it to:

我尝试将其更改为:

echo " Not uploaded because of error #".$_FILES["file"]["error"];

but all I get now is:

但我现在得到的是:

"File is a document Not uploaded because of error #"

There is no number after

之后没有号码

.$_FILES["file"]["error"]

1 个解决方案

#1


0  

Change: $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);

更改:$ target_file = $ target_dir。基名($ _ FILES [ “fileToUpload”] [ “名称”]);

To: $target_file = $target_dir . $_FILES["fileToUpload"]["name"];

要:$ target_file = $ target_dir。 $ _FILES [ “fileToUpload”] [ “名称”];

The basename() command changes the directory to put the file.

basename()命令更改目录以放置文件。

Make sure the / after the directory name is set.

确保设置了目录名后的/。

#1


0  

Change: $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);

更改:$ target_file = $ target_dir。基名($ _ FILES [ “fileToUpload”] [ “名称”]);

To: $target_file = $target_dir . $_FILES["fileToUpload"]["name"];

要:$ target_file = $ target_dir。 $ _FILES [ “fileToUpload”] [ “名称”];

The basename() command changes the directory to put the file.

basename()命令更改目录以放置文件。

Make sure the / after the directory name is set.

确保设置了目录名后的/。