相册:添加照片

时间:2019-11-23 04:12:51

标签: php html json ajax xml

好,所以我正在做一个照相馆项目,我正在尝试添加照片。我有一个页面,该页面允许用户浏览图片和另一个输入字段以为其图片添加标题。添加的图片路径和标题应添加到要从中提取图像的xml文件中。 另外,如果有帮助,我正在使用Materialize CSS。我不确定为什么它不起作用,但这是我目前拥有的:

表格

<form action="../models/addPicture.php" method="POST" enctype="multipart/form-data">
            <div class="row center">
                <h5>Choose a Photo to Upload</h5>
            </div>

            <div class="file-field input-field">
                <div class="btn-large black">
                    <span>Browse</span>
                    <input type="file" />
                </div>

                <div class="file-path-wrapper">
                    <input class="file-path validate" type="text" placeholder="Upload file" name="picFile" />
                </div>
           </div>

           <div class="row">
                <div class="input-field">
                    <input type="text" name="picTitle" class="validate" placeholder="Enter a title for your picture.">
                </div>
            </div>

            <div class="row center">
                <button class="btn-large black" type="submit">Add Pic!</button>
            </div>

方法:

<?php
    if (($_FILES['picFile']['name']!="")){

        $target_dir = "../images/";
        $file = $_FILES['picFile']['name'];
        $path = pathinfo($file);
        $filename = $path['basename'];
        $ext = $path['extension'];
        $temp_name = $_FILES['picFile']['temp_name'];
        $path_filename_ext = $target_dir.$filename.".".$ext;


        if (file_exists($path_filename_ext)) 
        {
            $message = "Added Picture Failed please try again.";
            $color = "red-text";
            header("Location: ../index.php?Message=".$message."&color=".$color);
        }
        else
        {
            move_uploaded_file($temp_name,$path_filename_ext);
            $message = "Added Picture Successfully. Please Click Reset to View.";
            $color = "green-text";
            header("Location: ../index.php?Message=".$message."&color=".$color);
        }
    }
    else
    {
        $message = "Please enter a picture to submit.";
        $color = "red-text";
        header("Location: ../index.php?Message=".$message."&color=".$color);

    }
?>

1 个答案:

答案 0 :(得分:0)

我在您的代码中发现了一个小错误。 9。

  • $ temp_name = $ _FILES ['picFile'] ['temp_name'];

['temp_name];更改为['tmp_name];

使用此代码。

$temp_name = $_FILES['picFile']['tmp_name'];