How to delete the specific image in folder using codeigniter? - php

I am using code-igniter. In my project i would like to delete the specific image in folder(artical_images). If i try to use delete_files() option it deletes all the images in the folder when use the code like,
public function delete_post($value='$id')
{
$query=$this->db->query("select artical_profile from articals where artical_id='$value' ");
$img_name=$query->result()[0]->artical_profile;
//echo $img_name;
delete_files('./artical_images/',TRUE);
// $this->db->where('artical_id',$value);
//$this->db->delete('articals');
exit();
}
But when i include the specific image name taken from db and include with delete file path it doesn't work.The code is below,
public function delete_post($value='$id')
{
$query=$this->db->query("select artical_profile from articals where artical_id='$value' ");
$img_name=$query->result()[0]->artical_profile;
//echo $img_name;
delete_files('./artical_images/$img_name',TRUE);
// $this->db->where('artical_id',$value);
//$this->db->delete('articals');
exit();
}
even i included the realpath option it also not works,
public function delete_post($value='$id')
{
$query=$this->db->query("select artical_profile from articals where artical_id='$value' ");
$img_name=$query->result()[0]->artical_profile;
$img_name=realpath(APPPATH . '/artical_images/$img_name');
delete_files('$img_name',TRUE);
echo $img_name;
// $this->db->where('artical_id',$value);
//$this->db->delete('articals');
exit();
}
please give some idea to delete the files,

$this->load->helper("file");
delete_files($path);

delete_files uses a directory path to delete all the files in the directory. It applies the recursive unlink command to delete files.
To delete particular file, You can use unlink with the proper file path.
e.g. unlink(FILE_PATH);

Related

Laravel 5.5 get file after uploading with Storage

In my Laravel 5.5 project I am having a problem in showing uploaded files. I uploaded the files using Storage. The part of store action of the controller is indicated below.
if ($request->hasFile('content_uz'))
{
$path = $request->file('content_uz')->store('/content/lesson'.$topic->lesson->id.'/topic'.$topic->id);
$data->content_uz = $path;
}
if ($request->hasFile('content_ru'))
{
$path = $request->file('content_ru')->store('/content/lesson'.$topic->lesson->id.'/topic'.$topic->id);
$data->content_ru = $path;
}
Uploading happened successfully. The path to uploaded 'content_uz' file is stored with "storage/app/content/lesson2/topic3" path and content_uz column is stored in my db as below:
content\lesson2\topic3\WSjrlG9a1ermGDOvRJTjn9iEIhfFvhVzjaOs6l79.mp4
How can I display the files in my Blade template? I searched the web, but with no result.
You can use method like this,
public function showFile() {
header("Content-type: video/mp4");
return Storage::get($filePath);
}
I hope this will help.
You may access the files of storage directory by two ways.
If your files are publicly accessible then you may follow laravel public disk.
If your files are protected or private then you may declare a route to access the files.
Route::get('content/{lesson}/{topic}/{file}', function($lesson, $topic, $file)
{
//Check access logic
$filePath = '/content/' . $lesson . '/' . $topic . '/' . $file;
return Storage::get($filePath);
});

How to Delete Images from Public/Images Folder in laravel 5 (URL Data)

