dpa84373 2013-04-18 18:28
浏览 16
已采纳

试图创建一个简单的图像上传器

I'm trying to create an image uploader for my web application. I've tried searching online for solutions on how to create one, but haven't had much luck finding anything of real help. I'm also not sure what words I should be using to search for the right results. I did find something on the W3Schools website, (http://www.w3schools.com/php/php_file_upload.asp), but I still can't get it to upload a file to my server. Not certain of what the issue is since i followed the instructions on the website to set it up. I also found this (JavaScript: Upload file), but it's not quite exactly what I want it to do. I still need the php script since the image is not the only thing i'm going to be sending to the server. I will also be sending other information to be stored into my database on the server, such as image name and other information associated with the image.

The php script that I'm using, which i got off the W3S website, is down below. Where I am stuck at is that once I hit the submit button my page crashes or I should say I get a Server Error back, but with no information as to what caused it. Any help would be greatly appreciated

<?php
$allowedExts = array("gif", "jpeg", "jpg", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if (($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjepg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 20000)
&& in_array($extension, $allowedExts)) {
    if ($_FILES["file"]["error"] > 0) {
        echo "Error: " . $_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 "Stored in: " . $_FILES["file"]["tmp_name"];

        if (file_exists("public_hmtl/uploads/" . $_FILES["file"]["name"])) {
            echo $_FILES["file"]["name"] . " already exists. ";
        }
        else {
            move_uploaded_file($_FILES["file"]["tmp_name"], "public_hmtl/uploads/" . $_FILES["file"]["name"]);
            echo "Stored in: " . "public_hmtl/uploads/" . $_FILES["file"]["name"];
        }
    }
}
else {
    echo "Invalid file";
}
?>

Here's also the html for the form.

    <!DOCTYPE html>
    <html>
    <head>
        <title>Location details</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    </head>
    <body>
        <div>
            <form enctype="multipart/form-data" method="post" action="scripts/snap.php">
        <!--<p>
            <input id="plat" class="plat" name="plat" type="hidden" value="" />
            <input id="plon" class="plon" name="plon" type="hidden" value=""  />
        </p>-->
        <div class="row">
            <label for="file">Select an image to Upload</label><br />
            <input type="file" name="file" id="file" />
        </div>
        <div id="filename"></div>
        <div id="fileSize"></div>    
        <div id="fileType"></div>
        <div class="row">
            <input type="submit" name="submit" value="Submit" />
            <!--<input type="button" onclick="uploadFile()" value="Upload" />-->
        </div>
        <div id="progressNumber"></div>
    </form>
</div>
  • 写回答

2条回答 默认 最新

  • dslfq06464 2013-04-18 18:52
    关注

    In if statement, there is mismatch open and closed tag, correct it

    if ( ( ($_FILES["file"]["type"] == "image/gif")
    || ($_FILES["file"]["type"] == "image/jpeg")
    || ($_FILES["file"]["type"] == "image/jpg")
    || ($_FILES["file"]["type"] == "image/pjepg")
    || ($_FILES["file"]["type"] == "image/x-png")
    || ($_FILES["file"]["type"] == "image/png")
    )
    && ($_FILES["file"]["size"] < 20000)
    && in_array($extension, $allowedExts) )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表