How I can delete image from database and Image folder in CodeIgniter - php

I'm a beginner I'm facing issue related to the deletion of Image from my upload folder.
My code is deleting just image name from database but not the Image from folder what I suppose to do.
My Model
public function delete_std($std_id,$std_image)
{
$this->db->where('std_id', $std_id);
unlink(FCPATH."uploads/".$std_image);
$this->db->delete('student');
}
My Controller
public function delete_std($std_id)
{
$this->load->model('std_model');
$this->std_model->delete_std($std_id,$std_image);
redirect('std_main/view_std');
}

Try this way
public function delete_std($std_id = '', $std_image = '')
{
// Make sure $std_image is correct image.
if (unlink(FCPATH . "/uploads/" . $std_image)) {
$this->db->where('std_id', $std_id);
$this->db->delete('student');
}
}

if(unlink("upload+path/".'image_name');){
$this->db->where('std_id', $std_id);
$this->db->delete('student');
}

Related

How to unlink image?

im using modal bootstrap with codeignitter 4. I can delete the data in database but cannot unlink the image. here my code :
Model :
public function deleteProduct($id)
{
$query = $this->db->table('produk')->delete(array('kode' => $id));
return $query;
}
Controller :
public function delete()
{
$model = new produkModel();
$id = $this->request->getPost('kode');
$produk = $this->produkModel->find($id);
if ($produk['gambar'] != 'default.png') {
unlink('imgproduk/' . $produk['gambar']);
}
$model->deleteProduct($id);
return redirect()->to('/Tabel_Produk');
}
i want to delete the image in directory but dont delete the default.img
Use complete folder path to delete/access image through PHP by using $_SERVER['DOCUMENT_ROOT'] to get server path.
public function delete(){
$model = new produkModel();
$id = $this->request->getPost('kode');
$produk = $this->produkModel->find($id);
if ($produk['gambar'] != 'default.png') {
unlink($_SERVER['DOCUMENT_ROOT'].'/imgproduk/' . $produk['gambar']);
}
$model->deleteProduct($id);
return redirect()->to('/Tabel_Produk');
}

How to delete file if the row of table data had deleted in laravel?

How to delete file if the row of table data had deleted in laravel?
code:
public function destroy($id)
{
$customer = customer::find($id);
$exist_image=$customer->prooffile;
if (isset($exist_image) && file_exists($exist_image)) {
unlink($exist_image);
}
$customer->delete();
return $this->index();
}
I Think file_exists() not working here any better way to delete the file in public folder.
If your $exist_image return only image name then
$customer = customer::find($id);
if(! $customer){
abort(403);
}
$exist_image=$customer->prooffile;
if(File::exists(public_path('userfiles/proof/'.$exist_image))) {
File::delete(public_path('userfiles/proof/'.$exist_image));
$customer->prooffile=null;
$customer->save();
}
Import right facade
use Illuminate\Support\Facades\File;
First, you should run filepath through realpath, secondly check if the returned path is writable and if so, unlink it.

cannot delete the image from the directory but the data succeed deleting from database in codeigniter

i cannot delete the image from directory, but the data succeed deleting from database. Please i need help.
Below is the my controller
public function hapus(){
$id = $this->input->get('id');
/* query showing image for deleting image first before delete it from database */
$path = './asset/uploads/';
$path1 = './asset/hasil_resize/';
$arraydelete = array('id'=>$id);
$rowdel = $this->Model_upldgbr->get_byimage($arraydelete);
/* the image delete from folder */
#unlink($path.$path1.$rowdel->namafile);
$this->Model_upldgbr->get_delete($arraydelete);
$this->session->set_flashdata("pesan", "<div class=\"col-md-12\"><div class=\"alert alert-danger\" id=\"alert\">Success deleting the image and data !!</div></div>");
redirect('root/upload');
}
And Here is my Model
function get_delete($where){
$this->db->where($where);
$this->db->delete($this->tabel);
return TRUE;
}
//function for showing data one by one from the table
function get_byimage($where) {
$this->db->from($this->tabel);
$this->db->where($where);
$query = $this->db->get();
if ($query->num_rows() == 1) {
return $query->row();
}
}
}
Delete Button link is like this :
<i class="fa fa-trash-o"></i>
Hope this will help you :
use two unlink method for removing file from two folders by using FCPATH, should be like this
$path = FCPATH.'asset/uploads/';
$path1 = FCPATH.'asset/hasil_resize/';
#unlink($path.$rowdel->namafile);
#unlink($path1.$rowdel->namafile);
//Or better use ci file helper's `delete_files()` method
for more : https://www.codeigniter.com/user_guide/general/reserved_names.html

