Delete (unlink) problem!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Morteneistrom@gmail.com

    Delete (unlink) problem!

    Im a complete n00b at PHP so please forgive me if this i a stupid
    question
    Why wont the following work.

    PHP Code:

    <?php
    foreach (glob('../images/profil/*.jpg') as $filename) {
    echo "$filename <br>". "<a href="unlink('$ filename');">De lete</
    a>";
    }
    ?>

    I want a list of files in the dir and a link next to each file that
    deletes the file if clicked.
    Any hints or solutions are very much appreciated.

    -Effix

  • Krustov

    #2
    Re: Delete (unlink) problem!

    <comp.lang.ph p>
    <>
    <Thu, 7 Aug 2008 09:32:20 -0700 (PDT)>
    <8ef1a638-475f-412b-b54a-d756658110fd@p2 5g2000hsf.googl egroups.com>
    Im a complete n00b at PHP so please forgive me if this i a stupid
    question
    Why wont the following work.
    >
    PHP Code:
    >
    <?php
    foreach (glob('../images/profil/*.jpg') as $filename) {
    echo "$filename <br>". "<a href="unlink('$ filename');">De lete</
    a>";
    }
    ?>
    >
    I want a list of files in the dir and a link next to each file that
    deletes the file if clicked.
    Any hints or solutions are very much appreciated.
    >
    Although the below code is intended to remove .php files - it could be
    adapted to use .jpg files instead .

    Remove the # character in front of the print "" stuff to display the
    filenames in your browser .

    IMHO ignore and resist the temptation to delete the .jpg files to start
    with as your first task is .....

    (1) grab the .jpg files in the folder
    (2) display the .jpg filename - with a clickable link to DELETE it



    <?php

    $batman=0;

    $dir="images/"; $joker=opendir( $dir);
    while (false!==($boyw onder=readdir($ joker)))
    {
    $pisky=1;
    if ($boywonder==". ") {$pisky=0;}
    if ($boywonder==". .") {$pisky=0;}
    if ($pisky==1) {$files[]=$boywonder; $batman=$batman +1;}
    }
    closedir($joker );

    $temp=0; $spock=0;
    while ($temp<$batman)
    {
    $demo=$files[$temp];
    # print "demo = $demo <br>";
    $qaz=strpos($de mo,"_time.php") ;
    if ($qaz==true)
    {
    $jack=strlen($d emo);
    $film1=substr($ demo,0,$jack-9);
    $film2=$demo;
    $spock=$spock+1 ;
    $kook1[$spock]=$film1;
    $kook2[$spock]=$film2;
    }
    $temp=$temp+1;
    }

    # print "<br><br>";

    if ($spock>0)
    {
    $yard=1;
    while ($yard<$spock+1 )
    {
    # print "$yard = $kook1[$yard] <br>";
    # print "$yard = $kook2[$yard] <br>";
    $filename="imag es/" . $kook2[$yard]; $fp=fopen($file name,"r");
    $qaz=fgets($fp) ; $tempt=trim($qa z); fclose($fp);
    $kom=time();
    if ($kom>$tempt)
    {
    $gort="images/" . $kook1[$yard] . ".jpg"; unlink($gort);
    $gort="images/" . $kook1[$yard] . "_temp.jpg" ; unlink($gort);
    $gort="images/" . $kook1[$yard] . ".php"; unlink($gort);
    $gort="images/" . $kook1[$yard] . "_time.php" ; unlink($gort);
    }
    $yard=$yard+1;
    }
    }

    ?>

    Comment

    • Morteneistrom@gmail.com

      #3
      Re: Delete (unlink) problem!

      On 7 Aug., 18:59, Krustov <m...@privacy.n etwrote:
      <comp.lang.ph p>
      <>
      <Thu, 7 Aug 2008 09:32:20 -0700 (PDT)>
      <8ef1a638-475f-412b-b54a-d75665811...@p2 5g2000hsf.googl egroups.com>
      >
      >
      >
      Im a complete n00b at PHP so please forgive me if this i a stupid
      question
      Why wont the following work.
      >
      PHP Code:
      >
      <?php
      foreach (glob('../images/profil/*.jpg') as $filename) {
      echo "$filename <br>". "<a href="unlink('$ filename');">De lete</
      a>";
      }
      ?>
      >
      I want a list of files in the dir and a link next to each file that
      deletes the file if clicked.
      Any hints or solutions are very much appreciated.
      >
      Although the below code is intended to remove .php files - it could be
      adapted to use .jpg files instead .
      >
      Remove the # character in front of the print "" stuff to display the
      filenames in your browser .
      >
      IMHO ignore and resist the temptation to delete the .jpg files to start
      with as your first task is .....
      >
      (1) grab the .jpg files in the folder
      (2) display the .jpg filename - with a clickable link to DELETE it
      >
      <?php
      >
      $batman=0;
      >
      $dir="images/"; $joker=opendir( $dir);
      while (false!==($boyw onder=readdir($ joker)))
      {
      $pisky=1;
      if ($boywonder==". ") {$pisky=0;}
      if ($boywonder==". .") {$pisky=0;}
      if ($pisky==1) {$files[]=$boywonder; $batman=$batman +1;}}
      >
      closedir($joker );
      >
      $temp=0; $spock=0;
      while ($temp<$batman)
      {
      $demo=$files[$temp];
      # print "demo = $demo <br>";
      $qaz=strpos($de mo,"_time.php") ;
      if ($qaz==true)
      {
      $jack=strlen($d emo);
      $film1=substr($ demo,0,$jack-9);
      $film2=$demo;
      $spock=$spock+1 ;
      $kook1[$spock]=$film1;
      $kook2[$spock]=$film2;
      >
      }
      $temp=$temp+1;
      }
      >
      # print "<br><br>";
      >
      if ($spock>0)
      {
      $yard=1;
      while ($yard<$spock+1 )
      {
      # print "$yard = $kook1[$yard] <br>";
      # print "$yard = $kook2[$yard] <br>";
      $filename="imag es/" . $kook2[$yard]; $fp=fopen($file name,"r");
      $qaz=fgets($fp) ; $tempt=trim($qa z); fclose($fp);
      $kom=time();
      if ($kom>$tempt)
      {
      $gort="images/" . $kook1[$yard] . ".jpg"; unlink($gort);
      $gort="images/" . $kook1[$yard] . "_temp.jpg" ; unlink($gort);
      $gort="images/" . $kook1[$yard] . ".php"; unlink($gort);
      $gort="images/" . $kook1[$yard] . "_time.php" ; unlink($gort);
      >
      }
      $yard=$yard+1;
      }
      }
      >
      ?>
      Thank you for your reply. But I cant seem to get your code to work.
      And also it seems to be a bit too complex for the fairly simple
      function that I want.
      The code that I have works fine when I remove the delete link.
      <?php
      foreach (glob('../images/profil/*.jpg') as $filename) {
      echo "$filename <br>";
      ?>

      This gives me a nice list of the jpg files in my folder.
      All I need to to is to add a link to the that activates
      unlink('$filena me')
      How do I do this?

      Comment

      • macca

        #4
        Re: Delete (unlink) problem!

        Hi krustov, did you write this code? If so have you ever heard of
        using meaningful variable names? It makes code much easier to read/
        understand/manipulate etc.

        Maybe you should read this...


        Comment

        • Geoff Berrow

          #5
          Re: Delete (unlink) problem!

          Message-ID:
          <c955e1f5-f5b3-4916-a9e5-69a1d9fe25c7@l6 4g2000hse.googl egroups.comfrom
          Morteneistrom@g mail.com contained the following:
          >Thank you for your reply. But I cant seem to get your code to work.
          >And also it seems to be a bit too complex for the fairly simple
          >function that I want.
          >The code that I have works fine when I remove the delete link.
          ><?php
          >foreach (glob('../images/profil/*.jpg') as $filename) {
          echo "$filename <br>";
          >?>
          >
          >This gives me a nice list of the jpg files in my folder.
          >All I need to to is to add a link to the that activates
          >unlink('$filen ame')
          >How do I do this?
          You really don't want to do this. Let's say you have a page containing
          links that delete files. Let's suppose you accidentally let that get
          spidered by a search engine. What happens when the SE dutifully follows
          the links?

          I'd use a form and place a checkbox next to each image, something like
          echo "<input type='checkbox' name ='del[]' value='$filenam e'>";

          The 'del[]' construct makes sure that the filenames are conveniently
          placed in an array.

          Then have a submit (<input type='submit' name='delete' value=Delete all
          checked boxes'>delete) button somewhere.

          Assuming the page is calling itself you would then do something like

          <?php
          if(isset($_POST['delete'])){
          foreach ($_POST['del'] as $filename){
          if(unlink($file name)){
          echo "$filename deleted!<br>";
          }
          }
          }
          ?>

          Untested.

          In practice I'd probably store the names of the deleted files in a
          session variable and have an interim confirmation screen. And I'd also
          password protect my delete script. But you get the idea.





          --
          Geoff Berrow 011000100110110 0010000000110
          001101101011011 001000110111101 100111001011
          100110001101101 111001011100111 010101101011
          The Slippery Hill Boys Tel: 07985 425932. American themed barn dances and bluegrass performances. Stoke on Trent, Newcastle under Lyme, Staffordshire, Cheshire and surrounding areas.

          Comment

          • Krustov

            #6
            Re: Delete (unlink) problem!

            <comp.lang.ph p>
            <>
            <Thu, 7 Aug 2008 10:23:29 -0700 (PDT)>
            <c955e1f5-f5b3-4916-a9e5-69a1d9fe25c7@l6 4g2000hse.googl egroups.com>
            Thank you for your reply. But I cant seem to get your code to work.
            And also it seems to be a bit too complex for the fairly simple
            function that I want.
            The code that I have works fine when I remove the delete link.
            <?php
            foreach (glob('../images/profil/*.jpg') as $filename) {
            echo "$filename <br>";
            ?>
            >
            This gives me a nice list of the jpg files in my folder.
            All I need to to is to add a link to the that activates
            unlink('$filena me')
            How do I do this?
            >
            Part of the learning process is tinkering .

            Strip away the bottom two thirds of the code - and then add a little bit
            at a time .

            That means ..... simply adapting some code you have that already works
            isnt always suitable ..... and in some cases when you want to achieve a
            certain effect ..... then you dont have a choice but to learn some new
            techniques ..... or do without .

            So , Your first task is to GRAB the list of filenames from the directory
            and then DISPLAY them in the browser .

            But if your petty little brain says 'but i dont want to do that' then i
            suggest you surf www.magicwand.com as it offers a one click solution to
            every problem known to mankind .

            Comment

            • macca

              #7
              Re: Delete (unlink) problem!

              Hi try something like this (untested code), might help:


              <?php
              foreach (glob('../images/profil/*.jpg') as $filename) {
              echo $filename.' <a href="'.$_SERVE R['PHP_SELF'].'?delete_img=' .
              $filename.'">De lete '.$filename.'</a><br>';
              ?>


              Then have something like

              <?php

              function del_file($dir,$ filename){

              if (file_exists($d ir.'/'.$filename)){
              unlink($dir.'/'.$filename);
              echo $filename.' was deleted';
              }

              }


              $dir = '../images/profil/';
              if (isset($_GET['delete_img'])){
              del_file($dir,$ _GET['delete_img']);

              }

              ?>

              Comment

              • Morteneistrom@gmail.com

                #8
                Re: Delete (unlink) problem!

                On 7 Aug., 19:52, macca <ptmcna...@goog lemail.comwrote :
                Hi try something like this (untested code), might help:
                >
                <?php
                foreach (glob('../images/profil/*.jpg') as $filename) {
                echo $filename.' <a href="'.$_SERVE R['PHP_SELF'].'?delete_img=' .
                $filename.'">De lete '.$filename.'</a><br>';
                ?>
                >
                Then have something like
                >
                <?php
                >
                function del_file($dir,$ filename){
                >
                if (file_exists($d ir.'/'.$filename)){
                unlink($dir.'/'.$filename);
                echo $filename.' was deleted';
                >
                }
                }
                >
                $dir = '../images/profil/';
                if (isset($_GET['delete_img'])){
                del_file($dir,$ _GET['delete_img']);
                >
                }
                >
                ?>
                Thank you for your reply macca but I already got it working. I
                appreciate the time you took to help me.
                Thank you
                - Effix

                Comment

                • Effix

                  #9
                  Re: Delete (unlink) problem!

                  On 7 Aug., 19:39, Geoff Berrow <blthe...@ckdog .co.ukwrote:
                  Message-ID:
                  <c955e1f5-f5b3-4916-a9e5-69a1d9fe2...@l6 4g2000hse.googl egroups.comfrom
                  Morteneist...@g mail.com contained the following:
                  >
                  Thank you for your reply. But I cant seem to get your code to work.
                  And also it seems to be a bit too complex for the fairly simple
                  function that I want.
                  The code that I have works fine when I remove the delete link.
                  <?php
                  foreach (glob('../images/profil/*.jpg') as $filename) {
                  echo "$filename <br>";
                  ?>
                  >
                  This gives me a nice list of the jpg files in my folder.
                  All I need to to is to add a link to the that activates
                  unlink('$filena me')
                  How do I do this?
                  >
                  You really don't want to do this. Let's say you have a page containing
                  links that delete files. Let's suppose you accidentally let that get
                  spidered by a search engine. What happens when the SE dutifully follows
                  the links?
                  >
                  I'd use a form and place a checkbox next to each image, something like
                  echo "<input type='checkbox' name ='del[]' value='$filenam e'>";
                  >
                  The 'del[]' construct makes sure that the filenames are conveniently
                  placed in an array.
                  >
                  Then have a submit (<input type='submit' name='delete' value=Delete all
                  checked boxes'>delete) button somewhere.
                  >
                  Assuming the page is calling itself you would then do something like
                  >
                  <?php
                  if(isset($_POST['delete'])){
                  foreach ($_POST['del'] as $filename){
                  if(unlink($file name)){
                  echo "$filename deleted!<br>";
                  }
                  }}
                  >
                  ?>
                  >
                  Untested.
                  >
                  In practice I'd probably store the names of the deleted files in a
                  session variable and have an interim confirmation screen. And I'd also
                  password protect my delete script. But you get the idea.
                  >
                  --
                  Geoff Berrow 011000100110110 0010000000110
                  001101101011011 001000110111101 100111001011
                  100110001101101 111001011100111 010101101011htt p://slipperyhill.co .uk
                  Thanks very much for the help Geoff! worked like a charm.
                  -Effix

                  Comment

                  • Geoff Berrow

                    #10
                    Re: Delete (unlink) problem!

                    Message-ID:
                    <2ca666a1-317d-43f5-874d-703fa01b99e9@k3 7g2000hsf.googl egroups.comfrom
                    macca contained the following:
                    ><?php
                    >foreach (glob('../images/profil/*.jpg') as $filename) {
                    echo $filename.' <a href="'.$_SERVE R['PHP_SELF'].'?delete_img=' .
                    >$filename.'">D elete '.$filename.'</a><br>';
                    >?>

                    Same problem. If the page gets spidered, the files are toast.
                    --
                    Geoff Berrow 011000100110110 0010000000110
                    001101101011011 001000110111101 100111001011
                    100110001101101 111001011100111 010101101011
                    The Slippery Hill Boys Tel: 07985 425932. American themed barn dances and bluegrass performances. Stoke on Trent, Newcastle under Lyme, Staffordshire, Cheshire and surrounding areas.

                    Comment

                    • Michael Fesser

                      #11
                      Re: Delete (unlink) problem!

                      ..oO(Geoff Berrow)
                      >Message-ID:
                      ><c955e1f5-f5b3-4916-a9e5-69a1d9fe25c7@l6 4g2000hse.googl egroups.comfrom
                      >Morteneistrom@ gmail.com contained the following:
                      >
                      >>This gives me a nice list of the jpg files in my folder.
                      >>All I need to to is to add a link to the that activates
                      >>unlink('$file name')
                      >>How do I do this?
                      >
                      >You really don't want to do this. Let's say you have a page containing
                      >links that delete files. Let's suppose you accidentally let that get
                      >spidered by a search engine. What happens when the SE dutifully follows
                      >the links?
                      Indeed.

                      And it doesn't have to be a SE. It can even be your own browser that
                      wipes out your files, simply by prefetching some of the following links
                      in the background while you're still watching the overview page. Using
                      the GET method to directly trigger modifications on the server without
                      a POST confirmation is _very_ dangerous.
                      >I'd use a form and place a checkbox next to each image, something like
                      >echo "<input type='checkbox' name ='del[]' value='$filenam e'>";
                      >
                      >The 'del[]' construct makes sure that the filenames are conveniently
                      >placed in an array.
                      >
                      >Then have a submit (<input type='submit' name='delete' value=Delete all
                      >checked boxes'>delete) button somewhere.
                      Yep. That's the more secure way.

                      Micha

                      Comment

                      • Krustov

                        #12
                        Re: Delete (unlink) problem!

                        <comp.lang.ph p>
                        <Michael Fesser>
                        <Thu, 07 Aug 2008 21:34:18 +0200>
                        <i7jm94lh8vjidk r0mvnhne752ahs0 55hhi@4ax.com>
                        You can safely ignore the Krusty clown.
                        >
                        RTFM .

                        But if it wasnt for krusty - then how many people would only have
                        replied with the above .

                        Or perhaps mr fesser is going to claim that nobody on comp.lang.php
                        would ever say such a thing to a newbie user who asked for help .

                        Comment

                        • Jerry Stuckle

                          #13
                          Re: Delete (unlink) problem!

                          Krustov wrote:
                          <comp.lang.ph p>
                          <Michael Fesser>
                          <Thu, 07 Aug 2008 21:34:18 +0200>
                          <i7jm94lh8vjidk r0mvnhne752ahs0 55hhi@4ax.com>
                          >
                          >You can safely ignore the Krusty clown.
                          >>
                          >
                          RTFM .
                          >
                          But if it wasnt for krusty - then how many people would only have
                          replied with the above .
                          >
                          Or perhaps mr fesser is going to claim that nobody on comp.lang.php
                          would ever say such a thing to a newbie user who asked for help .
                          >
                          >
                          You have a very warped opinion of your own self worth. A legend (only)
                          in your own mind.

                          --
                          =============== ===
                          Remove the "x" from my email address
                          Jerry Stuckle
                          JDS Computer Training Corp.
                          jstucklex@attgl obal.net
                          =============== ===

                          Comment

                          • Michael Fesser

                            #14
                            Re: Delete (unlink) problem!

                            ..oO(Jerry Stuckle)
                            >Krustov wrote:
                            >
                            >But if it wasnt for krusty - then how many people would only have
                            >replied with the above .
                            >>
                            >Or perhaps mr fesser is going to claim that nobody on comp.lang.php
                            >would ever say such a thing to a newbie user who asked for help .
                            >>
                            >>
                            >
                            >You have a very warped opinion of your own self worth. A legend (only)
                            >in your own mind.
                            Maybe OLPC was not the best idea. ;)

                            Micha

                            Comment

                            • Jerry Stuckle

                              #15
                              Re: Delete (unlink) problem!

                              Michael Fesser wrote:
                              .oO(Jerry Stuckle)
                              >
                              >Krustov wrote:
                              >>
                              >>But if it wasnt for krusty - then how many people would only have
                              >>replied with the above .
                              >>>
                              >>Or perhaps mr fesser is going to claim that nobody on comp.lang.php
                              >>would ever say such a thing to a newbie user who asked for help .
                              >>>
                              >>>
                              >You have a very warped opinion of your own self worth. A legend (only)
                              >in your own mind.
                              >
                              Maybe OLPC was not the best idea. ;)
                              >
                              Micha
                              >
                              Oh, I think it is. But some people need to grow enough to be classified
                              as a child. :-)

                              --
                              =============== ===
                              Remove the "x" from my email address
                              Jerry Stuckle
                              JDS Computer Training Corp.
                              jstucklex@attgl obal.net
                              =============== ===

                              Comment

                              Working...