-2

I've an upload input field in a form. the problem that the name of the file is being inserted in the database but the file is not being uploaded to the server, and the same code is working on the same server in a different file in the same directory but in a different query. form is set to enctype="multipart/form-data",

here is the code where its not working

<!DOCTYPE html>
<html lang="en">
<head>
<?php require_once("includes/session.php"); ?>
<?php require_once("includes/dbc.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php confirm_logged_in(); ?>
<?php find_selected_post(); ?>

<?php
 $target = "../upload/"; 
 $target = $target . basename( $_FILES['post_photo']['name']); 

    if (intval($_GET['cat']) == 0) {
        redirect_to('cat_posts.php');
    }

    include_once("includes/form_functions.php");

    if (isset($_POST['submit'])) {
        $errors = array();

        $required_fields = array('post_title', 'position', 'visible', 'post_content');
        $errors = array_merge($errors, check_required_fields($required_fields, $_POST));

        $fields_with_lengths = array('post_title' => 30);
        $errors = array_merge($errors, check_max_field_lengths($fields_with_lengths, $_POST));

        $category_id = mysql_prep($_GET['cat']);
        $post_title = trim(mysql_prep($_POST['post_title']));
        $post_content = mysql_prep($_POST['post_content']);
        $post_description = mysql_prep($_POST['post_description']);
        $post_keywords = mysql_prep($_POST['post_keywords']);
        $post_tags = mysql_prep($_POST['post_tags']);
        $post_photo =($_FILES['post_photo']['name']);
        $position = mysql_prep($_POST['position']);
        $visible = mysql_prep($_POST['visible']);



        if (empty($errors)) {
            $query = "INSERT INTO ss_posts (
                        post_title, post_content, post_description, post_keywords, post_tags, post_photo, position, visible, category_id
                    ) VALUES (
                        '{$post_title}',  '{$post_content}', '{$post_description}', '{$post_keywords}',  '{$post_tags}', '{$post_photo}', {$position}, {$visible}, {$category_id}
                    )";
            if ($result = mysql_query($query, $connection)) {
                $message = "Successfully Created.";
                $new_post_id = mysql_insert_id();
                redirect_to("cat_posts.php?post={$new_post_id}");
            } else {
                $message = "The Post Could Not Be Created.";
                $message .= "<br />" . mysql_error();
            }
        } else {
            if (count($errors) == 1) {
                $message = "There was 1 error in the form.";
            } else {
                $message = "There were " . count($errors) . " errors in the form.";
            }
        }
    }

?>
    <?php
error_reporting(E_ALL);

echo "<pre>";
print_r($_FILES);
echo "</pre>";

echo "<br/>target: " . $target;

if (!move_uploaded_file($_FILES['post_photo']['tmp_name'], $target)) {
    echo "<br/>Upload failed.";
} else {
    echo "<br/>Upload done.";
}
?>
    <meta charset="utf-8"/>
    <title>New Post - Administration Panel</title>
    <script src="js/ckeditor/ckeditor.js" type="text/javascript"></script>
    <link rel="stylesheet" href="js/ckeditor/sample.css">
    <link rel="stylesheet" href="css/layout.css" type="text/css" media="screen" />
    <link rel="stylesheet" href="css/form.css" type="text/css" media="screen" />
    <link rel="stylesheet" href="css/button.css" type="text/css" media="screen" />  <!--[if lt IE 9]>
    <link rel="stylesheet" href="css/ie.css" type="text/css" media="screen" />
    <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
    <script src="js/jquery-1.5.2.min.js" type="text/javascript"></script>
    <script src="js/hideshow.js" type="text/javascript"></script>
    <script src="js/jquery.tablesorter.min.js" type="text/javascript"></script>
    <script type="text/javascript" src="js/jquery.equalHeight.js"></script>
    <script type="text/javascript">
    $(document).ready(function() 
        { 
          $(".tablesorter").tablesorter(); 
     } 
    );
    $(document).ready(function() {

    //When page loads...
    $(".tab_content").hide(); //Hide all content
    $("ul.tabs li:first").addClass("active").show(); //Activate first tab
    $(".tab_content:first").show(); //Show first tab content

    //On Click Event
    $("ul.tabs li").click(function() {

        $("ul.tabs li").removeClass("active"); //Remove any "active" class
        $(this).addClass("active"); //Add "active" class to selected tab
        $(".tab_content").hide(); //Hide all tab content

        var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
        $(activeTab).fadeIn(); //Fade in the active ID content
        return false;
    });

    });
    </script>
    <script type="text/javascript">
    $(function(){
    $('.column').equalHeight();
    });
    </script>
