problems deleting file from unlink()

Asked

Viewed 109 times

2

i would like to delete a file in the file, and nothing is going well of the following Strict standards error: Only variables should be passed by Ference could you guide me to resolve this error? I’ll be grateful! Image link:

<a id="photo-'.$resultfotos['id'].'" href="site.php?userid='.$user_id.'&pageid='.$page.'&fotoid='.$resultfotos['id'].'">
   <img src="uploads/photos/'.$resultfotos['foto'].'"/>
</a> 

Link to delete the photo:

Delete the photo //I made a include on the "deletefotos.php page"//

<?php 
   $image = end(explode('-',$_GET['photo']));
   $result = Pagina::minhaFoto($imagem,$user_id); 
   if($result['res']){
      if(Pagina::delFoto($idDaimagem)){
         if(file_exists('../uploads/photos'.$result['foto'])){
            unlink('../uploads/photos'.$result['foto']);
         }
      }
   } ?> 

page class, page.class.php

<?php
 static function minhaFoto($imagem){
   $select = self::getConn()->prepare('SELECT `pagina` FROM `fotos` WHERE `id`=? LIMIT 1');
   $select->execute(array($imagem));
   if($select->rowCount()==1){
      $asfoto = $select(PDO::FETCH_ASSOC);
      $dados['res'] = self::myEvent($asfoto['pagina'],$user_id);
      $dados['foto'] = $asfoto['foto'];
      return $d; 
  } 
}

static function delFoto($idDaimagem){
   $del = self::getConn()->prepare('DELETE FROM `fotos` WHERE `id`=?');
   return $del->execute(array($idDaimagem));
}     
?>

2 answers

3

The problem is that end() expects a reference or is a variable do, the two-step assignment.

Change:

$image = end(explode('-',$_GET['photo']));

To:

$nome = explode('-',$_GET['photo'])
$ext = end($nome);

Stay tuned for signing functions with & it means that the argument should be passed as reference most of the time.

Mixed end ( array &$array )

Related:

Doubt about PHP function 'end'

3

Correct @rray response. The problem is in relation to end accept only variables, not array expressions. Therefore, this function accepts an argument that is a variable, because it is a parameter with reference.

There is a way to circumvent this function limitation end.

Thus:

function last(array $array)
{
    return end($array);
}

Thus, the $array which is parameter of last, is passed on to end as argument, becoming reference to end, and may be an expression for last.

Example:

echo last(explode('-', '1-2-3')); // Imprime '3'
  • Good idea the function, +1

  • I saw it in the source code of laravel 3. You can trick the functions that accept reference like this. For example, to get the first item of the array: function first($a) { return reset($a) }. Ex: first([1, 2, 3]).

  • :D Portable. There is another question of yours ... q speaks of a function that according to the manual accepts only references, but for some reason confusingly accepts values.

  • Of Current

Browser other questions tagged

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