Unlink: Permission Denied - Trying to get Property of non-object

Asked

Viewed 85 times

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, I’ll make some changes to the '$r = $Pic'

  • @Lucas the folder structure.

  • @rray is enough? don’t know exactly how to describe the folder structure..

  • Ta good yes, this php file is inside which folder?

  • @rray admin folder

  • 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, 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?

  • If the error remains the trying to get property..., you should proceed as @rray mencinou’s response.

Show 4 more comments

1 answer

0


while($r = $Pic)
{
    unlink('../img/produtos/'.$r->nome);
}

$r receives $Pic, wouldn’t it be the same? where is the stopping condition?

$Pic = $row_listaPic['nome'];, $r is an array and not an object that is one of the reasons unklink fails.

  • I got it.. OK, I made the change and put it like this: 'mysql_fetch_object($listPic)' so I can run the array. Right? Here it still doesn’t erase the images.. @rray

  • @Lucas appeared some mistake?

  • did not appear, but did not delete @rray images either

  • @Lucas better edit the question code.

  • while: 'while($r = mysql_fetch_object($listPic))' @rray

  • Put in your question the structure of the project, if you can in text.

  • I am actually trying to do similar to this video (https://www.youtube.com/watch?v=vjbpyYXqP-A), I will try to describe

  • @Lucas When you print $r->nome what appears? comes the name of the file with extension? vc gave the correct permission in the folder?

  • when printing appears '5555076openphotonet_P1000795.JPG' which is the name of 1 file. I haven’t been able to work with folder permissions yet, but when entering properties it is released for all changes for all users. @rray

  • @Lucas, try to go the whole way, I’m not sure if it’ll work, unlink(realpath('../img/produtos/'.$r->nome));

  • I tried here, and it made no difference unlink. I was thinking here, is this not happening due to PHP version? Easyphp 5.3.0?

  • @Lucas, no, check the permissions of the img folder, full control just to test, then you can change.

  • It continues the same way. I gave full permission to all users and groups

  • @Lucas, do if(!unlink('../img/produtos/'.$r->nome)){ echo 'erro<br>';}

  • It’s the same way too, it made no difference... :(

Show 11 more comments

Browser other questions tagged

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