如何在PHP5中上传文件时创建dir

时间:2016-03-02 12:08:25

标签: php

我想创建一个表单,我可以将文件上传到目录。如果它不存在我将创建它。我想选择自己应该放在哪里。

<?php
$root = $_SERVER["DOCUMENT_ROOT"];
$url_link = "/gpsupload/tour/";
$dist = $root . $url_link;
if(!is_dir($dist)) {
echo"Create dir!";
exit;
}
if(!is_writeable($dist)) {
echo "Tjek writable ". $dist ." !!!!!<br />";
exit;
}
?>
<body>
<form enctype="multipart/form-data" action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">
Select file:<br>
<input name="upfile" type="file"><br>
File upload to directory: <?php echo $dist; ?>
<br />
Or write name to new dir: <input type="text" name="newdir">
<br />
<input type="submit" value="Upload">
</form>
<?php
if (isset($_FILES["upfile"])) {

if(!empty($_POST["newdir"])) {

mkdir($dist . $_POST["newdir"], 0777);
chmod($dist . $_POST["newdir"], 0777);

$dist = $dist . $_POST["newdir"] ."/";
$url_link .= $_POST["newdir"] ."/"; 
}
 if(move_uploaded_file($_FILES["upfile"]["tmp_name"], $dist . $_FILES["upfile"]["name"])) {
 echo"File, ". $_FILES["upfile"]["name"] ." is uploadet. <a href=\"". $url_link . $_FILES["upfile"]["name"] ."\">". $_FILES["upfile"]["name"] ."</a>";
 }else{
  echo"File is not uploadet?";
 }
}
?> 
</body>

如何选择我想要创建目录的位置?

1 个答案:

答案 0 :(得分:1)

您可以使用mkdir(http://php.net/manual/en/function.mkdir.php)在php中创建目录。