将文件上传到php中尚未存在的文件夹中

时间:2015-05-15 23:04:33

标签: php html

我尝试使用表单上传文件并将其放入与登录用户名对应的文件夹中。如何将其设置为将文件上传到不具有该文件的文件夹中已经创建了。该文件夹应该是根据用户名,在这种情况下是$ user。

以下是我的代码:

dbptft.php

$user = $_SESSION['username'];
if (login_check($mysqli) == true) : ?>
            <p>Welcome <?php echo htmlentities($user); ?>!</p>
      <?php      
      $directorypath = "/uploads/". $user;
       mkdir($directorypath, 0644);

$target_path = "$directorypath/";

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

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
    " has been uploaded";
} else{
    echo "There was an error uploading the file, please try again!";
}

这是我的表格

<form method="post" enctype="multipart/form-data" action="dbptft.php">
    <input type="file" name="uploadedfile">
<input type="submit" class="submit" id="submit" value="Submit" />

错误:

    Welcome S00024972!


Warning: mkdir(): No such file or directory in /home/aukwizcq/public_html/ptft/dbptft.php on line 19

Warning: move_uploaded_file(/uploads/S00024972/Capture.PNG): failed to open stream: No such file or directory in /home/aukwizcq/public_html/ptft/dbptft.php on line 25

Warning: move_uploaded_file(): Unable to move '/tmp/phpubGytY' to '/uploads/S00024972/Capture.PNG' in /home/aukwizcq/public_html/ptft/dbptft.php on line 25
There was an error uploading the file, please try again!Error : (0)

1 个答案:

答案 0 :(得分:1)

您的来电是/home/aukwizcq/public_html/ptft/uploads而不是/。删除前缀/,否则您将服务器的根目录if (!is_dir($directorypath))称为绝对路径,而不是上传目录的相对路径。

我还要在mkdir()之前添加Error:Attribute "radius" has already been defined ,除非该目录始终是唯一的,以避免目录已存在通知。