以下脚本成功上传并将文件移至永久目录。
我正在父永久目录中创建一个具有配置文件ID名称的新文件夹,以便所有上传的文件都可以位于具有配置文件名称的文件夹中,并且在父文件夹中创建新文件夹
问题:移动文件后,它会移动到父文件夹,而不是新创建的文件夹。
PHP上传脚本。
<?php
define("UPLOAD_DIR", "images/$Pid");
if (!file_exists(UPLOAD_DIR)){
mkdir(UPLOAD_DIR, 0777, true);
}
if (!empty($_FILES["imageUpload"])){
$myFile = $_FILES["imageUpload"];
if ($myFile["error"] !== UPLOAD_ERR_OK) {
echo "<p>An error occurred.</p>";
exit;
}
$name = preg_replace("/[^A-Z0-9._-]/i", "_", $myFile["name"]);
$i = 0;
$parts = pathinfo($name);
while (file_exists(UPLOAD_DIR . $name)) {
$i++;
$name = $parts["filename"] . "-" . $i . "." . $parts["extension"];
}
$success = move_uploaded_file($myFile["tmp_name"],
UPLOAD_DIR . $name);
if (!$success) {
echo "<p>Unable to save file.</p>";
exit;
} else {
$imageDir = UPLOAD_DIR .'/'. $_FILES["imageUpload"]["name"];
echo $imageDir;
}
chmod(UPLOAD_DIR . $name, 0644);
}
?>
感谢。
答案 0 :(得分:2)
更改
$success = move_uploaded_file($myFile["tmp_name"],
UPLOAD_DIR . $name);
要
$success = move_uploaded_file($myFile["tmp_name"],
UPLOAD_DIR . '/'.$name);