1
Good afternoon, everyone,
Recently I’ve been trying to manage images where I need to use unlink. Follow the full function code:
$query_listaPic = "SELECT * FROM tbl_imagem WHERE tbl_produto_id = '$id'";
$listaPic = mysql_query($query_listaPic, $techConect) or die(mysql_error());
$row_listaPic = mysql_fetch_assoc($listaPic);
$totalRows_listaPic = mysql_num_rows($listaPic);
chmod("../img/produtos", 0755);
while($r = mysql_fetch_object($listaPic))
{
unlink('../img/produtos/'.$r->nome);
}
$deleteSQL = "DELETE FROM tbl_imagem WHERE tbl_produto_id = '$id'";
$Result1 = mysql_query($deleteSQL, $techConect) or die(mysql_error());
$rand = rand(5,98798967899);
foreach($_FILES['files']['tmp_name'] as $key => $tmp_name)
{
$filename = $rand.$_FILES['files']['name'][$key];
$file_tmp = $_FILES['files']['tmp_name'][$key];
$query = "INSERT INTO tbl_imagem(nome, tbl_produto_id) VALUE ('$filename', '$id')";
$dir = "../img/produtos/";
move_uploaded_file($file_tmp, $dir.$filename);
$Result2 = mysql_query($query, $techConect) or die(mysql_error());
//echo "<script> alert('Imagens Atualizadas.'); window.location = 'produto-info.php'; </script>";
}
In this excerpt he should select the images that already exist registered in such product, delete them and insert the new ones, but soon in unlink('../img/produtos/'.$r->nome);
it returns me an error of:
Unlink: Permission Denied - Trying to get Property of non-object
In this project I need to insert images in the 'products' url folder of this file (../img/products), and to access them, enter the name in the table tbl_image linked to table tbl_products.
To edit I would delete the records referring to the images and also delete the files, and then add the new files to the folder and the tbl_image table.
To delete would only remove the files and remove the BD records.
It would be something similar to this video.
The point is that I’m not being able to edit images from the record, not excluding old images and not inserting them into the BD (according to the current code described above).
Folder Structure:
Project > img > products > images
Project > admin > administrative pages
SQL:
CREATE TABLE `tbl_imagem` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`nome` varchar(100) NOT NULL,
`tbl_produto_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `fk_tbl_imagem_tbl_produto1_idx` (`tbl_produto_id`)
);
CREATE TABLE `tbl_produto` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`nome` varchar(50) NOT NULL,
`descricao` varchar(255) DEFAULT NULL,
`codigotm` varchar(11) NOT NULL,
`datacadastro` datetime NOT NULL,
`tbl_subcategoria_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `fk_tbl_produto_tbl_subcategoria1_idx` (`tbl_subcategoria_id`)
);
Thank you all from now on!
fetch_assoc
returns an array, and there is no object$r
.– Edilson
@Edilson, I’ll make some changes to the '$r = $Pic'
– Lucas
@Lucas the folder structure.
– rray
@rray is enough? don’t know exactly how to describe the folder structure..
– Lucas
Ta good yes, this php file is inside which folder?
– rray
@rray admin folder
– Lucas
These problems with the function
unlink
are all related, or are because of the bad reference, or else infrequent permissions in the directory: http://php.net/manual/en/function.chmod.php– Edilson
@Edilson, I tried to do the permissions aletrations (I made a change to the code to see how) and it made no difference, it seems like nothing I do changes anything. What would be this problem of bad reference?
– Lucas
If the error remains the
trying to get property...
, you should proceed as @rray mencinou’s response.– Edilson