首页 > 解决方案 > 我对 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代码。

标签: phphtml

解决方案


目录格式必须如下: $target_dir = "Characters/Wraith/"; not
$target_dir = "/Characters/Wraith/";


推荐阅读