Potrebna pomoć oko unlink funkcije

1

Potrebna pomoć oko unlink funkcije

offline
  • Pridružio: 30 Avg 2008
  • Poruke: 91

Napisano: 28 Mar 2011 12:25

Pozdrav,

Kada želim da izbrišem jednu sliku onda ovo radi
$id =$row_slike['id']; $file_1 = "../imgs/$id/1.jpg"; if (file_exists($file_1)) {     unlink($file_1); } else {     ; }

Kako mogu sve "jpg" slike da izbrišem u tom folderu

pokušao sam ovako nešto ali ne radi

foreach(glob('../imgs/$id/*.jpg') as $file){      unlink($file); }

Dopuna: 28 Mar 2011 12:57

Našao sam ovo, ali problem je u tome što ne prepznaje $id, ako upišem broj onda radi

$id =$row_slike['id']; define('PATH', '../imgs/$id/'); function destroy($dir) {     $mydir = opendir($dir);     while(false !== ($file = readdir($mydir))) {         if($file != "." && $file != "..") {             chmod($dir.$file, 0777);             if(is_dir($dir.$file)) {                 chdir('.');                 destroy($dir.$file.'/');                 rmdir($dir.$file) or DIE("couldn't delete $dir$file<br />");             }             else                 unlink($dir.$file) or DIE("couldn't delete $dir$file<br />");         }     }     closedir($mydir); } destroy(PATH); echo 'all done.';



Registruj se da bi učestvovao u diskusiji. Registrovanim korisnicima se NE prikazuju reklame unutar poruka.
offline
  • Pridružio: 15 Maj 2006
  • Poruke: 333
  • Gde živiš: Babušnica

$id =$row_slike['id']; $file_1 = "../imgs/$id/1.jpg"; if (file_exists($file_1)) {     unlink($file_1); } else {     ; }

Ovaj kod ubaci u while petlju (ako tako dobijaš $id iz baze) kojom izvlačiš $id. Umesto 1.jpg stavi ime slike (pod pretpostavkom da imaš polje za ime u bazi).



offline
  • Pridružio: 30 Avg 2008
  • Poruke: 91

Problem je u tome što se meni slike nalaze u istoj tabeli kao i smeštaj, nisu posebno

offline
  • Pridružio: 15 Maj 2006
  • Poruke: 333
  • Gde živiš: Babušnica

Da ne bi nagađali gde je šta, šta ima a šta nema postavi ceo kod/funkciju ili deo koda iznad $id =$row_slike['id'];
I koja polja imaš u bazi pored id?

offline
  • Pridružio: 21 Apr 2007
  • Poruke: 98

Probaj ovako:

    <?php    $ekstenzije = array('jpg','png'); //Nalazi JPG i PNG fajlove    $files = array();    $dir = opendir("../imgs/$id/");    $count=0;    ?><textarea style="width:600;height:450px;">Brisem:<?php    while( ($file = readdir($dir)) != false )     {         if( !is_dir($file) && !in_array($file,array('.','..')) && in_array(substr($file,strrpos($file,'.')+1),$ekstenzije) )         {             $files[] = array(                     'path' => $file,                     'filename' => pathinfo($file,PATHINFO_BASENAME),                     'name' => pathinfo($file,PATHINFO_FILENAME)                 );       if (file_exists($file)) {$count++; echo "\n$count | $file"; unlink($file); }         }     }     closedir($dir);    ?></textarea>        

offline
  • Pridružio: 30 Avg 2008
  • Poruke: 91

Napisano: 28 Mar 2011 13:58

mysql_select_db($database_configuration, $configuration); $query_slike = sprintf("SELECT * FROM products WHERE id = %s", GetSQLValueString($colname_slike, "int")); $slike = mysql_query($query_slike, $configuration) or die(mysql_error()); $row_slike = mysql_fetch_assoc($slike); $totalRows_slike = mysql_num_rows($slike); $id =$row_slike['id']; $dir='../imgs/$id'; define('PATH', '../imgs/$id/'); function destroy($dir) {     $mydir = opendir($dir);     while(false !== ($file = readdir($mydir))) {         if($file != "." && $file != "..") {             chmod($dir.$file, 0777);             if(is_dir($dir.$file)) {                 chdir('.');                 destroy($dir.$file.'/');                 rmdir($dir.$file) or DIE("couldn't delete $dir$file<br />");             }             else                 unlink($dir.$file) or DIE("couldn't delete $dir$file<br />");         }     }     closedir($mydir); } destroy(PATH); echo 'all done.';        if ((isset($_GET['id'])) && ($_GET['id'] != "")) {   $deleteSQL = sprintf("DELETE FROM products WHERE id=%s",                        GetSQLValueString($_GET['id'], "int"));   mysql_select_db($database_configuration, $configuration);   $Result1 = mysql_query($deleteSQL, $configuration) or die(mysql_error());   $deleteGoTo = "accommodations.php";   if (isset($_SERVER['QUERY_STRING'])) {     $deleteGoTo .= (strpos($deleteGoTo, '?')) ? "&" : "?";     $deleteGoTo .= $_SERVER['QUERY_STRING'];       }   header(sprintf("Location: %s", $deleteGoTo)); }

