php - 我对 php 中的目录以及如何对它们进行字符串处理有疑问
问题描述
所以我正在写一个网站,这个网站的一个特点是允许用户上传特定角色的图片。我已经编写了一些 php 代码,以便从表单中获取值,然后决定将图像放入的目录。
我正在使用 Apache 2 运行 XAMPP,并且在运行代码时,之前输入的文件没有被放入目标文件夹中。并且正在显示一条警告消息“文件未上传”,该消息是硬编码的。
if($Character == "Bangalore"){
$target_dir = "/Characters/Bangalore/";
}
elseif($Character == "Blooudhound"){
$target_dir = "/Characters/Blooudhound/";
}
elseif($Character == "Caustic"){
$target_dir = "/Characters/Caustic/";
}
//(etc...)
elseif($Character == "Wraith"){
$target_dir = "/Characters/Wraith/";
}
else{
$target_dir = "/uploads/";
}
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
//(etc...)
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
//Report if this was moved or not
这是php代码
<select name="Character">
<option value="Bangalore">Bangalore</option>
<option value="Bloodhound">Bloodhound</option>
<option value="Caustic">Caustic</option>
<option value="Gibraltar">Gibraltar</option>
<option value="Lifeline">Lifeline</option>
<option value="Mirage">Mirage</option>
<option value="Octane">Octane</option>
<option value="Pathfinder">Pathfinder</option>
<option value="Wraith">Wraith</option>
</select>
这是html代码。
解决方案
目录格式必须如下:
$target_dir = "Characters/Wraith/";
not
$target_dir = "/Characters/Wraith/";
推荐阅读
- bash - find 何时报告路径中的双正斜杠?
- css - 在 style.css 中更改颜色不适用于 WP 主题
- java - 在运行时覆盖/禁用 Aspect
- c++-winrt - C++/winRT CreateFolderAsync - 如何检查现有文件夹?
- javascript - javascript从数组中的所有对象中提取某些属性
- java - 如何配置 spring 以使用外部 LDAP 服务器
- rest - 如何通过 REST API 更新 jenkins 凭据?
- algorithm - BST 转换为对称结构树
- arrays - 在数组中搜索以查看每个对象已使用了多少次
- c - 无法将节点附加到 C 中我的链表的末尾