2015-05-15 102 views
-1

我试图上传文件使用窗体并将它们放入与登录的用户名对应的文件夹中。我如何使它上传该文件放入尚未创建的文件夹中。在这种情况下,该文件夹应该根据用户名$ user。上传文件里面还不存在的文件夹在php

下面是我的代码:

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) 
+0

dbptft.php有上传文件的权限吗? – Zerquix18

+0

@ Zerquix18每当我改变它的权限,它会给我错误。它不应该是权限应该改变的文件夹吗? –

回答

1

您的电话是/uploads而不是/home/aukwizcq/public_html/ptft/uploads。删除前缀/否则,您指的是服务器的根目录/作为绝对路径,而不是上传目录的相对路径。

我还会在mkdir()之前加上if (!is_dir($directorypath)),除非目录总是唯一的,以避免目录已经存在的通知。

+0

上传已存在。 –

+0

Yeap,只是看到了错误。一秒钟,更新... – Devon

+0

@KyleJosh,问题解决了吗? – Devon

相关问题