php: create directory on form submit?

社会主义新天地 提交于 2019-12-03 17:02:29
thecodeassassin

It might be a good idea to make sure that the directory you are handling is indeed a directory. This code works... edit as you please.

define("PATH", "/home/born05/htdocs/swish_s/Swish");

$test = "set";
$_POST["dirname"] = "test";


if (isset($test)) {
  //get value of inputfield
  $dir = $_POST['dirname'];
  //set the target path ??

$targetfilename = PATH . '/' . $dir;

if (!is_file($dir) && !is_dir($dir)) {
    mkdir($dir); //create the directory
    chmod($targetfilename, 0777); //make it writable
}
else
{
    echo "{$dir} exists and is a valid dir";
}

Good luck!

Edited: comment was a good hint ;)

You have to use

!is_dir($dir)

instead of

!file_exists($dir)

it's not a file, it's a directory!

Good luck!

You can use is_dir().

@codeworxx file_exists can be used to check a directory as well..

http://www.php.net/manual/en/function.file-exists.php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!