Solved

PHP - How to use a textbox and a button to remove a certain file?

Posted on 2011-09-09
6
208 Views
Last Modified: 2012-05-12
Hello everyone! The title may not be that clear what I'm trying to do. First of all, I have created multiple paths like this:

F:\MyFolder\1\testfile.txt
F:\MyFolder\2\testfile.txt
F:\MyFolder\3\testfile.txt

You notice that the only thing that changes in the path is the middle. Now my question. Can I use a textbox to determine the numbers 1, 2, 3 so If I for example type a 2 in the textbox and then click a button, the file
F:\MyFolder\2\testfile.txt will be removed. And if I type a 3 the file F:\MyFolder\3\testfile.txt will be removed.
Basically everything in the path stays the same only the second folder changes.

Thank you for your support,

einstein10
0
Comment
Question by:einstein10
  • 3
  • 2
6 Comments
 
LVL 17

Accepted Solution

by:
OmniUnlimited earned 500 total points
ID: 36515398
Sure.  You could set up PHP like this:

<?php

if (isset($_POST)) {
    $fileroot = "F:\MyFolder\";
    $filename = "\testfile.txt";
    $filepath = $fileroot . $_POST['fileindex'] . $filename;
    unlink($filepath);
}
echo '<form method="post" action="">';
echo '<label for "fileindex">Index of file to be deleted: <input type="text" name="fileindex" id="fileindex" value="' . $_POST['fileindex'] . '" />';
echo '<input type="submit" name="submit" value="Submit" />';
echo '</form>';
?>

Open in new window

0
 
LVL 17

Expert Comment

by:OmniUnlimited
ID: 36515400
Sorry, forgot to close the <label> tag on line 10:
 
echo '<label for "fileindex">Index of file to be deleted: <input type="text" name="fileindex" id="fileindex" value="' . $_POST['fileindex'] . '" /></label>';

Open in new window

0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 36515405
This is the form:

<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<input type="text" name="filenumber" value="" />
<input type="submit" name="submit" value="Submit" />
</form>

This isthe php code at the top of the same page:

<?php
if isset($_POSR['submit']){
  $number=$_POST['filenumber'];
  $fname="F:\MyFolder\" . $number . "\testfile.txt";
  unlink($fname);
  echo "File deleted."
}
?>

Cheers
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 31

Expert Comment

by:Marco Gasi
ID: 36515408
Oooops: didn't refresh...
0
 

Author Closing Comment

by:einstein10
ID: 36515495
Thank you so much! You have done a great job.
0
 
LVL 17

Expert Comment

by:OmniUnlimited
ID: 36515501
No problem.  Best of luck to you on your project!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question