12 Replies - 303 Views - Last Post: 01 March 2011 - 02:15 PM Rate Topic: -----

#1 Keleko  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 21
  • Joined: 17-February 11

Deleting $FILE

Posted 01 March 2011 - 12:39 PM

i'm trying to use a reference from another page to delete a file off the server but with the filename being unknown aka $FILE. having trouble figuring out the code and how it works. i thought the href would push it over but to me the script just doesnt look right.

echo $FILE . "<a href='/delete.php?name=$FILE'>delete</a>"


delete.php
<?php

$fileToRemove = '/uploads/family/$FILE';
if (file_exists($fileToRemove)) { 

   if (@unlink($fileToRemove) === true) {
      echo "image removed";
   } else {
      echo "can not delete";
   }
}
?>



Is This A Good Question/Topic? 0
  • +

Replies To: Deleting $FILE

#2 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 994
  • View blog
  • Posts: 3,547
  • Joined: 05-June 09

Re: Deleting $FILE

Posted 01 March 2011 - 12:47 PM

It doesn't/can't/won't work.

Where did you get this from?

If you're passing over ?name=whatever, you need to access it in your delete.php script via $_GET['name'].

This post has been edited by RudiVisser: 01 March 2011 - 12:48 PM

Was This Post Helpful? 2
  • +
  • -

#3 Keleko  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 21
  • Joined: 17-February 11

Re: Deleting $FILE

Posted 01 March 2011 - 12:59 PM

I made the majority of it myself, kinda new to this but still shoulda known i needed to do that, might $_REQUEST also work? maybe a little bit more efficiently, or? kind of like
$image = $_REQUEST['$FILE'];

if (file_exists($image))
{
  unlink ($image);
  echo "File deleted";
}
else
{
  echo "no file found";
}


Was This Post Helpful? 0
  • +
  • -

#4 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 994
  • View blog
  • Posts: 3,547
  • Joined: 05-June 09

Re: Deleting $FILE

Posted 01 March 2011 - 01:10 PM

$_REQUEST would work for the simple fact that it's a compilation of $_GET, $_POST and $_COOKIE.

However, your code wouldn't work, I'm curious where did $FILE come from in the first place?

It's not been defined so won't do anything in the code you've provided! :(
Was This Post Helpful? 0
  • +
  • -

#5 Keleko  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 21
  • Joined: 17-February 11

Re: Deleting $FILE

Posted 01 March 2011 - 01:15 PM

it is from previous script, to show the files and each file then has a href.
while ($FILE = readdir ($handle) !=FALSE)
{
   echo etc.
}


This post has been edited by Keleko: 01 March 2011 - 01:15 PM

Was This Post Helpful? 0
  • +
  • -

#6 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 994
  • View blog
  • Posts: 3,547
  • Joined: 05-June 09

Re: Deleting $FILE

Posted 01 March 2011 - 01:26 PM

I see, but in that code, the expression within the brackets for while() is being executed, so $FILE is actually being set on every iteration.

In your new code, $FILE means absolutely nothing.

I assume that this is within your while, am I right?
echo $FILE . "<a href='/delete.php?name=$FILE'>delete</a>"


If so, leave it as is.

Your code within delete.php should look something like this:
<?php
$imagePath = $_GET['name'];

if (file_exists($imagePath))
{
  unlink($imagePath);
  echo "File deleted";
}
else
{
  echo "no file found";
}
?>


Does that help at all? :)
Was This Post Helpful? 1
  • +
  • -

#7 Keleko  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 21
  • Joined: 17-February 11

Re: Deleting $FILE

Posted 01 March 2011 - 01:33 PM

yes, thank you :)
the first part is during the while yes. i think most of my problem was i should of remembered the $_GET, kinda feel stupid about that one XD but thanks for reminding me!
Was This Post Helpful? 0
  • +
  • -

#8 Keleko  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 21
  • Joined: 17-February 11

Re: Deleting $FILE

Posted 01 March 2011 - 01:40 PM

ouch, didnt work on the server will have to do another
opendir()

Was This Post Helpful? 0
  • +
  • -

#9 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 994
  • View blog
  • Posts: 3,547
  • Joined: 05-June 09

Re: Deleting $FILE

Posted 01 March 2011 - 01:42 PM

What is it that didn't work on the server exactly?? Was there an error??

I see no way in which opendir would help.
Was This Post Helpful? 0
  • +
  • -

#10 Keleko  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 21
  • Joined: 17-February 11

Re: Deleting $FILE

Posted 01 March 2011 - 01:51 PM

Fatal error: Function name must be a string in /www/zymichost.com/s/p/a/sparks/htdocs/delete.php on line 4


<?php

$imagePath = $_GET('name');



line 4
Was This Post Helpful? 0
  • +
  • -

#11 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 994
  • View blog
  • Posts: 3,547
  • Joined: 05-June 09

Re: Deleting $FILE

Posted 01 March 2011 - 01:53 PM

The issue with that is that you're using ( and ), ie. brackets.

You're accessing an array and as such need to use square brackets as I demonstrated ([ and ]).

EDIT: That shouldn't work locally either!

This post has been edited by RudiVisser: 01 March 2011 - 01:59 PM

Was This Post Helpful? 1
  • +
  • -

#12 Keleko  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 21
  • Joined: 17-February 11

Re: Deleting $FILE

Posted 01 March 2011 - 02:00 PM

thanks you were right, no more error but also no echo or action is done. i'll spend some time messing around to see if i can get it to work.
Was This Post Helpful? 0
  • +
  • -

#13 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 994
  • View blog
  • Posts: 3,547
  • Joined: 05-June 09

Re: Deleting $FILE

Posted 01 March 2011 - 02:15 PM

That's strange, there's only 1 execution path that the code can take.

Have you tried an echo before you even set the $imagePath variable?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1