0

I am working on my PHP code (insert_post.php) and I have two errors:

**Warning: move_uploaded_file(news_images/RaiderX.jpg): failed to open stream: No such file or directory in C:\xampp\htdocs\my_cms\admin\insert_post.php on line 106

Warning: move_uploaded_file(): Unable to move 'C:\xampp\tmp\php60F7.tmp' to 'news_images/RaiderX.jpg' in C:\xampp\htdocs\my_cms\admin\insert_post.php on line 106**

Everything is fine and I can see that data (images) is coming through to my mysql database but I cannot understand why there are these two errors.

This is the code:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>

<style type="text/css">
td, td {
        padding:0px;
        margin:0px;
        }

</style>
<script src="//tinymce.cachefly.net/4.1/tinymce.min.js"></script>
<script>tinymce.init({selector:'textarea'});</script>

</head>

<body>

<form action="insert_post.php" method="post" enctype="multipart/form-data">

        <table width="800" align="center" border="2">
        <tr bgcolor="#F47302">
                <td colspan="6" align="center"><h1>Insert New Post</h1></td>
        </tr>

        <tr>
                <td align="right" bgcolor="F47302"><strong>Post Title:</strong></td>
            <td><input type="text" name="post_title" size="40"</td>
        </tr>

         <tr>
                <td align="right"bgcolor="F47302" ><strong>Post Categories:</strong></td>
            <td>
                <select name="cat">
                        <option value="null">Select a Category</option>
           <?php
                        include("../includes/database.php");

                        $get_cats = "select * from categories";

                        $run_cats = mysqli_query($con, $get_cats);

                        while ($cats_row=mysqli_fetch_array($run_cats)) {

                                $cat_id=$cats_row['cat_id'];
                                $cat_title=$cats_row['cat_title'];

                                echo "<option value='$cat_id'>$cat_title</option>";
                                }
                        ?>
                </select>

            </td>
        </tr>

         <tr>
                <td align="right" bgcolor="F47302"><strong>Post Author:</strong></td>
            <td><input type="text" name="post_author" size="40"</td>
        </tr>
         <tr>
                <td align="right" bgcolor="F47302"><strong>Post Keywords:</strong></td>
            <td><input type="text" name="post_keywords" size="50"</td>
        </tr>

         <tr>
                <td align="right" bgcolor="F47302"><strong>Post Image:</strong></td>
            <td><input type="file" name="post_image" size="50"</td>
        </tr>
         <tr>
                <td align="right" bgcolor="F47302"><strong>Post Content:</strong></td>
            <td><textarea name="post_content" rows="15" cols="60"></textarea></td>
        </tr>
         <tr>
            <td colspan="6" align="center" bgcolor="F47302"><input type="submit" name="submit" value="Publish Now"</td>
        </tr>

    </table>
</form>
</body>
</html>

<?php
if(isset($_POST['submit'])) {

   $post_title = $_POST['post_title'];
   $post_date = date('m-d-y');
   $post_cat = $_POST['cat'];
   $post_author = $_POST['post_author'];
   $post_keywords = $_POST['post_keywords'];
   $post_image = $_FILES['post_image'] ['name'];
   $post_image_tmp = $_FILES['post_image']['tmp_name'];
   $post_content = $_POST['post_content'];



        if($post_title=='' OR $post_cat=='null' OR $post_author=='' OR $post_keywords=='' OR $post_image=='' OR $post_content==''){

                echo "<script>alert('Please fill in all the fileds')</script>";
                exit();
                }
        else {

                move_uploaded_file($post_image_tmp,"news_images/$post_image");

                $insert_posts = "insert into posts (category_id,post_title,post_date,post_author,post_keywords,post_image,post_content) values ('$post_cat','$post_title','$post_date','$post_author','$post_keywords','$post_image','$post_content')";

                $run_posts = mysqli_query($con,$insert_posts);

                        echo "<script>alert('Post Has been Published!')</script>";

                        echo "<script>window.open('index.php?insert_post','_self')</script>";
                }
        }

?>
Álvaro González
  • 142,137
  • 41
  • 261
  • 360
Michal Koczwara
  • 123
  • 1
  • 6

1 Answers1

0

check

$_FILES["post_image"]["error"]

for any sort of upload error first, this may give you a better idea of why this is happening and also catch the problem so you don't cache the info of a missing file in your DB