Welcome Guest, Not a member yet? Register   Sign In
How can I delete file from my uploads folder?
#1

[eluser]Mohammed Zayan[/eluser]
In my site I can upload photos and I save these photos in folder named upload
and save it's path in my database
I want to delete a photo from database and folder too

in php I use this code

Code:
$row = $result->fetch_object();
$filename = $row->photo_link;
unlink($filename);

How can I do that with CodeIgniter?
#2

[eluser]CroNiX[/eluser]
Same way.

Code:
unlink('/full/path/to/file/file.jpg');
#3

[eluser]Mohammed Zayan[/eluser]
I do that
Code:
$query = $this->books_model->getBookPhoto();
if($query){
    $data = $query;
    foreach($data as $row){
    $photo_link = $row->photo_link;
}
unlink($photo_link);

that what I have

A PHP Error was encountered

Severity: Warning

Message: unlink() [function.unlink]: http does not allow unlinking

Filename: controllers/books.php

Line Number: 199
#4

[eluser]CroNiX[/eluser]
then $photo_link is a url and not a file path... Just change it.
#5

[eluser]Mohammed Zayan[/eluser]
I save it in database to be able to view the photo by <img />
In the first code $filename is a link also
Is this will do what I want?
Code:
$this->load->helper('file');
delete_files($photo_link, TRUE)
#6

[eluser]CroNiX[/eluser]
Maybe. You'd have to play with them as I don't really use those. I only save the filename in my database, not the full url, so knowing the paths to the files is easy.
#7

[eluser]Mohammed Zayan[/eluser]
I use this
Code:
if($this->upload->do_upload()){
    $photo_data = $this->upload->data();
    $FileName =  $photo_data['file_name'];
    $photo_link = base_url() . 'uploads/' . $FileName;
}else{
    $data['message'] = $this->upload->display_errors();
}
#8

[eluser]CroNiX[/eluser]
assuming your "uploads" directory is at the same level as your index.php...

Code:
unlink(FCPATH . 'uploads/' . $FileName);

should produce
/path/to/your/uploads/filename.jpg
#9

[eluser]InsiteFX[/eluser]
You can also try:
Code:
unset

#10

[eluser]CroNiX[/eluser]
unset() deletes files?




Theme © iAndrew 2016 - Forum software by © MyBB