2013-06-19 51 views
1

我使用此表单上载服务器中的文件并在数据库sql中创建记录。使用取消链接删除服务器文件()

如何从服务器删除图像?

我试过用另一个文件eliminaimg.php(transl:deleteimg.php)中的方法“取消链接”,但不工作! 帮帮我!!!

<?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 ([email protected]_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); 
} 
?> 
+0

'$ COMANDO = “从图像删除其中Id = '$ _REQUEST [ID]'”;'应'$ COMANDO =“从图像删除其中Id = '{$ _REQUEST [' ID” ]}'“;' –

+1

你确定你没有使用完整的uri,比如yourdomain.com/someimage.png? –

+1

$ path_img来自eliminaimg.php? – Polygnome

回答

1

你需要从数据库中,而不是$_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); 
} 
+0

警告:mysql_fetch_assoc():提供的参数不是有效的MySQL结果 –

+0

将'&&'更改为'和',这应该可以解决这个问题。 – Barmar

+0

你是最棒的!简单而且运作良好的解决方案。谢谢! –

0

可能是你给错误的文件路径。尝试这个。

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

}

+0

如果在上传脚本中创建文件时他不需要'DOCUMENT_ROOT',为什么他需要在删除脚本中? – Barmar

相关问题