Inserts with Laravel 5.1 without page reloading

I've been using Dropzone for several days and I faced some issues. The idea is: the user selects his file, it uploads and goes in his file directory and some of the file's properties (size, name) go in the DB. I can't do it because when the user uploads the file, the page does not refresh and nothing goes in Input::file('file'). I just can't do it. Here is the code i'm using:
class UploadController extends Controller {
public function upload() {
if(Input::hasFile('file')){
$file = Input::file('file');
$user = Auth::id();
$file->move('uploads/'.$user, $file->getClientOriginalName());
}
else {
echo 'Please select a file first';
}
}
Here are the two functions in File.php model
public function getFileId(){
$fileName = Input::file('file')->getClientOriginalName();
$files = File::where('filename', $fileName)->get(); //$fileName
foreach ($files as $file) {
$fileid = $file->fileid;
echo $fileid.'<br>';
Input::file('file')->fileid = $file->fileid; // put fileid as an attribute to the object file for futher usage
}
}
public function incrementFileId(){
$files = File::orderBy('fileid', 'desc')->take(1)->get();
foreach($files as $file){
echo $file->fileid + 1 .' incremented file id<br>';
}
}
So how should my third model function look like to upload the file's properties? DropZone uses Ajax and I though that I should get the file attributes from there but could this be done?!
Use Request instead of Input:
public function upload(Request $request)
{
if ($request->hasFile('file'))
{
$file = $request->file('file');
$file->move('uploads/'.$user, $file->getClientOriginalName());
}

naming a file after submitting a form in Symfony

I am working on a form which accepts some user input and an image file, the submission part and the data getting entered into the database is working fine but I am stuck at how to name a file once it is uploaded, right now this is what i see as an image name in database C:\wamp2.5\tmp\phpF360.tmp which obviously is not correct.
This is what my controller looks like DefaultController.php
public function createBlogAction(Request $request)
{
$post = new Post();
$form = $this->createForm(new PostCreate(), $post);
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$post->upload();
$post->setDate(date_create(date('Y-m-d H:i:s')));
$post->setAuthor('ClickTeck');
$em->persist($post);
$em->flush();
$this->get('session')->getFlashBag()->add(
'notice',
'Success'
);
}
return $this->render('BlogBundle:Default:blog-create.html.twig', array(
'form' => $form->createView()
)
);
}
This is what my upload() looks like inside Entity/Post.php which is uploading the file and moving it into the folder, the file name that I see in a folder is correct however now the one that goes into the database
public function upload()
{
if (null === $this->getImage()) {
return;
}
// I might be wrong, but I feel it is here that i need to name the file
$this->getImage()->move(
$this->getUploadRootDir(),
$this->getImage()->getClientOriginalName()
);
$this->path = $this->getUploadDir();
$this->file = null;
}
I will really appreciate if someone can push me in right direction, I just need to name the file, a name which gets assigned to the image in database and the file should get uploaded with the same name as well.
UPDATE
I managed to get it to work using the following function, not sure if this is the best practice but it did work, i would love to hear from others on this. please do not provide any links, if you can refine what has already been done that would be great.
public function upload()
{
// the file property can be empty if the field is not required
if (null === $this->getImage()) {
return;
}
$dirpath = $this->getUploadRootDir();
$image = $this->getImage()->getClientOriginalName();
$ext = $this->getImage()->guessExtension();
$name = substr($image, 0, - strlen($ext));
$i = 1;
while(file_exists($dirpath . '/' . $image)) {
$image = $name . '-' . $i .'.'. $ext;
$i++;
}
$this->getImage()->move($dirpath,$image);
$this->image = $image;
$this->path = $this->getUploadDir();
$this->file = null;
}
This topic from documentation may help you : http://symfony.com/doc/current/cookbook/doctrine/file_uploads.html
In addition, you should not put your upload function in the controller but rather use Doctrine events (Lifecycle callbacks) to call your function automatically.
as per suggestion of #theofabry you can check symfony2 documentation How to handle File Uploads with Doctrine, Controller must be thin as much as possible and try to do upload with Doctrine Events.
If you want to continue with your logic you may try following code, I have not tested yet...so please be careful.
// set the path property to the filename where you'ved saved the file
$this->path = $this->file->getClientOriginalName();
instead of
$this->path = $this->getUploadDir();

Categories