简体   繁体   English

在PHP中,move_uploaded_file将文件上传到一个目录,但是无法上传到另一个目录吗?

[英]In PHP, move_uploaded_file uploads files to one directory, but fails to upload for a different directory?

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. 我对PHP相对较新,并且正在一个网站上工作,该网站应该接受用户上载pdf,doc或docx文件时的功能。 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. 我能够在线查找代码,并查看html表单的外观(不是我的代码,因为我只是想立即查看裸露的上传功能是否正常工作)以及处理上载的代码。 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) 注意 :问题可能太长,请跳到结尾以解决明确的问题,并查看到目前为止我已尝试过的操作)

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/"). 因此,使用上面的代码,我可以成功地将文件上传到PHP代码中指定的路径(“ ../../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." 但是,如果我尝试更改此路径,则在网页上显示的所有内容是“文件是documentSorry,上传文件时出错。”

Now, the path I am giving instead of "../../UPLOADS/" is just "test/". 现在,我提供的路径不是“ ../../UPLOADS/”,而是“ test /”。 I created another folder in the directory where my webpages are, and named it "test" 我在网页所在的目录中创建了另一个文件夹,并将其命名为“ test”

I am a university student, and so I am using my Linux account under my school's AFS servers. 我是一名大学生,因此我在学校的AFS服务器下使用Linux帐户。 After I ssh into my account, I have to cd into my public_html directory. SSH进入我的帐户后,我必须进入cd到public_html目录。 Once I am there, it contains: 我到那里后,它包含:

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”以及其他网页,因此,如果我将CD装入学生,则会看到:

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. 问题(清晰而又简单):我希望这样做,以便可以将文件上传到“ resport”目录中的测试中,而不是“ public_html”目录中的UPLOADS。 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/") 我可以将文件上传到“ public_html / UPLOADS”(相对于sample.php和docs.php的路径是“ ../../UPLOADS/”),但是我需要将文件上传到“ public_html / resport /学生/测试”(相对于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. 我尝试过的操作:我已经检查了UPLOADS目录的权限,它是drwxrwxrwx,所以我也为测试目录设置了相同的权限,并给了它一个权限,但是没有任何改变。 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. 我也尝试过将“ test ”重命名为“ UPLOADS ”,然后再次更改权限,但还是没有。 I'm not sure what the issue could be. 我不确定是什么问题。 I'd greatly appreciate any help, thanks! 非常感谢您的帮助,谢谢!

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"]

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

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

The basename() command changes the directory to put the file. basename()命令更改目录以放置文件。

Make sure the / after the directory name is set. 确保设置了目录名称后的/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM