请帮助,我已经到处检查找到答案,有些已经很接近,但从来都不是我需要的。我试图让用户上传文件并将其放在名为id#
的目录中。 $spid = users id
并在此页面的其他位置正常工作。让我们说$spid = 100
,我希望将图片上传到uploads/100/some.jpg
。然而,无论我做什么,他们最终都会进入uploads/
这是我的代码:世界上我做错了什么?
uploadform.html -
<form action="uploads2.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload"><p>
<input type="submit" value="Upload Image" name="submit">
</form>
uploads2.php
$targetdir= "uploads/$spid";
if(!is_dir($targetdir)){
mkdir("uploads/".$spid, 0777);
}
$targetfile = $targetdir."/".basename($_FILES['file']['name']);
$allexts = array("jpg", "jpeg", "gif", "png");
$ext = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 2000000)
&& in_array($ext, $allexts)){
if (move_uploaded_file($_FILES['file']['tmp_name'], $targetfile)) {
print_r ($targetfile);
echo "File upload was successful.";
} else {
echo "An error has occured. Please try again.";
}
} else {
echo "Invalid file type. Please try again.";
}
答案 0 :(得分:0)
那是因为如果您上传some.jpg
,$targetfile
将是uploads/100some.jpg
而不是uploads/100/some.jpg
。
更改
$targetfile = $targetdir.basename($_FILES['file']['name']);
到
$targetfile = $targetdir.'/'.basename($_FILES['file']['name']);