not displaying images uploaded in ubuntu localhost because of www-data

感情迁移 提交于 2019-12-23 02:42:16

问题


I uploaded image into my test project(it is an online shop) and they are not be displayed where they should(the index.php page of my project)

the directory is /var/www/html/php/

It has no problem with inserting the name of picture to database and the upload function work good and transfer images to the image directory of project well, but the only problem is that they are not displayed.

I must say that i've tried lots of ways and i granted so many types of users and group permissions and even the unsafe permissions like: chmod 777 or i take my user navid to www-data group and vise versa! But it doesn't work.

This is some of my code:

//function for adding product
function add_product($product_name, $product_price, $product_cat, $product_desc, $image_name, $image_tmp){
    global $db;
    move_uploaded_file($image_tmp, '../images/' . $image_name);
    $query = mysqli_query($db, "INSERT INTO products(product_name, product_price, product_cat, product_desc, product_image) VALUES ('$product_name','$product_price', '$product_cat', '$product_desc', '$image_name')");

}

this is my fetch function:

//the fuction to fetch all products from database to show in admin profile
function get_products($limit = 0){
    global $db;
    if ($limit == 0) {//this if works for showing all products if it is 0
        $query = mysqli_query($db, "SELECT * FROM products ORDER BY id DESC ");
    } else {
        $query = mysqli_query($db, "SELECT * FROM products ORDER BY id ASC LIMIT $limit");
    }
    return $query;
}

and this is the action of adding product and displaying the images:

//the action of ADD_PRODUCT
if (isset($_POST['add-product'])) {
    $product_name = $_POST['product-name'];
    $product_price = $_POST['product-price'];
    $product_cat = $_POST['product-cat'];
    $image_name = $_FILES['product-image']['name'];
    $image_tmp = $_FILES['product-image']['tmp_name'];
    $product_desc = $_POST['product-desc'];

    if (add_product($product_name, $product_price, $product_cat, $product_desc, $image_name, $image_tmp)) {
        $message = "";
    } else {
        $error = "";
    }
}

what i use in index page:

$products = get_products(6);

来源:https://stackoverflow.com/questions/49475181/not-displaying-images-uploaded-in-ubuntu-localhost-because-of-www-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!