move_uploaded_file将文件放在目标目录之外

时间:2020-12-15 16:03:55

I currently have the following:

我目前有以下内容:

    if (in_array($_FILES['userfile']['type'], $mimeTypes))
      {

$target_path = "./uploads/{$_SESSION['email']}";

$target_path = $target_path . basename( $_FILES['userfile']['name']);

if(move_uploaded_file($_FILES['userfile']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['userfile']['name']).
    " has been uploaded";

} else{
    echo "There was an error uploading the file, please try again!";
}

When someone creates a login, a directory is created for the user using their email, 0666 permission.

当有人创建登录时,将使用他们的电子邮件0666权限为用户创建目录。

Currently, the move_uploaded_file() places the file just oustide of the user's directory, inside the uploads directory. The error is being thrown. I've checked many times that my relative path is correct.

目前,move_uploaded_file()将文件放在uploads目录中的用户目录的oustide。错误被抛出。我多次检查过我的相对路径是否正确。

I'm using Ubuntu and the uploaded file has a Lock symbol on it, but lets me drag and drop into the user's folder.

我正在使用Ubuntu并且上传的文件上有一个Lock符号,但是我可以拖放到用户的文件夹中。

2 个解决方案

#1


2  

Your $target_path appears to be missing a / following the user's directory, since basename()'s output won't include a /.

您的$ target_path似乎缺少用户目录后面的/,因为basename()的输出将不包含/。

// Append trailing slash
$target_path = "./uploads/{$_SESSION['email']}/";
$target_path = $target_path . basename( $_FILES['userfile']['name']);

The correct permissions for a directory should be 0755 (you don't want it world-writable!), as directories should have execute permissions to be traversed by the filesystem. You can make it group-writable (0775) and make sure the upload directories have their group set to the Apache web server user. This would be best done by setting setgid on the uploads/ directory and setting its group to the apache user so use directories created beneath it inherit the correct write permissions without being world-writable.

对目录的正确权限应该是0755(您不希望它是全局可写的!),因为目录应该具有文件系统遍历的执行权限。您可以使其成为可写组(0775)并确保上载目录的组已设置为Apache Web服务器用户。这最好通过在uploads /目录上设置setgid并将其组设置为apache用户来完成,因此使用在其下创建的目录将继承正确的写权限而不是全局可写的。

#2


1  

It looks like your $target_path needs a / after it, or joined with the next line will just make a file that has their email address prepended to it.

看起来你的$ target_path需要一个/之后,或者加入下一行只会创建一个文件,其前面有他们的电子邮件地址。

$target_path = "./uploads/{$_SESSION['email']}/";

$target_path = $target_path . basename( $_FILES['userfile']['name']);

If it's still putting it in the uploads/ directory, check that $_SESSION['email'] is set properly.

如果它仍然将它放在uploads /目录中,请检查$ _SESSION ['email']是否设置正确。

#1


2  

Your $target_path appears to be missing a / following the user's directory, since basename()'s output won't include a /.

您的$ target_path似乎缺少用户目录后面的/,因为basename()的输出将不包含/。

// Append trailing slash
$target_path = "./uploads/{$_SESSION['email']}/";
$target_path = $target_path . basename( $_FILES['userfile']['name']);

The correct permissions for a directory should be 0755 (you don't want it world-writable!), as directories should have execute permissions to be traversed by the filesystem. You can make it group-writable (0775) and make sure the upload directories have their group set to the Apache web server user. This would be best done by setting setgid on the uploads/ directory and setting its group to the apache user so use directories created beneath it inherit the correct write permissions without being world-writable.

对目录的正确权限应该是0755(您不希望它是全局可写的!),因为目录应该具有文件系统遍历的执行权限。您可以使其成为可写组(0775)并确保上载目录的组已设置为Apache Web服务器用户。这最好通过在uploads /目录上设置setgid并将其组设置为apache用户来完成,因此使用在其下创建的目录将继承正确的写权限而不是全局可写的。

#2


1  

It looks like your $target_path needs a / after it, or joined with the next line will just make a file that has their email address prepended to it.

看起来你的$ target_path需要一个/之后,或者加入下一行只会创建一个文件,其前面有他们的电子邮件地址。

$target_path = "./uploads/{$_SESSION['email']}/";

$target_path = $target_path . basename( $_FILES['userfile']['name']);

If it's still putting it in the uploads/ directory, check that $_SESSION['email'] is set properly.

如果它仍然将它放在uploads /目录中,请检查$ _SESSION ['email']是否设置正确。