Solved

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

Posted on 2011-09-09
6
209 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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

Independent Software Vendors: 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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

739 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