weixin_33717298 2019-11-23 04:12 采纳率: 0%
浏览 35

相册:添加照片

Ok so I’m working on a photo gallery project and I’m trying to add a photo. I have a page that allows the user to browse for a picture and another input field for adding a title for their picture. The added picture path and title should be added to a xml file where the images are being pulled from. Also if it helps, I'm using Materialize css. I’m not sure why it’s not working but this is what I currently have:

Form

<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>

Method:

<?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条回答 默认 最新

  • weixin_33699914 2019-11-23 08:04
    关注

    I have found a small mistake in your code at line no. 9.

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

    Change the ['temp_name]; to ['tmp_name];

    Use this code.

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

报告相同问题?