</head>
<body>

    <header id="header">
        <hgroup>
            <h1 class="site_title"><a href="index.php">Administration Panel</a></h1>
            <h2 class="section_title">New Post</h2><div class="btn_view_site">
            <a href="../index.php">View Site</a></div>
        </hgroup>
    </header> 
    <!-- end of header bar -->

    <section id="secondary_bar">
        <div class="user">
            <p>Hello, <?php echo $_SESSION['username']; ?> (<a href="logout.php">Logout</a>)</p>
        </div>

        <div class="breadcrumbs_container">
            <article class="breadcrumbs"><a href="index.php">Administration Panel</a> 
            <div class="breadcrumb_divider"></div> 
            <a class="current">New Post</a></article>
        </div>
    </section>
    <!-- end of secondary bar -->

    <aside id="sidebar" class="column" style="height:160%;">

                <hr/>
        <h3>Pages</h3>
        <ul class="toggle">
            <li class="icn_new_article"><a href="new_page.php">Add a New Page</a></li>
            <li class="icn_edit_article"><a href="edit_page.php">Edit/Delete a Page</a></li>
        </ul>
                <hr/>

        <h3>Users</h3>
        <ul class="toggle">
            <li class="icn_add_user"><a href="add_user.php">Add New User</a></li>
            <li class="icn_view_users"><a href="view_users.php">View Users</a></li>
        </ul>
                <hr/>

        <h3>Blog</h3>
        <ul class="toggle">
            <li class="icn_categories"><a href="new_category.php">Create a Category</a></li>
            <li class="icn_new_article"><a href="cat_posts.php">Create/Edit a Post</a></li>
            <li class="icn_settings"><a href="blogwidgets.php">Home Blog Settings</a></li>
            <li class="icn_settings"><a href="blogsettings.php">Blog Settings</a></li>
        </ul>
                <hr/>
        <h3>Settings</h3>
        <ul class="toggle">
            <li class="icn_settings"><a href="settings.php">Settings</a></li>
            <li class="icn_settings"><a href="logo.php">Site Logo</a></li>
            <li class="icn_jump_back"><a href="logout.php">Logout</a></li>
        </ul>

        <footer>
                <hr />
            <p><strong>Copyright &copy; 2013 Sky Define</strong></p>
            <p>Powered by <a href="http://www.skydefine.com">Sky Define</a></p>
            </br>           
            </br>

        </footer>
    </aside><!-- end of sidebar -->

    <section id="main" class="column">

                            <?php


                                move_uploaded_file($_FILES['post_photo']['tmp_name'], $target);
            // output a list of the fields that had errors
            if (!empty($errors)) {
                echo "<p class=\"errors\">";
                echo "Please review the following fields:<br />";
                foreach($errors as $error) {
                    echo " - " . $error . "<br />";
                }
                echo "</p>";
            }
            ?>



        <article class="module width_full">
            <header><h3>New Post</h3></header>



        <div class="module_content">




            <h2>Adding New Post</h2>
            <?php if (!empty($message)) {echo "<p class=\"message\">" . $message . "</p>";} ?>
            <?php if (!empty($errors)) { display_errors($errors); } ?>

