Jump to content

Recommended Posts

This administrator page displays a list of client slideshows with a delete checkbox at the beginning of each row.  Upon submission the script deletes the folder containing the slideshow, deletes an image in a different folder, and removes the record from the database; then it reloads itself showing the current listings.  I'm having a problem deleting the folder and image.  I think the problem is happening with the $folder and $image variable assignments.  To test it I tried to echo $folder and $image and got nothing which would explain why I'm getting errors with paths not existing.  I'm sure the answer's right there in front of me, I'm just not seeing it.  Thanks in advance.

 

<?php
            // If 'delbtn' clicked then delete row, folder and image then show new slideshow listing.
              if(isset($_POST['delbtn'])){
                // Loop through check boxes
                  foreach($_POST['deletes'] as $id){
			    mysql_query("SELECT FROM SLIDESHOW foldername, image_name WHERE slide_id='$id'");
			    $folder='foldername';
			    $image='image_name';
                    $folderPath = implode(DIRECTORY_SEPARATOR, array('..','client','$folder'));
				$imagePath = implode(DIRECTORY_SEPARATOR, array('..','client_images','$image'));
				echo $folder;
				echo $image_name;
                    // Delete folder and contents.
				if (!is_dir('$folderPath')) {
				 echo 'Folder $folder does not exist.  ';
				}else{
                      rmdir("$folderPath") or die( "Unable To Delete Folder.  Please Try Again Later" );
				}
                    // Delete client image.
                    if(!is_file("$imagePath")){
				 echo 'Client image $image does not exist';
				}else{
				  unlink("$image") or die( "Unable To Delete File. Please Try Again Later" );
                    }
                    //  Delete row from database
                    mysql_query("DELETE FROM Slideshow WHERE slide_id='$id'");           
			  }
              }
              // SQL query
              $query="SELECT * FROM Slideshow";
              $result=mysql_query($query) or die('Query failed: ' . mysql_error());	
              // Get number of rows
              $num_rows = mysql_num_rows($result);
              // Error message for empty DB else show results
              if ($num_rows == 0){
                $errors[] = "Database does not contain slideshow listings!<br /><br />
                             <button type=\"submit\" onClick=\"location.href='slideshow_new.php'\">Add Slideshow</button>";
              } else {
           ?>	
[code]


Here's the entire code if you're interested.
[code]
<?php
            // If 'delbtn' clicked then delete row, folder and image then show new slideshow listing.
              if(isset($_POST['delbtn'])){
                // Loop through check boxes
                  foreach($_POST['deletes'] as $id){
			    mysql_query("SELECT FROM SLIDESHOW foldername, image_name WHERE slide_id='$id'");
			    $folder='foldername';
			    $image='image_name';
                    $folderPath = implode(DIRECTORY_SEPARATOR, array('..','client','$folder'));
				$imagePath = implode(DIRECTORY_SEPARATOR, array('..','client_images','$image'));
				echo $folder;
				echo $image_name;
                    // Delete folder and contents.
				if (!is_dir('$folderPath')) {
				 echo 'Folder $folder does not exist.  ';
				}else{
                      rmdir("$folderPath") or die( "Unable To Delete Folder.  Please Try Again Later" );
				}
                    // Delete client image.
                    if(!is_file("$imagePath")){
				 echo 'Client image $image does not exist';
				}else{
				  unlink("$image") or die( "Unable To Delete File. Please Try Again Later" );
                    }
                    //  Delete row from database
                    mysql_query("DELETE FROM Slideshow WHERE slide_id='$id'");           
			  }
              }
              // SQL query
              $query="SELECT * FROM Slideshow";
              $result=mysql_query($query) or die('Query failed: ' . mysql_error());	
              // Get number of rows
              $num_rows = mysql_num_rows($result);
              // Error message for empty DB else show results
              if ($num_rows == 0){
                $errors[] = "Database does not contain slideshow listings!<br /><br />
                             <button type=\"submit\" onClick=\"location.href='slideshow_new.php'\">Add Slideshow</button>";
              } else {
           ?>				
           <form action="slideshow_list.php" method="post" class="clearfix">
             <fieldset class="clearfix">
               <legend> Slideshow List </legend>
               <?php
                 // Print results in HTML
                 // Number of records
                 echo "<p class=\"indent\">Slideshow count = $num_rows</p>\n";
               ?>
               <table border="0" cellpadding="4" cellspacing="4" align="center">
                 <tr>
                   <th align=center>Delete</th>
                   <th>ID</th>
                   <th>Username</th>
                   <th>Folder</th>
                   <th>Active</th>
                   <th>Date</th>
                   <th>Image</th>
                  </tr>
               <?php while($row=mysql_fetch_array($result)){?>
                 <tr>
                   <td align=center><input type="checkbox" name= <?php echo "deletes[]" ?> id= <?php echo $row['slide_id'] ?> value=<?php echo $row['slide_id'] ?> /></td>
                   <td><?php echo $row['slide_id'] ?></td>
                   <td><?php echo $row['username'] ?></td>
                   <td><?php echo $row['foldername'] ?></td>
                   <td align="center"><?php echo $row['active'] ?></td>
                   <td><?php echo $row['session_date'] ?></td>
                   <td><?php echo $row['image_name'] ?></td>
                 </tr>
               <?php } ?>
                 <tr>
			    <td><input class="delete" type="submit" name="delbtn" value="Delete" /></td>
                 </tr>
               </table>
             </fieldset>
           </form>
           <?php }  
             if(count($errors) > 0){ 
           ?>
               <fieldset class="clearfix">
                 <legend>Errors</legend>
            <?php
                echo "<div class=\"indent\"><br /><p class=\"bodyText\">The following errors have occured:</p></div>";
                echo "<div class=\"indent\"><p class=\"error\">";
                foreach($errors AS $error){
                  echo $error . "\n";
                }
                echo "</p></div>";							
             //we use javascript to go back rather than reloading the page 
             // so the user doesn't have to type in all that info again.
              }
             //close DB
             mysql_close();
           ?>

Link to comment
https://forums.phpfreaks.com/topic/187034-problems-deleting-file-and-folder/
Share on other sites

You are echo'ing the wrong values. You are hard codng the vlaues of $image and $folder

        $folder='foldername';
        $image='image_name';

 

Tghe values you need to be checking are $folderPath and $imagePath. And, if $image and $folder do not have any values then something isn't right. I suspect that they are not displaying because the HTML is fouled up. It is really hard to follow your code because it is constantly going in and out of the PHP and HTML.

I mispoke when I said I got nothing from echoing $folder and $image.  What I'm getting is foldername and image_name.  Hard coding is not my intent.  I want to select 'foldername' and 'image_name' values from the db according to the selected id.

 

There are a few things wrong here. First is your query:

 

                mysql_query("SELECT FROM SLIDESHOW foldername, image_name WHERE slide_id='$id'");

 

Should be:

                $res = mysql_query("SELECT foldername, image_name FROM SLIDESHOW WHERE slide_id='$id'");

 

Now the assignment of the folder and image name

 

                $folder=mysql_result($res, 0,0);
                $image=mysql_result($res, 1,0);

 

Now you are trying to use variables inside of single quotes. When using $variable inside of single quotes they are taken literally and will display like that. So since you are using this on its own I would remove the single quotes (you could also replace them with double quotes).

 

           $folderPath = implode(DIRECTORY_SEPARATOR, array('..','client', $folder));
               $imagePath = implode(DIRECTORY_SEPARATOR, array('..','client_images',$image));
               echo $folder;
               echo $image_name;
                    // Delete folder and contents.
               if (!is_dir($folderPath)) {

 

I have not fully looked at the rest of your code, but hopefully that gets you going on what you need to work on / change.

:D Last month I was crawling.  Today I feel like I'm  standing.  Thanks for the help.  Makes a lot of sense!!  I had to change

<?php
$folder=mysql_result($res, 0, 0);
$image=mysql_result($res, 1, 0);
?>

with

<?php
$folder=mysql_result($res, 0, "foldername");
$image=mysql_result($res, 0, "image_name");
?>

Everything seems to be working fine.  I'll close this post once I'm sure all the bugs have been worked out.  Thanks again!!

 

Here's the script if anyone's interested.

<?php
            // If 'delbtn' clicked then delete row, folder and image then show new slideshow listing.
              if(isset($_POST['delbtn'])){
                // Loop through check boxes
                  foreach($_POST['deletes'] as $id){
                    $res = mysql_query("SELECT foldername, image_name FROM Slideshow WHERE slide_id='$id'");
                    $folder=mysql_result($res, 0, "foldername");
                    $image=mysql_result($res, 0, "image_name");
                    $folderPath = implode(DIRECTORY_SEPARATOR, array('..','client', $folder));
                    $imagePath = implode(DIRECTORY_SEPARATOR, array('..','client_images', $image));
                    // Delete folder and contents.
                   if (!is_dir($folderPath)) {
                    echo 'Folder path does not exist.  ';
                  }else{
                    rmdir($folderPath) or die( "Unable To Delete Folder.  Please Try Again Later" );
                  }
                    // Delete client image.
                    if(!is_file($imagePath)){
                     echo 'Client image path does not exist';
                    }else{
                      unlink($imagePath) or die( "Unable To Delete File. Please Try Again Later" );
                    }
                    //  Delete row from database
                    mysql_query("DELETE FROM Slideshow WHERE slide_id='$id'");           
			  }
              }
              // SQL query
              $query="SELECT * FROM Slideshow";
              $result=mysql_query($query) or die('Query failed: ' . mysql_error());	
              // Get number of rows
              $num_rows = mysql_num_rows($result);
              // Error message for empty DB else show results
              if ($num_rows == 0){
                $errors[] = "Database does not contain slideshow listings!<br /><br />
                             <button type=\"submit\" onClick=\"location.href='slideshow_new.php'\">Add Slideshow</button>";
              } else {
           ?>				
           <form action="slideshow_list.php" method="post" class="clearfix">
             <fieldset class="clearfix">
               <legend> Slideshow List </legend>
               <?php
                 // Print results in HTML
                 // Number of records
                 echo "<p class=\"indent\">Slideshow count = $num_rows</p>\n";
               ?>
               <table border="0" cellpadding="4" cellspacing="4" align="center">
                 <tr>
                   <th align=center>Delete</th>
                   <th>ID</th>
                   <th>Username</th>
                   <th>Folder</th>
                   <th>Active</th>
                   <th>Date</th>
                   <th>Image</th>
                  </tr>
               <?php while($row=mysql_fetch_array($result)){?>
                 <tr>
                   <td align=center><input type="checkbox" name= <?php echo "deletes[]" ?> id= <?php echo $row['slide_id'] ?> value=<?php echo $row['slide_id'] ?> /></td>
                   <td><?php echo $row['slide_id'] ?></td>
                   <td><?php echo $row['username'] ?></td>
                   <td><?php echo $row['foldername'] ?></td>
                   <td align="center"><?php echo $row['active'] ?></td>
                   <td><?php echo $row['session_date'] ?></td>
                   <td><?php echo $row['image_name'] ?></td>
                 </tr>
               <?php } ?>
                 <tr>
			    <td><input class="delete" type="submit" name="delbtn" value="Delete" /></td>
                 </tr>
               </table>
             </fieldset>
           </form>
           <?php }  
             if(count($errors) > 0){ 
           ?>
               <fieldset class="clearfix">
                 <legend>Errors</legend>
            <?php
                echo "<div class=\"indent\"><br /><p class=\"bodyText\">The following errors have occured:</p></div>";
                echo "<div class=\"indent\"><p class=\"error\">";
                foreach($errors AS $error){
                  echo $error . "\n";
                }
                echo "</p></div>";							
              }
             //close DB
             mysql_close();
           ?>

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.