图像保存到dic并用PHP显示它们

时间:2015-02-09 03:17:25

标签: php html image

<body>
<?php 
if (isset($_POST["submit_photo"])) 
{
    processform();
} else {
    displayform();
}

function processform() 
{
    if (isset($_FILES["photo"]) and $_FILES["photo"]["error"] == UPLOAD_ERR_OK) 
    {
        if ($_FILES["photo"]["type"] != "image/png") 
        {
            echo " png image please, thank you";

        }
        else if (!move_uploaded_file($_FILES["photo"]["temp_name"], "photos/".basename($_FILES["photo"]["name"]))) 
        {
        echo "sorry there was a problem uploading photots".$_FILES["photo"]["error"];

    } else {
        displaythanks();
    }

} else {
    switch ($_FILES["photo"]["error"]) 
    {
        case UPLOAD_ERR_INI_SIZE: 
            $message =  "The photo is larger than the server allows";
            break;

        case UPLOAD_ERR_FORM_SIZE:
            $message = "The photo is larger than the script allows";
            break;
        case UPLOAD_ERR_NO_FILE:
            $message= "No file selected to be uploaded";
            break;

        default:
            $message= "please contact your administrator for help";
            break;
        }
        echo "Sorry there was a problem with uploading photos. ".$message;  
    }
}

function displayform()
{?>
    <h1> Uploading a photo </h1>
    <form style="width:30em" action="photo_upload.php" method="post" enctype="multipart/form-data">               
    <label for="visitor_name">Your Name :</label>
    <input type="text" name="visitor_name" id="visitor_name" value="" /><br />
    <label for="photo">Your Photo:</label>
    <input type="file" name="photo" id="photo" value="" /><br />                
    <input type="submit" name="submit_photo" id="submit_photo" value="Submit" />
    </form>
<?php       
}
function displaythanks() 
{
    echo "thanks for uploading your photo ". $_POST["visitor_name"];
    echo "here is your photo";
?>
    <p><img scr="photos/<?php echo $_FILES["photo"]["name"] ?>" alt="photo"/></p>
<?php
}
?>
</body> 

我一直在尝试使用该表单获取png图像并将其保存到位于同一目录(根文件夹)中的照片文件夹并显示它。发生的事情是,当我点击提交按钮时,它不会将其保存到那些照片/文件夹中。我一直在浏览寻找解决方案,但大多数人写的方式几乎和我一样。但有些原因它不起作用。难道我做错了什么?如果可以,请指出。而且我想要任何解决方案。多谢你们。

1 个答案:

答案 0 :(得分:0)

你的代码有两个问题,但在解释它们之前,请为你的代码提供一个很好的样式,这样它就是可读的

问题1.你拼写错误$_FILES["photo"]["temp_name"]
,它应该是$_FILES["photo"]["tmp_name"]

问题2.在php中,当你首先尝试上传文件时,它会将文件上传到模板文件夹中,然后让程序员决定是否上传它,这是你的规则,例如max upload size,format ..最后,如果您决定上传文件,则必须使用move_uploaded_file()函数,但在您的代码中,此行位于else if部分,因此如果图像类型为png,则您的代码将永远不会到达该行。这是更正后的代码:

<body>
<?php
if (isset($_POST["submit_photo"]))
{
    processform();
}
else
{
    displayform();
}

function processform()
{
    if (isset($_FILES["photo"]) and $_FILES["photo"]["error"] == UPLOAD_ERR_OK)
    {
        if ($_FILES["photo"]["type"] != "image/png")
        {
            echo " png image please, thank you";
        }
        if (!move_uploaded_file($_FILES["photo"]["tmp_name"], "photos/" .  basename($_FILES["photo"]["name"])))
        {
            echo "sorry there was a problem uploading photots".$_FILES["photo"]["error"];
        }
        else
        {
            displaythanks();

        }

    }
    else
    {
        switch ($_FILES["photo"]["error"])
        {
            case UPLOAD_ERR_INI_SIZE:
                $message =  "The photo is larger than the server allows";
                break;

            case UPLOAD_ERR_FORM_SIZE:
                $message = "The photo is larger than the script allows";
                break;
            case UPLOAD_ERR_NO_FILE:
                $message= "No file selected to be uploaded";
                break;

            default:
                $message= "please contact your administrator for help";
                break;
        }
        echo "Sorry there was a problem with uploading photos. ".$message;
    }
}

function displayform()
{?>
    <h1> Uploading a photo </h1>
    <form style="width:30em" action="photo_upload.php" method="post" enctype="multipart/form-data">
        <label for="visitor_name">Your Name :</label>
        <input type="text" name="visitor_name" id="visitor_name" value="" /><br />
        <label for="photo">Your Photo:</label>
        <input type="file" name="photo" id="photo" value="" /><br />
        <input type="submit" name="submit_photo" id="submit_photo" value="Submit" />
    </form>
<?php
}
function displaythanks()
{
    echo "thanks for uploading your photo ". $_POST["visitor_name"];
    echo "here is your photo";
    ?>
    <p><img scr="photos/<?php echo $_FILES["photo"]["name"] ?>" alt="photo"/></p>
<?php
}
?>
</body>