Jump to content


Unlink() Function


6 replies to this topic

#1 Pooch

    Young Padawan

  • Members
  • Pip
  • 63 posts

Posted 04 March 2006 - 11:27 AM

I was wondering how one would give the address of a file to unlink() when the file is out of the directory of the php script.
Example:
I have a file (/public/index.php) that is including my script (/public/pages/script.php). The file (/public/pages/script.php) should be deleting a file in another directory (/public/records/record.doc). What URL would I put in the unlink() function?

I've tried this and it doesn't seem to work:
 $file = "record.doc";
  $filename = ereg_replace(" ", "%20", $file);
   $path = "../records/" . $filename;
   echo $path;
   chmod($path,0777);
   if (unlink($path)) {
	echo "<br>File deleted!";
   } else {
	echo "<br>Couldn't delete file.";
   }

Anyone know what $path should equal?

#2 Chaos King

    Senior Programmer

  • P2L Staff
  • PipPipPip
  • 676 posts
  • Gender:Male
  • Location:Florida

Posted 04 March 2006 - 11:39 AM

I have tested it myself in the same environment you described and it is working. Are you getting any errors what so all?

#3 Pooch

    Young Padawan

  • Members
  • Pip
  • 63 posts

Posted 05 March 2006 - 10:56 AM

All I get is "Couldn't delete file." I the script works fine when I'm not including it, but when I include it (use include "/public/pages/script.php" on "/public/index.php"), it doesn't work.

Edit: I got rid of the ../ and it worked when I tried to remove a .txt file. It doesn't work when I try to delete .doc's. Is this because I have a linux server?

Edited by Pooch, 05 March 2006 - 11:26 AM.


#4 Jamie Huskisson

    Retired P2L Staff

  • Members
  • PipPipPipPip
  • 3,648 posts
  • Gender:Male
  • Location:Nottingham, UK

Posted 05 March 2006 - 02:13 PM

your direct root address will more than likely be /home/username/public_html/ not /public_html/

#5 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 05 March 2006 - 03:08 PM

The script works when he doesn't include it because when it's not included, it has the right path the the directory. Keep in mind Jay, not everybody uses cPanel, so /public could be the right root directory for him.

When you include /public/pages/script.php into /public/index.php, it changes the file path, so /public/records is no longer above script.php, it's a folder below it.
http://www.pagetutor...e/lesson10.html <-- more info on file paths (for html, but the concept is universal).

Just keep in mind, when ever you include a file, the file thats being included (in this case, script.php), becomes part of the including file (index.php). So all references, variables, etc... are shared from the point of inclusion on.

Edited by rc69, 05 March 2006 - 03:09 PM.


#6 Pooch

    Young Padawan

  • Members
  • Pip
  • 63 posts

Posted 12 March 2006 - 09:36 AM

Is there a reason why unlink() wouldn't work for .doc's? I have a linux server, would this have anything to do with the problem? The script works fine for deleting .txt files, but it won't delete .doc.

Here's my code:
   $sql = mysql_query("SELECT * FROM records WHERE id=$id");
   $r = mysql_fetch_assoc($sql);
   $file  = $r["title"];

   $result = mysql_query($sql2);
   $filename = ereg_replace(" ", "%20", $file);
   $path = "records/" . $filename;
   echo "ID: " . $id . "<br>";
   echo "FILE: " . $file . "<br>";
   echo "FILENAME: " . $filename . "<br>";
   echo "PATH:" . $path . "<br>";
   chmod($path,0777);
   if (unlink($path)) {
	$sql2 = "DELETE FROM records WHERE id='$id'";
	mysql_query($sql2);
	echo "<br>Record deleted!<br><br>";
   } else {
	echo "<br>Couldn't delete file.<br><br>";
   }


#7 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 13 March 2006 - 04:13 PM

As far as i know unlink doesn't care what kind of file you're trying to delete. It could be the way the file was put on the server though.
If you FTP'd the .doc, and created the .txt via fopen, that could be one reason it won't work. But if you FTP'd them both, then it might be a permissions thing (either chmod, or ini set. If it's ini set, you're pretty much s-o-l).





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users