how to delete images file from public/images folder in laravel 5 ??
i found some example from this site, but i know they are just using the file name in their record table, but i'm using something like URL e.g localhost/project/uploads/filename.jpg on my record table. so if i tried like this :
$image_path = $data->image; // the value is : localhost/project/image/filename.format
if(File::exists($image_path)) {
File::delete($image_path);
}
the file is not deleted
help pls, thanks
If you want to delete image from your server, you have to reference location of file in directory server, means you could not reference by url link to delete it.
Commonly, Laravel 5 file is locate in public folder.
Example: your files are located in public/images
$image_path = "/images/filename.ext"; // Value is not URL but directory file path
if(File::exists($image_path)) {
File::delete($image_path);
}
If I can delete image from server by reference URL then Google is the first target :)
You can use the normal PHP delete file keyword ( #unlink )
$image_path = "the name of your image path here/".$request->Image;
if (file_exists($image_path)) {
#unlink($image_path);
}
This is what I do to delete an image:
public function SliderDelete(String $slider_id)
{
$slider = Slider::findOrFail($slider_id);
$image_path = public_path("\storage\images\sliders\\") .$slider->photo;
if(File::exists($image_path)) {
File::delete($image_path);
}
else{
$slider->delete();
//abort(404);
}
$slider->delete();
return response()->json(['success'=>'Slider deleted successfully!']);
}
in laravel 8 you do it like
// import Storage class
use Illuminate\Support\Facades\Storage;
Storage::disk('public')->delete('path-of-file');
you can use disk of your choice like
Storage::disk('s3')->delete('path-of-file');
$filename = public_path($fileloc);
if(File::exists($filename)) {
File::delete($filename);
}
call this function and pass two parameter
$filepath = path where your file exist
$filename = name of your file
public static function UnlinkImage($filepath,$fileName)
{
$old_image = $filepath.$fileName;
if (file_exists($old_image)) {
#unlink($old_image);
}
}
public function destroy($id)
{
$imagePath = YourModelName::select('image')->where('id', $id)->first();
$filePath = $imagePath->image;
if (file_exists($filePath)) {
unlink($filePath);
YourModelName::where('id', $id)->delete();
}else{
YourModelName::where('id', $id)->delete();
}
}
To be able to delete the image it should be in the following form :
"/uploads/img1.jpg" where uploads directory is in the public directory then use the following code:
$image_path = puplic_path("/uploads/img1.jpg")
if (file_exists($image_path)) {
File::delete($image_path);
}
Here is the correct and easy way .
if (file_exists(public_path() . '/storage/gallery/images/'. $item->image)) {
unlink(public_path() . '/storage/gallery/images/'. $item->image);
}

Removing an image from server

I am trying to delete the images from the server using unlink() function. This is deleting the image name from the database, but the image is not deleted from the server, what am i doing wrong?
public function actionDelete()
{
if(Yii::$app->request->isAjax)
{
$id = $_POST['id'];
$product=Product::find()->where(['id'=>$id])->one()->delete();
$delete=CategoryProduct::find()->where(['product_id'=>$id])->all();
foreach($delete as $del)
{
$del->delete();
}
$imgfile="<?php echo Yii::$app->request->baseUrl;?>/web/assets/uploads/<?php echo $product->image;?>";
unlink($imgfile);
echo json_encode(TRUE);die;
}
echo json_encode(FALSE);die;
}
Its is best to set an alias for the upload path (best place will be config/bootstrap.php)so that we can have standard name to all the upload folder. i.e
Yii::setAlias('image_uploads', dirname(dirname(__DIR__)) . '/web/assets/uploads');
You can use the same for saving and deleting the file.
Saving will be like
move_uploaded_file($tmp_file, \Yii::getAlias('#image_uploads/products/') . $product->image);
or
You can use Yii Methods i.e for e.g.
$this->imageFile->saveAs($orignal_file_full_path); // where imageFile in input type file
Deleting will be something like:
unlink (\Yii::getAlias('#image_uploads/products/') . $product->image);
the Core idea is to use real paths for deleting and saving rather then URLs

Codeigniter deleting files in a directory not working

I am trying to delete pdf files from a folder. I have written the delete_files function as follows
public function delete_files($company_id)
{
$this->load->model('search_model');
$company = $this->search_model->get_company($company_id);
$username = $company[0]['username'];
$path=$this->config->base_url('/uploads/'.$username . '/' . 'uploaded');
$this->load->helper("file"); // load the helper
delete_files($path, true); // delete all files/folders
}
when i did echo $path; it shows the right path where i want the files deleted but when i run the entire function nothing happens and i just get a white screen.
You need to use path to file and not resource locator.
$path = FCPATH . "uploads/$username/uploaded";
try this code
Use
$path = FCPATH . "uploads/$username/uploaded";
unset($path);

Codeigniter -> File Upload Path | Folder Create

Currently I have the following:
$config['upload_path'] = 'this/path/location/';
What I would like to do is create the following controller but I am not sure how to attack it!!
$data['folderName'] = $this->model->functionName()->tableName;
$config['upload_path'] = 'this/'.$folderName.'/';
How would I create the $folderName? dictionary on the sever?
Jamie:
Could I do the following?
if(!file_exists($folderName))
{
$folder = mkdir('/location/'$folderName);
return $folder;
}
else
{
$config['upload_path'] = $folder;
}
am not sure what they are talking about by using the file_exist function since you need to check if its the directory ..
$folderName = $this->model->functionName()->tableName;
$config['upload_path'] = "this/$folderName/";
if(!is_dir($folderName))
{
mkdir($folderName,0777);
}
please note that :
i have added the permission to the folder so that you can upload files to it.
i have removed the else since its not useful here ( as #mischa noted )..
This is not correct:
if(!file_exists($folderName))
{
mkdir($folderName);
}
else
{
// Carry on with upload
}
Nothing will be uploaded if the folder does not exist! You have to get rid of the else clause.
$path = "this/$folderName/";
if(!file_exists($path))
{
mkdir($path);
}
// Carry on with upload
$config['upload_path'] = $path;
Not quite sure what you mean by 'dictionary', but if you're asking about how to create a variable:
$folderName = $this->model->functionName()->tableName;
$config['upload_path'] = "this/$folderName/";
To add/check the existence of a directory:
if(!file_exists($folderName))
{
mkdir($folderName);
}
else
{
// Carry on with upload
}
Not 100% sure what you want from your description so here are a few suggestions:
If you need to programmatically create a folder using PHP you can use the mkdir() function, see: http://php.net/manual/en/function.mkdir.php
Also, check out the Codeignitor file helper for reading and writing files:
http://codeigniter.com/user_guide/helpers/file_helper.html
If the folder already exists, you need to make sure the permissions are write-able. On windows you can right click on the folder, properties, security and edit there. On Linux you'll want the chmod command.

Categories