Question

I use this form for uploading files in the server and create the record in database sql.

How make for delete images from the server?

i've tried with method "unlink" in another file eliminaimg.php (transl: deleteimg.php), but not work!!! Help me!!!

 <?php
     @include 'config.php';

    if(isset($_POST['Submit'])){
      // faccio un po' di inclusioni...
       @require 'function.php';
      // Creo una array con i formati accettati
      $tipi_consentiti = array("image/gif","image/jpeg","image/png");

      // verifico che il formato del file sia tra quelli accettati
      if (@in_array($_FILES['imagefile']['type'], $tipi_consentiti)){ 
        // copio il file nella cartella delle immagini
        @copy ($_FILES['imagefile']['tmp_name'], $path_img . $_FILES['imagefile']['name']);

        // recupero i dati dal form
        $titolo = @addslashes($_POST['titolo']);
        $categoria = @addslashes($_POST['categoria']);
        $nome = @addslashes($_FILES['imagefile']['name']);
        $path = $path_img . stripslashes($nome);
        $tipo = @addslashes($_FILES['imagefile']['type']);


        // creo la miniatura
        @makeThumb($path_img,$path,$nome,$tipo);

        // aggiorno il database
        $query = "INSERT INTO images (Titolo,Categoria,Nome,Tipo) VALUES('$titolo','$categoria','$nome','$tipo')";
        $res = @mysql_query($query) or die (mysql_error());


        // Stampo a video un po' di informazioni
        echo "Nome: ".$_FILES['imagefile']['name']."<br />"; 
        echo "Dimensione: ".$_FILES['imagefile']['size']."<br />"; 
        echo "Tipo: ".$_FILES['imagefile']['type']."<br />"; 
        echo "Copia eseguita con successo."; 
      }else{
        // stampo un messaggio di errore nel caso in cui il file sia di un formato non consentito
        echo "Impossibile eseguire l'upload.";
      }
    } 

    echo "</form>";
    echo "<div id='panelright' class='panelright'>";
    echo "<button id='buttonallimg'>Tutti</button>";
    echo "<button id='buttoncollimg'>Collane</button>";
    echo "<button id='buttonanelimg'>Anelli</button>";
    echo "<button id='buttonorecimg'>Orecchini</button></div>";
    echo "<div id='all' class='viewgallery'>";
    echo "<table width='100px' border='1'>";

    $query = "SELECT * FROM images";
    $res = mysql_query($query) or die (mysql_error());
    $column = 1;
      while ($dati=@mysql_fetch_array($res)){
         if ($column == 1) {
            echo "<tr>";
        }


        $nome = stripslashes($dati['Nome']);

         echo "<td><a href='eliminaimg.php?Id=$dati[Id]?confirm=true' class='confirm'> <img src='images/delete.png'></a>";
         echo "<a href=\"" . $path_img . $nome . "\"rel=\"gallery[gallery1]\"><img src=\"" . $path_img . "tb_" . $nome . "\" \"></a></td>";

    if ($column == 5) {
            echo "</tr>";
            $column = 1;
        } else {
            $column++;
        }
    }
    if ($column != 1) {
        echo "</tr>";
    }
    echo "</table></div>";

eliminaimg.php

<?php

function delete(){
 @include 'config.php';


$comando = "DELETE from images " .
"where Id = '$_REQUEST[Id]'";


if(!mysql_query($comando))
echo "Modifica fallita <br/>";



chmod($path_img,0777);

$nome = $_REQUEST['imagefile']['name'];
$path = $path_img . $nome;


unlink ($path);


mysql_close($cn);
}
?>
Was it helpful?

Solution

You need to get the filename from the database, not $_REQUEST.

function delete(){
 @include 'config.php';

$comando = "SELECT Nome FROM images WHERE Id = '$_REQUEST[Id]'";
if ($result = mysql_query($comando) and $row = mysql_fetch_assoc($result)) {
  $nome = $row['Nome'];
} else {
  die("Query failed: " . mysql_error());
}

$comando = "DELETE from images " .
"where Id = '$_REQUEST[Id]'";


if(!mysql_query($comando))
echo "Modifica fallita <br/>";



chmod($path_img,0777);

$path = $path_img . $nome;


unlink ($path);


mysql_close($cn);
}

OTHER TIPS

May be you are giving wrong file path. try this.

function del_directory_record($filename) {
  return unlink($_SERVER['DOCUMENT_ROOT'] . "/foldername/$filename");

}

Check to make sure that the premissions of the file are really 777. Also see who owns the file. PHP may be own that file. You might need chown that file as well.

SureVerify - Email Verification

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top