首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP文件上传什么都不做

PHP文件上传什么都不做
EN

Stack Overflow用户
提问于 2013-09-19 12:01:50
回答 3查看 808关注 0票数 0

我以前有过几个上传表单,但是,即使在几乎复制了以前的代码之后,这似乎也不起作用,我更喜欢在一个php脚本文件中完成所有工作,所以所有这些都是在这个文件中生成的。

我的表格:

代码语言:javascript
复制
<form action="" method="post" enctype="multipart/form-data">
    <ul>
        <li>
            <label for="file">File : </label>
            <input type="file" id="file" name="file" required="required" />
        </li>
        <li>
            <input type="submit" value="Upload" />
        </li>
    </ul>
</form>

我的php上传:

代码语言:javascript
复制
if(!empty($_POST['file']))
{
    echo "Found.";
    $exts = array("gif", "jpeg", "jpg", "png");
    $temp = explode(".", $_FILES["file"]["name"]);
    $ext = end($temp);
    if((($_FILES["file"]["type"] == "image/gif")
    || ($_FILES["file"]["type"] == "image/jpeg")
    || ($_FILES["file"]["type"] == "image/jpg")
    || ($_FILES["file"]["type"] == "image/pjpeg")
    || ($_FILES["file"]["type"] == "image/x-png")
    || ($_FILES["file"]["type"] == "image/png"))
    && ($_FILES["file"]["size"] < 20000)
    && in_array($ext, $exts))
    {
        if($_FILES["file"]["error"] > 0)
        {
            $result = "Error Code: " . $_FILES["file"]["error"] . "<br />";
        }
        else
        {
            $scandir = scandir("/images/news/");
            $newname = (count($scandir-2)) . $ext;
            move_uploaded_file($_FILES["file"]["tmp_name"],"/images/news/" . $newname);
            $ulink = "/images/news/" . $newname;
            $result = "Success, please copy your link below";
        }
    }
    else
    {
        $result = "Error.";
    }
}

当我上传一个.png图像时,页面看起来很刷新,我在其中放置了echo "Found.";,以检查它在$_POST["file"]中是否有任何内容,但它似乎没有任何内容。

我不明白为什么页面没有正确提交。我已经将action=""更改为action="upload.php",以确保它指向相同的页面,但仍然没有。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-09-19 12:03:13

使用$_FILES['file']而不是$_POST['file']

http://www.php.net/manual/en/features.file-upload.post-method.php上阅读更多关于http://www.php.net/manual/en/features.file-upload.post-method.php的信息

票数 6
EN

Stack Overflow用户

发布于 2013-09-19 12:04:43

$_POST['file']替换为$_FILES['file']并设置action=""

票数 1
EN

Stack Overflow用户

发布于 2013-09-19 12:09:44

试试这个..。因为$_POST不适用于文件,所以对于文件我们使用$_FILES。

代码语言:javascript
复制
if(!empty($_FILES['file']))
{
    echo "Found.";
    $exts = array("gif", "jpeg", "jpg", "png");
    $temp = explode(".", $_FILES["file"]["name"]);
    $ext = end($temp);
    if((($_FILES["file"]["type"] == "image/gif")
    || ($_FILES["file"]["type"] == "image/jpeg")
    || ($_FILES["file"]["type"] == "image/jpg")
    || ($_FILES["file"]["type"] == "image/pjpeg")
    || ($_FILES["file"]["type"] == "image/x-png")
    || ($_FILES["file"]["type"] == "image/png"))
    && ($_FILES["file"]["size"] < 20000)
    && in_array($ext, $exts))
    {
        if($_FILES["file"]["error"] > 0)
        {
            $result = "Error Code: " . $_FILES["file"]["error"] . "<br />";
        }
        else
        {
            $scandir = scandir("/images/news/");
            $newname = (count($scandir-2)) . $ext;
            move_uploaded_file($_FILES["file"]["tmp_name"],"/images/news/" . $newname);
            $ulink = "/images/news/" . $newname;
            $result = "Success, please copy your link below";
        }
    }
    else
    {
        $result = "Error.";
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18894086

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档