我正在使用mkdir
函数创建和chmod目录$dirX
$dirY
。
以下代码块仅创建$dirY
并在那里上传所需的文件。这里出了什么问题?为什么没有与上传的文件一起创建其他目录?
$dirA = 'mydir1/';
$dirB = '../mydir2/';
$directory = array('$dirA','$dirB');
foreach ($directory as $dir);
if (!is_dir($dir)){
mkdir($dir, 0777)
};
for($f=0; $f<count($_FILES['newsimage_upload']['name']); $f++) {
$nume_f = $_FILES['newsimage_upload']['name'][$f];
$thefile = $dir . '/'. $nume_f; //It doesn't set the directories of array's strings
if (!move_uploaded_file ($_FILES['newsimage_upload']['tmp_name'][$f], $thefile)) {
$uploadresult[$f] = 'The file '. $nume_f. 'could not be copied, try again';
}
答案 0 :(得分:1)
这是问题所在:
foreach ($directory as $dir);
制作
foreach ($directory as $dir) {
...
}
你应该没事。
答案 1 :(得分:1)
您的代码应类似于:
$dirA = 'mydir1/';
$dirB = '../mydir2/';
$directory = array('$dirA','$dirB');
foreach ($directory as $dir){
if (!is_dir($dir)) mkdir($dir, 0777);
for($f=0; $f<count($_FILES['newsimage_upload']['name']); $f++) {
$nume_f = $_FILES['newsimage_upload']['name'][$f];
$thefile = $dir . '/'. $nume_f; //It doesn't set the directories of array's strings
if (!move_uploaded_file ($_FILES['newsimage_upload']['tmp_name'][$f], $thefile)) {
$uploadresult[$f] = 'The file '. $nume_f. 'could not be copied, try again';
}
//some more code
} //closing the for
//some more code
} //closing the foreach
请注意,在原始代码示例中,for循环缺少结束大括号,因此我假设您在最终的foreach结束括号之前在原始代码中将其关闭。
答案 2 :(得分:1)
PHP的mkdir
函数已经完成了此功能。只需将recursive
选项指定为true
。
您还需要使用realpath
来解析带有点的路径。
此外,正如另一位回答者所述 - 您需要在初始foreach
的内部代码块周围使用括号。
$dirA = 'mydir1/';
$dirB = '../mydir2/';
$directory = array('$dirA','$dirB');
foreach ($directory as $dir){
// Note the next two lines which I have modified:
$realPath = realpath($dir);
if (!is_dir($realPath)) mkdir($realPath, 0777, $recursive=true);
for($f=0; $f<count($_FILES['newsimage_upload']['name']); $f++) {
$nume_f = $_FILES['newsimage_upload']['name'][$f];
$thefile = $dir . '/'. $nume_f; //It doesn't set the directories of array's strings
if (!move_uploaded_file ($_FILES['newsimage_upload']['tmp_name'][$f], $thefile)) {
$uploadresult[$f] = 'The file '. $nume_f. 'could not be copied, try again';
}
//some more code
} //closing the for
//some more code
} //closing the foreach
答案 3 :(得分:0)
应检查来自mkdir()的返回码以确保实际创建了目录。 umask()的值可能会阻止创建的目录具有所需的权限, 所以umask()应保存/设置为适当的值,然后在创建目录后恢复。
该行可能存在问题: $ directory = array('$ dirA','$ dirB'); 单引号不会将变量扩展到其内容中。 我建议在那一行使用双引号。