我已经为图像制作了一个 php 上传表单。它使用会话变量来确定上传目录。上传有两个选项,滑块目录或侧面目录,所以我有一个确定目录的 if 语句。如果我从表单中删除它,那么整个事情都可以正常工作,但是使用它,$_FILES 似乎没有被声明并作为未定义的索引错误返回。
可以找到代码如下:
上传.php
<?php
include("resize-class.php");
$allowedExt = array('jpg', 'jpeg', 'JPG', 'JPEG');
$tmps = explode(".", $_FILES['file']['name']);
$extension = end($tmps);
session_start();
if ($_POST['dir'] == 'side'){
$dirent = $_SESSION['sideDir'];
}
else if($_POST['dir'] == 'slider'){
$dirent = $_SESSION['sliderDIR'];
}
else{
die();
}
echo $_POST['dir'];
print_r($_FILES);
if (($_FILES["file"]["type"] == "image/jpeg")&& ($_FILES["file"]["size"] < 4000000000)&& in_array($extension, $allowedExt)) {
if ($_FILES["file"]["error"] > 0) {
echo "Error: " . $_FILES["file"]["error"] . "<br />";
echo 'here';
} else {
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . " <br />\n";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br /> \n";
echo "Stored in: " . $_FILES["file"]["tmp_name"]. "<br />\n";
}
if (file_exists($dirent. $_FILES["file"]["name"])) {
echo $_FILES["file"]["name"] . "already exists";
} else {
$fName = $_FILES["file"]["name"];
$tmpname = $_FILES["file"]["tmp_name"];
move_uploaded_file($_FILES["file"]["tmp_name"], $dirent . $_FILES["file"]["name"]);
$number = FileCounter($dirent);
echo "Number of images in DIR: " . $number. " <br />\n ";
$number +1;
$resizeObj = new resize($dirent.$fName);
$resizeObj -> resizeImage(250, 150, 'crop');
$resizeObj -> saveImage($dirent.$number.".jpg", 100);
unlink ($dirent.$_FILES["file"]["name"]);
}
} else {
echo "Error: " . $_FILES["file"]["error"] . "<br />";
}
function FileCounter($dir) {
$counter = 0;
$iterator = new DirectoryIterator(dirname($dir));
foreach ($iterator as $fileinfo) {
if ($fileinfo->isFile()) {
if ($fileinfo->getExtension() == "jpg") {
$counter++;
echo $counter . "\n";
}
}
}
return $counter;
}
?>
HTML表格:
<form action="includes/upload.php" method="post">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<div class="styled-select">
<label for="dir"> Upload to:</label>
<select size="2" name="dir" multiple="yes" id="dir">
<option value="side" >Side Images</option>
<option value="slider" >Slider Images</option>
</select>
</div>
<br />
<input type="submit" name="submit" value="Submit" />
</form>
我猜我的代码中有一个非常愚蠢的错误,我已经盯着它看了一个小时,或者我不知道关于 $_FILES 和 $_POST 的一些东西。(或者我可能像白痴一样对表格进行了编码!)。