How to show an image whose path is in the database?

Asked

Viewed 1,448 times

0

I’m using Extjs 4 and PHP.

I have an image saved in a server folder and its path is saved in my database. I tried to do something like this:

Code in the controller:

onVerImgClick: function(btn, o, e0pts){
    var grid = btn.up('grid');
    var records = grid.getSelectionModel().getSelection();
    var produtoId = records[0].data.id;
    //var image = Ext.create('ExtMVC.view.verImagem');
    Ext.Ajax.request({
        method: 'POST',
        url: 'php/importadorDadosImagem.php',
        success: function(response){
            var array = Ext.decode(response.responseText);
            var cont = 0;
            var caminho;
            while(cont < array.data.length){
                if(produtoId == array.data[cont].idProduto){
                    caminho = array.data[cont].imagem;
                }
                cont++;
            }
            cont = 0;
            if(caminho != null){
                var image = Ext.create('ExtMVC.view.verImagem');
                image.getEl().set({
                    src: caminho
                });
            }
        }
    });
}

mage.js

Ext.define('ExtMVC.view.verImagem', {
   extend: 'Ext.window.Window',
   alias: 'widget.verimagem',
   autoShow: true,
   modal: true,
   width: 400,
   height: 400,
   items: [
       {
           xtype: 'component',
           autoEl: {
               tag: 'img',
               src: ' '
           }
       }
   ]
});

importadorDadosImage.php

<?php
include("connect.php");

$queryString = "SELECT idProduto, imagem FROM imagens";

//consulta sql
$query = mysql_query($queryString) or die(mysql_error());

//faz um looping e cria um array com os campos da consulta
$dados = array();
while($dado = mysql_fetch_assoc($query)) {
    $dados[] = $dado;
}

$total = count($queryString);

echo json_encode(array(
    "success" => mysql_errno() == 0,
    "total" => $total,
    "data" => $dados
));

This way, no error is shown. However, the image does not appear.

1 answer

1

Good morning, have you ever first tried connecting to the database by PHP? you have to have in the database the following information, name and extension of the image, then in php you put in a variable, the full address of the root folder to the folder where the image is, then concatenate with the name and extension of the image, ai just insert into a javascript variable and assign in your code to display the image.

example

SQL = name | ext = image.ext

PHP = server/folder/subfolder/image.ext

EJS = http://server/folder/subfolder/image.ext

  • In the database I have saved the full path of the image. For example: . /uploads/Jellyfish.jpg .

  • @Marceloaugusto beware of this passage . / in my experience, I saw that this always error, instead of this, use a full url request, and see if only this will make the image appear, manually put in a variable with http and everything and see if it works, if yes, just create a request of your full url

  • If I put the full path does not give. If I put . /uploads/Jellyfish.jpg in the src of the script verImagem.js, leaving it static, the image appears.

  • @Marceloaugusto then if you put static it appears with this formatting, already if you search the database it does not appear, then your error is not in js but in the database, as you are searching in the database?

  • I added the PHP code where I import the image data.

  • I have all the products on a grid. To add an image, I select the product and click on a button. To see the image, it’s the same way. In BD, I have a table for the products and another table for the images. So to reference an image, I get the id of the selected product. When recovering, I compare the image id with the product id, if it is equal and take the path and try to set in src. But it is not setando.

  • @Marceloaugusto try using PDO you will see that it is much simpler and safer, follow an example. , but I saw that you didn’t add a search criteria to your querystring

  • Have you checked if php’s return is coming to ajax? Because as I saw you did not set the header in php, put this before echo: header('Content-Type: application/json');

  • <?php try { $a = new PDO("mysql:host=" . $host . "; dbname=" . $base, $user, $pass);&#xA;$a -> setAttribute(PDO :: ATTR_ERRMODE, PDO :: ERRMODE_EXCEPTION);&#xA;$a -> exec("set names utf8");&#xA;$b = $a -> prepare("SELECT idProduto, imagem FROM imagens WHERE idProduto = :var");&#xA;$b -> execute(array(":var" => $var));&#xA;$c = $b -> fetchAll();&#xA;if(count($c)) {&#xA;foreach($c as $d) {&#xA;echo $d["imagem"]; } }&#xA;$a = null; } catch(PDOException $e) { echo $e -> getMessage(); } ?>

  • Do not post code in the comments, edit your reply and the suggestion there.

  • @fly flourigh the return is correct yes. I put it on the console and it’s coming out right. The header is set in connect.php.

  • I suggest that you debug in parts then, set a static string for src instead of the path variable, if you pass change level until you get to where it fails.

  • because, you have to see in each part where is the error, because as I understood, the variable of one that is the path, is not passing to the variable of another to display the image, I mean, Voce search with php and inserts the path in a variable, this path then passes to another variable of javascrip and then is displayed, well, in case, Voce opens a product, this product has an id this id has multiple images these images display in various places, ie when ordering the product it creates an array with all images, if Voce creates a loop, it displays the images before going to javascript?

  • I was able to load the images as I wanted. I created another variable and made it receive the component that has xtype = 'image'. From there, I was able to do the novaVariable.setSrc(path); The problem now is how I save the path to the folders @flyflourigh spoke about. I am sending the way. /uploads/ it does not save and shows the following error: Warning: move_uploaded_file(./uploads/Koala.jpg): failed to open stream: No such file or directory in C: Program Files (x86) Easyphp-12.1 www app extjs-crud-mvc php receivedUpload.php on line 56.

  • If I put .. /uploads/ it saves, but at the time of seeing the image it does not found, that is, it does not find the folder with the image.

  • for this, just create two variables, one to insert the image in the database that would be a . /uploads/ and another to move the image that is a .. /uploads/

  • basically you will concatenate a point . before the other point .. when moving only

  • Ready! It worked. Thanks, guys!

Show 13 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.