Dopuna: 28 Mar 2011 13:59

Može i funkcija da izbriše sve jpg slike u folderu $id

offline
  • Pridružio: 15 Maj 2006
  • Poruke: 333
  • Gde živiš: Babušnica

Ako ti odgovara ovako...
//... $slike = mysql_query($query_slike, $configuration) or die(mysql_error()); $totalRows_slike = mysql_num_rows($slike); while($row_slike = mysql_fetch_array($slike)) { $id =$row_slike['id']; $ime_slike = $row_slike['IME_POLJA_ZA_NAZIV_SLIKE']; $dir="../imgs/$id"; $file_1 = "$dir/$ime_slike.jpg"; if (file_exists($file_1)) {     unlink($file_1); } else {     ; } } //...

offline
  • Pridružio: 30 Avg 2008
  • Poruke: 91

Napisano: 28 Mar 2011 14:18

Neće da briše ništa, kod koji sam postavio na početku radi ali nekako treba da ga nateram da prepozna $id, jer ako upišem id manualno onda radi

$id =$row_slike['id']; define('PATH', '../imgs/$id/'); function destroy($dir) {     $mydir = opendir($dir);     while(false !== ($file = readdir($mydir))) {         if($file != "." && $file != "..") {             chmod($dir.$file, 0777);             if(is_dir($dir.$file)) {                 chdir('.');                 destroy($dir.$file.'/');                 rmdir($dir.$file) or DIE("couldn't delete $dir$file<br />");             }             else                 unlink($dir.$file) or DIE("couldn't delete $dir$file<br />");         }     }     closedir($mydir); } destroy(PATH); echo 'all done.';

Dopuna: 28 Mar 2011 14:21

Izbacuje mi grešku
Warning: opendir(../imgs/$id/) [function.opendir]: failed to open dir: No such file or directory in

offline
  • Pridružio: 21 Apr 2007
  • Poruke: 98

Kod gore koji sam stavio savršeno radi. Ja ga koristim za brisanje cache fajlova.

offline
  • Pridružio: 30 Avg 2008
  • Poruke: 91

Napisano: 28 Mar 2011 15:24

Ok evo cele delete.php stranice bez unlink funkcije, gde da stavim tvoj kod. Želim da izbriše sve slike unutar foldera $id

Hvala na pomoći

<?php require_once('../Connections/configuration.php'); ?> <?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; } } $colname_slike = "-1"; if (isset($_GET['id'])) {   $colname_slike = $_GET['id']; } mysql_select_db($database_configuration, $configuration); $query_slike = sprintf("SELECT * FROM products WHERE id = %s", GetSQLValueString($colname_slike, "int")); $slike = mysql_query($query_slike, $configuration) or die(mysql_error()); $row_slike = mysql_fetch_assoc($slike); $totalRows_slike = mysql_num_rows($slike); if ((isset($_GET['id'])) && ($_GET['id'] != "")) {   $deleteSQL = sprintf("DELETE FROM products WHERE id=%s",                        GetSQLValueString($_GET['id'], "int"));   mysql_select_db($database_configuration, $configuration);   $Result1 = mysql_query($deleteSQL, $configuration) or die(mysql_error());   $deleteGoTo = "accommodations.php";   if (isset($_SERVER['QUERY_STRING'])) {     $deleteGoTo .= (strpos($deleteGoTo, '?')) ? "&" : "?";     $deleteGoTo .= $_SERVER['QUERY_STRING'];       }   header(sprintf("Location: %s", $deleteGoTo)); } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Delete accommodation</title> </head> <body> </body> </html> <?php mysql_free_result($slike); ?>

Dopuna: 28 Mar 2011 15:49

Ili bi možda bilo lakše da ne koristim dreamweaverov kod

Ko je trenutno na forumu
 

Ukupno su 551 korisnika na forumu :: 9 registrovanih, 2 sakrivenih i 540 gosta   ::   [ Administrator ] [ Supermoderator ] [ Moderator ] :: Detaljnije

Najviše korisnika na forumu ikad bilo je 3466 - dana 01 Jun 2021 17:07

Korisnici koji su trenutno na forumu:
Korisnici trenutno na forumu: boris.zic, ikan, Istman, kihot, MB120mm, samsung, SR-3m, suton, yrraf