Welcome Guest, Not a member yet? Register   Sign In
[Solved] Unlink file question
#1

(This post was last modified: 02-13-2015, 09:35 PM by wolfgang1983.)

When I delete a file from my file manager it deletes image from FCPATH . 'image/catalog/' . filename that works fine.

Example <input type="checkbox" value="C:\Xampp\htdocs\codeigniter-3\image/catalog/no_image.png" name="path[]">

But I also need to unlink the filename from FCPATH . 'image/cache'

PHP Code:
public function delete() {

$json = array();

if (!
$json) {
        
if(isset(
$_POST['path'])){

foreach (
$_POST['path'] as $path) {

// $path Deletes from FCPATH . image/catalog/filename;

unlink($path);

// To do delete selected file name from FCPATH . 'image/cache';

}

$json['success'] = 'Your file has been deleted!' .' '$path;

} else {

$json['error'] = 'You need to check the checkbox under the file you wish to delete!';

}

}


$this->output->set_content_type('Content-Type: application/json');

$this->output->set_output(json_encode($json));



I have attached the file manger

What would be best way to also delete filename from image/cache
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply
#2

If it's just the filename, you should be able to use PHP's pathinfo() or basename() function to isolate the filename, then build your new path and delete the file from there, as well.

If you need directory information as well, you will probably need to do a more complicated string comparison, but you should still be able to strip the unwanted portion of the path from the file and replace it with the path to the cache directory. pathinfo() may be helpful for that, as well.

In the latter case, it may be simpler to only use the relative paths in the data passed from the view, since you could build both paths from the relative path.
Reply
#3

(02-13-2015, 10:10 AM)mwhitney Wrote: If it's just the filename, you should be able to use PHP's pathinfo() or basename() function to isolate the filename, then build your new path and delete the file from there, as well.

If you need directory information as well, you will probably need to do a more complicated string comparison, but you should still be able to strip the unwanted portion of the path from the file and replace it with the path to the cache directory. pathinfo() may be helpful for that, as well.

In the latter case, it may be simpler to only use the relative paths in the data passed from the view, since you could build both paths from the relative path.

Thanks. I forgot had done it above with different part in index.


PHP Code:
public function delete() {

$json = array();

if (!
$json) {
        
if(isset(
$_POST['path'])){

foreach (
$_POST['path'] as $path) {


$filename basename($path);

$location1 FCPATH 'image/catalog/' $filename 
$location2 
FCPATH 'image/cache/' $filename;

if (
$filename == true) {
unlink($location1);
unlink($location2);
}

}

$json['success'] = 'Your file has been deleted!';

} else {
$json['error'] = 'You need to check the checkbox under the file you wish to delete!';
}
}

$this->output->set_content_type('Content-Type: application/json');
$this->output->set_output(json_encode($json));

There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB