Unconfigured Ad Widget

Collapse

Anúncio

Collapse
No announcement yet.

[Dúvida] Excluir arquivo com unlink

Collapse
X
 
  • Filter
  • Tempo
  • Show
Clear All
new posts

  • Font Size
    #1

    Duvida [Dúvida] Excluir arquivo com unlink

    Bom dia!

    Sou iniciante em PHP e uso a aba "data" do Dreamweaver para realizar as operações de inclusão, alteração e exclusão...
    Enfim... No meu arquivo de exclusão consigo deletar o registro do banco de dados tudo certo, mas não consigo deletar o arquivo associado ao registro usando o comando unlink...
    Preciso de ajuda com isso. Vou deixar o código do meu arquivo aqui para análise.
    Obrigado!

    Código:
    <?php require_once('Connections/con_danis.php'); ?>
    <?php
    if (!isset($_SESSION)) {
      session_start();
    }
    $MM_authorizedUsers = "administrador";
    $MM_donotCheckaccess = "false";
    
    // *** Restrict Access To Page: Grant or deny access to this page
    function isAuthorized($strUsers, $strGroups, $UserName, $UserGroup) { 
      // For security, start by assuming the visitor is NOT authorized. 
      $isValid = False; 
    
      // When a visitor has logged into this site, the Session variable MM_Username set equal to their username. 
      // Therefore, we know that a user is NOT logged in if that Session variable is blank. 
      if (!empty($UserName)) { 
        // Besides being logged in, you may restrict access to only certain users based on an ID established when they login. 
        // Parse the strings into arrays. 
        $arrUsers = Explode(",", $strUsers); 
        $arrGroups = Explode(",", $strGroups); 
        if (in_array($UserName, $arrUsers)) { 
          $isValid = true; 
        } 
        // Or, you may restrict access to only certain users based on their username. 
        if (in_array($UserGroup, $arrGroups)) { 
          $isValid = true; 
        } 
        if (($strUsers == "") && false) { 
          $isValid = true; 
        } 
      } 
      return $isValid; 
    }
    
    $MM_restrictGoTo = "acessoadmin.php";
    if (!((isset($_SESSION['MM_Username'])) && (isAuthorized("",$MM_authorizedUsers, $_SESSION['MM_Username'], $_SESSION['MM_UserGroup'])))) {   
      $MM_qsChar = "?";
      $MM_referrer = $_SERVER['PHP_SELF'];
      if (strpos($MM_restrictGoTo, "?")) $MM_qsChar = "&";
      if (isset($_SERVER['QUERY_STRING']) && strlen($_SERVER['QUERY_STRING']) > 0) 
      $MM_referrer .= "?" . $_SERVER['QUERY_STRING'];
      $MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" . urlencode($MM_referrer);
      header("Location: ". $MM_restrictGoTo); 
      exit;
    }
    ?>
    <?php
    if (!function_exists("GetSQLValueString")) {
    function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
    {
      if (PHP_VERSION < 6) {
        $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
      }
    
      $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
    
      switch ($theType) {
        case "text":
          $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
          break;    
        case "long":
        case "int":
          $theValue = ($theValue != "") ? intval($theValue) : "NULL";
          break;
        case "double":
          $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
          break;
        case "date":
          $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
          break;
        case "defined":
          $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
          break;
      }
      return $theValue;
    }
    }
    
    if ((isset($_POST['pos_id'])) && ($_POST['pos_id'] != "")) {
      $deleteSQL = sprintf("DELETE FROM tb_postagens WHERE pos_id=%s",
                           GetSQLValueString($_POST['pos_id'], "int"));
      
      mysql_select_db($database_con_danis, $con_danis);
      $Result1 = mysql_query($deleteSQL, $con_danis) or die(mysql_error());
      $arquivo = "uploads/".$row_pega_postagem['pos_arquivo'];
      unlink($arquivo);
      $deleteGoTo = "postagenslisadm.php";
      if (isset($_SERVER['QUERY_STRING'])) {
        $deleteGoTo .= (strpos($deleteGoTo, '?')) ? "&" : "?";
        $deleteGoTo .= $_SERVER['QUERY_STRING'];
      }
      header(sprintf("Location: %s", $deleteGoTo));
    }
    
    $colname_pega_postagem = "-1";
    if (isset($_GET['pos_id'])) {
      $colname_pega_postagem = $_GET['pos_id'];
    }
    mysql_select_db($database_con_danis, $con_danis);
    $query_pega_postagem = sprintf("SELECT tb_postagens.pos_id, tb_postagens.pos_titulo, tb_postagens.pos_texto, tb_postagens.pos_arquivo, tb_postagens.pos_data, tb_postagens.pos_destinatarioid, tb_usuarios.usu_id, tb_usuarios.usu_nome, DATE_FORMAT (tb_postagens.pos_data, '%%d/%%m/%%Y') as  pos_data FROM tb_postagens INNER JOIN tb_usuarios ON tb_postagens.pos_destinatarioid = tb_usuarios.usu_id WHERE tb_postagens.pos_id = %s", GetSQLValueString($colname_pega_postagem, "int"));
    $pega_postagem = mysql_query($query_pega_postagem, $con_danis) or die(mysql_error());
    $row_pega_postagem = mysql_fetch_assoc($pega_postagem);
    $totalRows_pega_postagem = mysql_num_rows($pega_postagem);
    ?>
    <!doctype html>
    <html>
    <head>
    <meta charset="windows-1250">
    <title>Danis - Excluir Postagem</title>
    <link href="estilo.css" rel="stylesheet" type="text/css">
    <link href='http://fonts.googleapis.com/css?family=Oxygen:400,300,700' rel='stylesheet' type='text/css'>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"></head>
    
    <body>
    
    <?php include("header.php");?>
    
    <div id="conteudo-container">
    	<div id="conteudo">
    		<p>&nbsp;</p>
     	  <h2>Excluir Postagem - ATEN&Ccedil;&Atilde;O! Esta a&ccedil;&atilde;o n&atilde;o poder&aacute; ser desfeita!</h2>
          <form method="POST" enctype="multipart/form-data" name="form1" id="form1">
            <table width="60%" align="center">
              <tr valign="baseline">
                <td nowrap align="right">Destinatário - </td>
                <td><?php echo $row_pega_postagem['usu_nome']; ?></td>
              </tr>
              <tr valign="baseline">
                <td nowrap align="right">Título - </td>
                <td><?php echo $row_pega_postagem['pos_titulo']; ?></td>
              </tr>
              <tr valign="baseline">
                <td align="right" valign="middle" nowrap>Texto - </td>
                <td><label for="pos_texto"><?php echo $row_pega_postagem['pos_texto']; ?></label></td>
              </tr>
              <tr valign="baseline">
                <td nowrap align="right">Arquivo - </td>
                <td><?php echo $row_pega_postagem['pos_arquivo']; ?></td>
              </tr>
              <tr valign="baseline">
                <td nowrap align="right">Data - </td>
                <td><?php echo $row_pega_postagem['pos_data']; ?></td>
              </tr>
              <tr valign="baseline">
                <td nowrap align="right">&nbsp;</td>
                <td><input name="Excluir" type="submit" id="Excluir" value="Excluir"></td>
              </tr>
            </table>
              <input name="pos_id" type="hidden" id="pos_id" value="<?php echo $row_pega_postagem['pos_id']; ?>">
          </form>
        </div><!-- conteudo -->
    </div>
    
    <?php include("footer.php");?>
    
    </body>
    </html>
    <?php
    mysql_free_result($pega_postagem);
    ?>
    Similar Threads

  • Font Size
    #2
    Eu mesmo acabei resolvendo.
    Em algum lugar do código, ele estava perdendo o valor da variável que guardava o arquivo a ser deletado, então eu resolvi criar uma variável de sessão mesmo, aí não tive mais problemas.
    Espero que possa ajudar outras pessoas.

    Comment

    X
    Working...
    X