我的文件没有被移动到使用 mkdir 指定的新创建的文件夹

发布于 2024-09-08 23:20:19 字数 1633 浏览 6 评论 0原文

所以我有一个问题。当有人在我正在构建的网站上注册帐户时,他们会进入一个设置页面,在那里他们可以上传个人资料图片。我正在使用 mkdir 自动创建一个带有用户名的子文件夹,该子文件夹工作正常,并且该子文件夹上的 CHMOD 为 777。我遇到的问题是图像没有被移动到指定的子文件夹,而是进入“uploads”文件夹,这是它的父目录。

希望我没有让任何人感到困惑,但我真的需要帮助。

下面是我为此编写的脚本。

=================================================== =============================

require_once('../admin/includes/config.php');

$uploaded_file = $_REQUEST['uploaded_file'];
$member_id = $_REQUEST['member_id'];
$name = $_REQUEST['name'];
$location = $_REQUEST['location'];
$hobbies = $_REQUEST['hobbies'];
$hobby = $_REQUEST['hobby'];

// using member_id, extract the username from the members table so we can auto create a sub-directory for the images

$result = mysql_query("SELECT username FROM members WHERE member_id = '$member_id'");

while($row = mysql_fetch_array($result))
  {
  $username = $row['username'];
  }

// unmask to change CHMOD of newly created sub directory to 777
$old_mask = umask(0);

//Create directory with member's username

$madedir = mkdir('../admin/uploads/'.$username.'', 0777) /*== TRUE ? 1 : 0*/;
umask($old_mask);


// Assign the directory the file is going to be uploaded to

$uploaddir = '../admin/uploads/'.$username.'';


// Get the file name

$file_name = $_FILES['uploaded_file']['name']; 


// Upload file to assigned directory with file name

$uploadfile = $uploaddir . basename($_FILES['uploaded_file']['name']);


// If the file has been uploaded, run this script

if (move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $uploadfile)) {

echo "Success";
} // end if
else {
echo "there was an error uploading your file"; 
}

So I'm having an issue. When someone signs up for an account on the site I'm building, they go to a setup page where they can upload a profile picture. I'm using mkdir to auto create a sub-folder with their username which is working fine, and CHMOD is 777 on that sub folder. The problem I'm having is that image is not being moved to that specified sub-folder, but it's going into the "uploads" folder which is it's parent directory.

Hopefully I haven't confused anyone, but I'm really needing help.

Below is my script I wrote to do this.

===============================================================================

require_once('../admin/includes/config.php');

$uploaded_file = $_REQUEST['uploaded_file'];
$member_id = $_REQUEST['member_id'];
$name = $_REQUEST['name'];
$location = $_REQUEST['location'];
$hobbies = $_REQUEST['hobbies'];
$hobby = $_REQUEST['hobby'];

// using member_id, extract the username from the members table so we can auto create a sub-directory for the images

$result = mysql_query("SELECT username FROM members WHERE member_id = '$member_id'");

while($row = mysql_fetch_array($result))
  {
  $username = $row['username'];
  }

// unmask to change CHMOD of newly created sub directory to 777
$old_mask = umask(0);

//Create directory with member's username

$madedir = mkdir('../admin/uploads/'.$username.'', 0777) /*== TRUE ? 1 : 0*/;
umask($old_mask);


// Assign the directory the file is going to be uploaded to

$uploaddir = '../admin/uploads/'.$username.'';


// Get the file name

$file_name = $_FILES['uploaded_file']['name']; 


// Upload file to assigned directory with file name

$uploadfile = $uploaddir . basename($_FILES['uploaded_file']['name']);


// If the file has been uploaded, run this script

if (move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $uploadfile)) {

echo "Success";
} // end if
else {
echo "there was an error uploading your file"; 
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痞味浪人 2024-09-15 23:20:19

您似乎在上传目录末尾缺少 / 。将此:更改

$uploaddir = '../admin/uploads/'.$username.'';

为:

$uploaddir = '../admin/uploads/'.$username.'/';

You seem to be missing a / at the end of the upload directory. Change this:

$uploaddir = '../admin/uploads/'.$username.'';

to this:

$uploaddir = '../admin/uploads/'.$username.'/';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文