移动上传的文件功能不会移动上传的图片

时间:2017-09-12 08:27:11

标签: php mysqli

我正在使用PHP创建一个在线商店,对于这个项目,我制作了一个CMS,允许管理员将新产品插入商店。

以下是insert_product.php页面:

    <?php 
if(isset($_POST['insert_product'])){
    // text data variables
    $product_title = $_POST['product_title'];
    $product_cat = $_POST['product_cat'];
    $product_brand = $_POST['product_brand'];
    $product_price = $_POST['product_price'];
    $product_desc = $_POST['product_desc'];
    $status = 'on';
    $product_keywords = $_POST['product_keywords'];

    // image names
    $product_img1 = $_FILES['product_img1']['name'];
    $product_img2 = $_FILES['product_img2']['name'];
    $product_img3 = $_FILES['product_img3']['name'];

    // image temp names
    $temp_name1 = $_FILES['product_img1']['tmp_name'];
    $temp_name2 = $_FILES['product_img2']['tmp_name'];
    $temp_name3 = $_FILES['product_img3']['tmp_name'];

    if($product_title == '' OR $product_cat == '' OR $product_brand == '' OR $product_price == '' OR $product_desc == '' OR $product_keywords == '' OR $product_img1 == ''){
        echo "
            <script>alert('Please fill all the fields!')</script>
            exit();
        ";
    }else{

        // uploading images to its folder
        move_uploaded_file($temp_name1,"product_images/$product_img1");
        move_uploaded_file($temp_name2,"product_images/$product_img2");
        move_uploaded_file($temp_name3,"product_images/$product_img3");

        $insert_product = "
        INSERT INTO products (cat_id,brand_id,date,product_title,product_img1,product_img2,product_img3,product_price,product_desc,status) 
        VALUES ('$product_cat','$product_brand',NOW(),'$product_title','$product_img1','$product_img2','$product_img3','$product_price','$product_desc','$status')
        ";

        $run_product = mysqli_query($con,$insert_product);

        if($run_product){
            echo "
                <script>alert('Product inserted successfully')</script>
                exit();
            ";
        }
    }
}
?>
<div class="content-wrapper">
    <section class="content-header">
        <h1>
            Add New Products
            <small>onlinestore.danoup.com</small>
        </h1>
        <ol class="breadcrumb">
            <li class="active">insertproducts.php</li>
        </ol>
    </section>
    <section class="content">
        <div class="box box-default">
            <div class="box-header with-border">
                <h3 class="box-title">Overall Info</h3>

                <div class="box-tools pull-right">
                    <!-- <button type="button" class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i></button> 
                    <button type="button" class="btn btn-box-tool" data-widget="remove"><i class="fa fa-remove"></i></button> -->
                </div>
            </div>
            <div class="box-body">
                <form action="" method="POST" enctype="multipart/form-data">
                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <div class="form-group">
                                    <label>Product Title:</label>
                                    <input type="text" name="product_title" class="form-control my-colorpicker1">
                                </div>
                            </div>
                            <div class="form-group">
                                <label>Product Category:</label>
                                <select class="form-control select2" name="product_cat" style="width: 100%;">
                                    <?php 
                                    echo get_cats();
                                    ?>
                                </select>
                            </div>
                            <div class="form-group">
                                <label>Product Brand:</label>
                                <select class="form-control select2" name="product_brand" style="width: 100%;">
                                    <?php 
                                    echo get_brands();
                                    ?>
                                </select>
                            </div>
                            <div class="form-group">
                                <div class="form-group">
                                    <label>Product Image 1:</label>
                                    <input type="file" name="product_img1" class="form-control my-colorpicker1">
                                </div>
                            </div>
                            <div class="form-group">
                                <div class="form-group">
                                    <label>Product Image 2:</label>
                                    <input type="file" name="product_img2" class="form-control my-colorpicker1">
                                </div>
                            </div>
                            <div class="form-group">
                                <div class="form-group">
                                    <label>Product Image 3:</label>
                                    <input type="file" name="product_img3" class="form-control my-colorpicker1">
                                </div>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <div class="form-group">
                                    <label>Product Price:</label>
                                    <input type="text" name="product_price" class="form-control my-colorpicker1">
                                </div>
                            </div>
                            <div class="form-group">
                                <div class="form-group">
                                    <label>Product Keywords:</label>
                                    <input type="text" name="product_keywords" class="form-control my-colorpicker1">
                                </div>
                            </div>
                            <input type="submit" name="insert_product" class="btn" value="Submit">
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-12">
                            <div class="box box-info">
                                <div class="box-header">
                                    <h3 class="box-title">Product Description:
                                        <small>Add new product to your onlinestore</small>
                                    </h3>
                                    <div class="pull-right box-tools">
                                        <button type="button" class="btn btn-info btn-sm" data-widget="collapse" data-toggle="tooltip" title="Collapse">
                                        <i class="fa fa-minus"></i></button>
                                        <button type="button" class="btn btn-info btn-sm" data-widget="remove" data-toggle="tooltip" title="Remove">
                                        <i class="fa fa-times"></i></button>
                                    </div>
                                </div>
                                <div class="box-body pad">
                                        <textarea name="product_desc" placeholder="Add description to your product" style="width: 100%; height: 200px; font-size: 14px; line-height: 18px; border: 1px solid #dddddd; padding: 10px;"></textarea>
                                </div>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
            <div class="box-footer">
                Visit <a href="https://select2.github.io/">Select2 documentation</a> for more examples and information about
                the plugin.
            </div>
        </div>
    </section>
</div>

所以它基本上包含用于插入新产品的html表单和php动作文件,并且它完美地工作,唯一的问题是move_uploaded_file函数,它不会移动上传的图像......

而且我在页面中看不到任何错误消息。 (即使我查看源代码)

您也可以尝试在我的在线CMS上测试此页面:

www.danoup.com/admin

因此,如果您想这样做,请按照以下说明访问insert_product页面:

1-输入验证码

2-插入这些用户凭据:

  username: pouyavey

  password: Godfather123456789

3-当您进入仪表板页面时,请转到左侧菜单并单击商店,然后插入新产品

4-之后,您可以插入新的虚拟产品作为示例,但请务必填写所有字段。

5-当您看到该消息时,已成功插入产品,转到online store站点。在那里你应该能够看到不包含任何上传图像的产品。像这样:

print screen

因此,如果您知道错误是什么以及为什么会发生这种情况请告诉我原因我真的需要它......

1 个答案:

答案 0 :(得分:0)

你需要检查一些事情:

  • 打开你的php ini并检查以下变量值 memory_limit,upload_max_filesize,post_max_size,file_uploads为ON。 (你没有上传file_max_filesize中提到的那个大小的文件)
  • 检查您上传的文件夹是否存在。
  • 允许您上传的文件夹。
  • 使error_reporting ON
  • 尝试在try / catch中进行上传