php多文件上传路径错误

时间:2015-03-23 08:51:28

标签: php

$tl = $_POST['title'];
$im = 'imag' . $id;

$query8 = "INSERT INTO media (fldr,title) VALUES ('{$im}','{$tl}')";
$result8 = mysql_query($query8, $connection);

mkdir("uploads/$im");
$path = "uploads/$im";

$valid_formats = array("jpg", "png", "gif", "zip", "bmp");
$max_file_size = 1024 * 100; //100 kb

// Upload directory
echo $path;
$count = 0;

// Loop $_FILES to execute all files
foreach ($_FILES['files']['name'] as $f = > $name) {
    if ($_FILES['files']['error'][$f] == 4) {
        continue; // Skip file if any error found
    }
    if ($_FILES['files']['error'][$f] == 0) {
        if ($_FILES['files']['size'][$f] > $max_file_size) {
            $message[] = "$name is too large!.";
            continue; // Skip large files
        } elseif (!in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats)) {
            $message[] = "$name is not a valid format";
            continue; // Skip invalid file formats
        } else {
            // No error found! Move uploaded files 
            if (move_uploaded_file($_FILES["files"]["tmp_name"][$f], $path . $name)) {
                $count++; // Number of successfully uploaded files
            }
        }
    }
}
  

图像未在指定路径中上传。图像上传到父目录(上传)。我需要上传到创建的目录。请帮帮我..

2 个答案:

答案 0 :(得分:0)

更改以下代码。您必须在$ path和$ name

之间放置路径分隔符
if (move_uploaded_file($_FILES["files"]["tmp_name"][$f], $path . "/" . $name)) {
        $count++; // Number of successfully uploaded files
   }

答案 1 :(得分:0)

您需要使用directory separator分隔路径和文件名,如下所示:

if (move_uploaded_file(
    $_FILES["files"]["tmp_name"][$f], $path . DIRECTORY_SEPARATOR . $name
)) {
    ...
}