Avatar of nascent1
nascent1Flag for Canada asked on

delete file from server

The attached code removes the entry from the db correctly, but does not delete the file from the directory. The "URL" path is correctly stored in the db and the directory the file is stored in is set with permissions to 777. How do I make it so the file is deleted from the directory?
<?php
include($_SERVER['DOCUMENT_ROOT']."/admin/var.php");  
session_start();
if(isset( $_SESSION['diradmin']))
{
include($_SERVER['DOCUMENT_ROOT']."/admin/head-medlinks.php");  
if(isset($_POST['submit']))
  {
       $id=$_POST['id'];
 
      $get_file = mysql_query("SELECT URL FROM cms_medlinks_entries WHERE id=".$id);
      
      list($del_path) = mysql_fetch_row($get_file);
      
      unlink($del_path);
 
    $deletefile="DELETE from cms_medlinks_entries where id='$id'";
    mysql_query($deletefile) or die("Could not delete file");
 
	print "Entry Deleted";
      
       }
	   else if(isset($_GET['id']))
  {
    $id=$_GET['id'];
    print "<form action='deletefile.php' method='post' id='default' class='box'>";
    print "Are you sure you want to delete this listing?<br>";
    print "<input type='hidden' name='id' value='$id'>";
    print "<input type='submit' name='submit' value='delete' class='submit'>";
	print "<a class='button' href=\"" . $_SERVER['HTTP_REFERER'] . "\">do NOT delete</a>";
 
print "</form>";
  }
include($_SERVER['DOCUMENT_ROOT']."/admin/footer-admin.php");  
}
else
{
  $result = mysql_query ($query);
if (! $result) Header("Location: $site/admin/login.php");
// else proceed
}
?>

Open in new window

PHP

Avatar of undefined
Last Comment
nascent1

8/22/2022 - Mon
shanikawm

Cannot determine exact issue without details of your table 'cms_medlinks_entries'. But you can do following.
Print the variable $del_path and check whether it is correct or not.
If it is correct and still has the problem just execute only unlink and see what happen.
<?php
   unlink('/path/to/file');
?>
ASKER
nascent1

Thanks for responding.

printing $del_path gives the correct path:
/admin/medical-links/library/013.jpg

unlink('/admin/medical-links/library/013.jpg');
still does not delete the file, though

Db structure and sample entry is attached.
CREATE TABLE `cms_medlinks_entries` (
  `id` bigint(21) NOT NULL auto_increment,
  `CatID` int(21) NOT NULL default '0',
  `EntryName` varchar(255) NOT NULL default '',
  `file` varchar(255) NOT NULL default '',
  `name` varchar(255) NOT NULL default '',
  `path` varchar(255) NOT NULL default '',
  `size` int(11) NOT NULL default '0',
  `type` varchar(30) NOT NULL default '',
  `out` bigint(20) NOT NULL default '0',
  `private` int(11) NOT NULL default '0',
  `Description` text NOT NULL,
  `links` int(21) NOT NULL default '0',
  `URL` varchar(255) NOT NULL default '',
  `email` varchar(40) NOT NULL default '',
  `validated` int(11) NOT NULL default '0',
  `linkout` bigint(20) NOT NULL default '0',
  `importance` int(11) NOT NULL default '0',
  `recip` varchar(255) NOT NULL default '0',
  `validatefile` varchar(255) NOT NULL default '',
  PRIMARY KEY  (`id`),
  KEY `index` (`EntryName`,`Description`(255),`URL`,`validated`),
  FULLTEXT KEY `full_index` (`EntryName`,`URL`,`Description`),
  FULLTEXT KEY `search_links` (`EntryName`),
  FULLTEXT KEY `search_links2` (`URL`),
  FULLTEXT KEY `search_links3` (`Description`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=50 ;
 
--
-- Dumping data for table `cms_medlinks_entries`
--
 
INSERT INTO `cms_medlinks_entries` (`id`, `CatID`, `EntryName`, `file`, `name`, `path`, `size`, `type`, `out`, `private`, `Description`, `links`, `URL`, `email`, `validated`, `linkout`, `importance`, `recip`, `validatefile`) VALUES
(49, 103, 'testfile', 'testfile', '', '', 413032, 'image/jpeg', 0, 0, '<p>testfile description</p>', 0, '/admin/medical-links/library/013.jpg', '', 1, 0, 5, '0', '1'),

Open in new window

ASKER
nascent1

In case it makes a difference, I'm using PHP 5 and MySQL 5.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
shanikawm

In Shell does this work ?
rm /admin/medical-links/library/013.jpg

Then try still in the shell,
php -r 'unlink("/admin/medical-links/library/013.jpg");'
ASKER
nascent1

Sorry, I don't know how to run shell commands. I have these files already loaded at my host.
ASKER
nascent1

I've requested shell access at my host, but don't know how long it will take until they review and grant it.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
shanikawm

I meant execute these commands in your host command prompt.
How do you check whether the file is removed from the directory?
ASKER CERTIFIED SOLUTION
shanikawm

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
nascent1

I don't know how to execute a command in my "host command prompt", sorry. I check to see if the file is deleted by looking at the directory using ftp.
ASKER
nascent1

I believe the issue is actually with my host. I'm checking into this now and will let you know asap.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
ASKER
nascent1

Ok ... I now have shell access :) and I've run these two commands as you'd suggested previously and both work to delete the file:
m /admin/medical-links/library/013.jpg
php -r 'unlink("/admin/medical-links/library/013.jpg");'

I'm looking for my error log now and will check as you suggest.
ASKER
nascent1

My problem is unsolved but I believe it is an issue at my host which you can't help with, of course. :) I'm awarding you the points for the question. Thanks so much for your help. I learned how to access shell! :) lol