HTML / PHP文件上传 [英] HTML/PHP File Upload

查看:129
本文介绍了HTML / PHP文件上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我希望用户只能上传文档或docx的。所以首先这里是我的HTML:

 < form action =upload_file.phpmethod =postenctype =multipart /表单数据> 
选择一个档案:< input type =filename =img>
< input type =submit>
< / form>

这里是我的php:

  $ allowedExts = array(doc,docx); 
$ extension = end(explode(。,$ _FILES [file] [name]));

if($ extension!=。doc|| $ extension!=。doc
&&($ _FILES [file] [size]< ; 200000)
&& in_array($ extension,$ allowedExts)){
if($ _FILES [file] [error]> 0)
{
回显返回代码:。 $ _FILES [file] [error]。 < br />;
}
else
{
echoUpload:。 $ _FILES [file] [name]。 < br />;
回显类型:。 $ _FILES [file] [type]。 < br />;
回声大小:。 ($ _FILES [file] [size] / 1024)。 Kb< br />;
回显临时文件:。 $ _FILES [file] [tmp_name]。 < br />;
$ b $ if(file_exists(Proposals /。$ _FILES [file] [name]))
{
echo $ _FILES [file] [名称] 。 已经存在。 ;


$ $ $ b $ move_uploaded_file($ _ FILES [file] [tmp_name],
Proposals /。$ _FILES [file] [名称]);
回声存储在:。 建议/。 $ _FILES [ 文件] [ 名称];


} else {
echoInvalid file;
}

每次我尝试上传文件时,无论是文档还是png输出:

pre $

类型
大小0 Kb
Temp文件:
已经存在。

上传到Proposals文件夹没有任何结果。所以我有两个问题:

1)我的代码有什么问题?

2)它重定向到upload_file.php并显示一条消息。有没有一种方法可以实际返回主页面,并显示它成功的文本?

解决方案

第一个问题已经2)重定向到upload_file.php,并显示一条消息。有没有一种方法可以实际返回主页面,并显示它成功的文本?


通常情况下最好显示成功消息在重定向的页面上,但你可以解决这两个方面:
$ b


  • 存储以前的URL(或知道它)并重定向到 header(Location:old_page.php);

  • 将表单的目标设为iframe。这意味着主页本身不会重定向,您可以从upload_file.php(它将加载到iframe中)发出响应,使您的应用程序无缝上传。 b

    这也不是获得文件扩展名的最好方式:

      $ extension = end explode(。,$ _FILES [file] [name])); 

    请尝试使用pathinfo: http://php.net/manual/en/function.pathinfo.php



    <$ p $ ($ _FILES [file] [name],PATHINFO_EXTENSION);

    if 语句:

      if($ extension!=。doc|| $ extension!=。doc
    &&( $ _FILES [file] [size]< 200000)
    & amp; in_array($ extension,$ allowedExts)){
    in_array 的一个小用法,我仍然会建议您把它拿出来,并使用更实用的搜索方法数组: http://php.net/manual/en/function.array-search .php



    所以你的如果会变成这样:


    ($ _FILES [file] [size]< 200000)&& array_search($ extension,$ allowedExts)!== b
    pre $ 假){


    So i want the user to only be able to upload docs or docx's. So first here's my html:

    <form action="upload_file.php" method="post" enctype="multipart/form-data">
        Select a file: <input type="file" name="img">
        <input type="submit">
    </form>
    

    And here's my php:

    $allowedExts = array("doc", "docx");
        $extension = end(explode(".", $_FILES["file"]["name"]));
    
        if ($extension!=".doc" || $extension!=".doc"
        && ($_FILES["file"]["size"] < 200000)
        && in_array($extension, $allowedExts)) {
            if ($_FILES["file"]["error"] > 0)
            {
                echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
            }
            else
            {
                echo "Upload: " . $_FILES["file"]["name"] . "<br />";
                echo "Type: " . $_FILES["file"]["type"] . "<br />";
                echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
                echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
    
                if (file_exists("Proposals/" . $_FILES["file"]["name"]))
                {
                    echo $_FILES["file"]["name"] . " already exists. ";
                }
                else
                {
                    move_uploaded_file($_FILES["file"]["tmp_name"],
                    "Proposals/" . $_FILES["file"]["name"]);
                    echo "Stored in: " . "Proposals/" . $_FILES["file"]["name"];
                }
            }
        } else {
            echo "Invalid file";
        }
    

    Everytime I try to upload a file, whether it's a doc or a png it outputs this:

    Upload: 
    Type: 
    Size: 0 Kb
    Temp file: 
    already exists.
    

    And nothing ends up getting uploaded to the Proposals folder. So I have 2 questions:
    1) what is the problem with my code?
    2) it redirects to upload_file.php and displays a message. Is there a way to actually go back the main page and display text that it was successful?

    解决方案

    The first question has already been answered so I won't answer it again.

    2) it redirects to upload_file.php and displays a message. Is there a way to actually go back the main page and display text that it was successful?

    It is normally better to show the success message on the redirected page but you can solve this two ways:

    • Store the previous URL (or know it) and redirect to it with header("Location: old_page.php");
    • Make the target of the form an iframe. This means the main page itself will not redirect and you can just bubble a response from upload_file.php (which will load in the iframe) making seamless uploading for your app.

    Also this is not the best way to get a file extension:

    $extension = end(explode(".", $_FILES["file"]["name"]));
    

    Try using pathinfo instead: http://php.net/manual/en/function.pathinfo.php

    $extension = pathinfo( $_FILES["file"]["name"], PATHINFO_EXTENSION);
    

    And your if statement:

    if ($extension!=".doc" || $extension!=".doc"
        && ($_FILES["file"]["size"] < 200000)
        && in_array($extension, $allowedExts)) {
    

    Even though this is a tiny usage of in_array I still would recommend taking that out and using a more practical method of searching arrays: http://php.net/manual/en/function.array-search.php

    So your if would become something like:

    if (($_FILES["file"]["size"] < 200000) && array_search($extension, $allowedExts)!==false) {
    

    这篇关于HTML / PHP文件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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