<div class="mws-panel grid_4">
                    <div class="mws-panel-header">
                    </div>
                    <div class="mws-panel-body">





            <form class="mws-form"  enctype="multipart/form-data"  action="new_post.php?cat=<?php echo $sel_category['id']; ?>" method="post">              
                                <div class="mws-form-inline">

                            <?php $new_post = true; ?>
            <?php if (!isset($new_post)) {$new_post = false;} ?>

                <div class="mws-form-row">
                                    <label>Post Name:</label>
                                    <div class="mws-form-item large">
                                        <input type="text" name="post_title" id="post_title"   class="mws-textinput" placeholder="Post Name Goes Here." />
                                    </div>
                                </div>


                <div class="mws-form-row">
                                    <label>Post Description:</label>
                                    <div class="mws-form-item large">
                                        <input type="text" name="post_description" id="post_description"  class="mws-textinput" placeholder="Post Description Goes Here." />
                                    </div>
                                </div>              

                                <div class="mws-form-row">
                                    <label>Post Keywords:</label>
                                    <div class="mws-form-item large">
                                        <input type="text" name="post_keywords" id="post_keywords"  class="mws-textinput"   placeholder="Post Keywords Goes Here, Separated By Commas!" />
                                    </div>
                                </div>



                <div class="mws-form-row">
                                    <label>Post Content:</label>
                                    <div class="mws-form-item large">
                                    <textarea name="post_content" id="post_content" class="ckeditor" > </textarea>
                                    </div>
                                </div>

                <div class="mws-form-row">
                                    <label>Post Tags:</label>
                                    <div class="mws-form-item large">
                                        <input type="text" name="post_tags" id="post_tags"  class="mws-textinput"   placeholder="Post Tags Goes Here, Separated By Commas!" />
                                    </div>
                                </div>

                    <div class="mws-form-row">
                                    <label>Edit Post Photo:</label>
                                    <div class="mws-form-item large">
                                        <input type="file"  name="post_photo"  id="post_photo" />
                                    </div>
                                </div>

                <div class="mws-form-row">
                                    <label>Position:</label>    
                             <div class="mws-form-item large">
                     <select name="position">
                    <?php
                    if (!$new_post) {
                        $post_set = get_posts_for_category($sel_post['category_id']);
                        $post_count = mysql_num_rows($post_set);
                    } else {
                        $post_set = get_posts_for_category($sel_category['id']);
                        $post_count = mysql_num_rows($post_set) + 1;
                        }
                    for ($count=1; $count <= $post_count; $count++) {
                    echo "<option value=\"{$count}\"";
                    if ($sel_post['position'] == $count) { echo " selected"; }
                    echo ">{$count}</option>";
                    }
                    ?>
                    </select>   
                    </div>
                                </div>

                <div class="mws-form-row">
                                    <label>Visible:</label>
                                    <div class="mws-form-item large">

                    <input type="radio" name="visible" value="0"<?php 
                    if ($sel_post['visible'] == 0) { echo " checked"; } 
                    ?> /> No
                    &nbsp;
                    <input type="radio" name="visible" value="1"<?php 
                    if ($sel_post['visible'] == 1) { echo " checked"; } 
                    ?> /> Yes
                </div>
                            </div>








                                </form>




            <div class="mws-button-row">
                        <input type="submit" name="submit" value="Add Post" class="mws-button green" />
            <a class="mws-button red" href="index.php">Cancel</a>          

    </div>

                            </div>
                                </div>

            </div>

                </article>
                <div class="clear"></div>
            </div>
        </article><!-- end of stats article -->

        <div class="spacer"></div>
    </section>


</body>

</html>
4

2 回答 2

0

我尝试查找错误的方式:

使您的错误可见:

<?php error_reporting(E_ALL); ?>

$target应该是完整路径

检查您的 HTML 表单。您是否添加了enctype 属性

<form enctype="multipart/form-data" method="POST" action="script.php">

你的输入正确吗?

<input type="file" name="post_photo"/>

$_FILES数组的内容是什么?

<?php echo print_r($_FILES); ?>

编辑

请让我们知道这些行返回的数据:

<?php
error_reporting(E_ALL);

echo "<pre>";
print_r($_FILES);
echo "</pre>";

echo "<br/>target: " . $target;

if (!move_uploaded_file($_FILES['post_photo']['tmp_name'], $target)) {
    echo "<br/>Upload failed.";
} else {
    echo "<br/>Upload done."
}
?>

编辑 2(解决方案)

您的提交按钮在表单标签之外。这就是为什么。像这样修复它:

<form>
    <input type="submit" name="submit" value="Add Post" class="mws-button green" />
</form>
于 2013-06-16T10:39:47.653 回答
0

$target应该包含根路径

$target = $_SERVER['DOCUMENT_ROOT']"/upload/"; 
$target = $target . basename( $_FILES['post_photo']['name']);
于 2013-06-16T10:26:12.097 回答