$ _FILES undefined PHP上传表单。无法弄清楚 [英] $_FILES undefined PHP upload form. Can't figure out

查看:155
本文介绍了$ _FILES undefined PHP上传表单。无法弄清楚的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:


我为图像做了一个php上传表单。它使用会话变量来确定上传目录。有两个选项,上传,滑块目录或侧目录,所以我有一个if语句,确定目录。如果我从表单中删除这个,那么整个事情就可以正常工作,但是$ _FILES似乎没有被声明,并且返回一个未定义的索引错误。



代码可以发现如下:

upload.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); $($ _FILES [file] [type] ==image / jpeg)&&($ _FILES [file] [size]< ($ _FILES [file] [error]> 0){
echoError:。 $ _FILES [file] [error]。 < br />;
echo'here';
} else {
echoUpload:。 $ _FILES [file] [name]。 < br />;
回显类型:。 $ _FILES [file] [type]。 < br /> \\\
;
回声大小:。 ($ _FILES [file] [size] / 1024)。 Kb> \\\
;
回声存储在:。 $ _FILES [ 文件] [ tmp_name的值。 < br /> \\\
; (file_exists($ dirent。$ _FILES [file] [name])){
echo $ _FILES [file] [name


]。 已经存在;
} else {
$ fName = $ _FILES [file] [name];
$ tmpname = $ _FILES [file] [tmp_name];
move_uploaded_file($ _ FILES [file] [tmp_name],$ dirent。$ _FILES [file] [name]);
$ number = FileCounter($ dirent);
回显DIR中的图像数目:。 $号。 < br /> \\\
;
$ number +1;
$ resizeObj = new resize($ dirent。$ fName);
$ resizeObj - > resizeImage(250,150,'crop');
$ resizeObj - > saveImage($ dirent。$ number。。jpg,100);
unlink($ dirent。$ _ FILES [file] [name]);
}
} else {
echoError:。 $ _FILES [file] [error]。 < br />;
}
函数FileCounter($ dir){
$ counter = 0;
$ iterator = new DirectoryIterator(dirname($ dir));
foreach($ iterator as $ fileinfo){
if($ fileinfo-> isFile()){
if($ fileinfo-> getExtension()==jpg){
$ counter ++;
echo $计数器。 \\\
;
}
}
}
return $ counter;
}
?>

HTML FORM:

 < form action =includes / upload.phpmethod =post> 
< label for =file>文件名:< / label>
< input type =filename =fileid =file/>
< div class =style-select>
< label for =dir>上传到:< / label>
< select size =2name =dirmultiple =yesid =dir>
< option value =side> Side Images< / option>
< option value =slider> Slider Images< / option>
< / select>
< / div>
< br />
< input type =submitname =submitvalue =Submit/>
< / form>

我猜测我的代码中有一个非常愚蠢的错误,一直盯着它一个小时,或者有一些我不知道$ _FILES和$ _POST。 (或可能我已经编码形式像一个白痴!)。

解决方案

您的表单丢失

  enctype =multipart / form-data


Possible Duplicate:
Getting ‘undefined index’ error while trying to use $_FILE in PHP

I have made a php upload form for images. It uses session variables to determine the upload directory. There are two options for the uploads, the slider dir or the side dir so I have an if statement that determines the directory. If i remove this from the form then the whole thing works ok however with it in, $_FILES seems to not be declared and returns as an undefined index error.

The code can be found is as follows:

upload.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:

<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>

I am guessing that there is either a really stupid error in my code that I am overlooking as I have been staring at it for an hour now or that there is something that I do not know about $_FILES and $_POST. (or possibly I have coded the form like a moron!).

解决方案

Your form is missing

enctype="multipart/form-data"

这篇关于$ _FILES undefined PHP上传表单。无法弄清楚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