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');
}
Related
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
I have a script when user can clip video, then that video uploads to public folder, and now I want to upload all video data to database. But i get error like in title. Here's my code:
Controller:
public function clip($id)
{
$video = Video::where('id', $id)->first();
$oldId = $video->id;
$originalName = $video->original_name;
$newName = str_random(50) . '.' . 'mp4';
FFMpeg::fromDisk('public')
->open('/uploads/videos/' .$video->file_name)
->addFilter(function ($filters) {
$filters->clip(FFMpeg\Coordinate\TimeCode::fromSeconds(5), FFMpeg\Coordinate\TimeCode::fromSeconds(2));
})
->export()
->toDisk('public')
->inFormat(new \FFMpeg\Format\Video\X264)
->save('/uploads/videos/' . $newName);
$data = ['user_id'=>Auth::user()->id,
'file_name'=>$newName,
'original_name'=> $originalName,
'old_id' => $oldId,
];
$video = Video::edit($data);
}
Model:
public static function edit($request)
{
$video = new Video;
$video->user_id = $request->user_id;
$video->file_name = $request->file_name;
$video->original_name = $request->original_name;
$video->save();
$old = $file = Video::where('id', $request->old_id)->delete();
//$old_file = unlink($request->file('file'));
return $video;
}
What should I edit?
Since you're passing array, you need to use $request['user_id'] syntax instead $request->user_id. You're getting the error because you're trying to treat the array as an object.
But since you have prepared array here, just use create method:
public static function edit($data)
{
$this->destroy($data['old_id']);
return $this->create($data);;
}
Don't forget to add $fillable array to your model to make it work.
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');
}
I want to delete record Photo and Photo_list from Database but give me error
This is my Code in Controller
public function deletephoto($id)
{
$product = $this->productRepository->findWithoutFail($id);
Product::select('photo','photo_list')->delete($product->id);
return redirect(route('stores.index'));
}
I don't think you can delete specific data with delete.
Delete is used to remove a row.
You will need to update your table with a request like that :
public function deletephoto($id)
{
$product = $this->productRepository->findWithoutFail($id);
Product::where('id', 100)->update(['photo' => NULL, 'photo_list' => NULL]);
return redirect(route('stores.index'));
}
You can see more here :
https://laravel.com/docs/5.3/eloquent#updates
https://laravel.com/docs/5.3/eloquent#deleting-models
You can try this :
public function deletephoto($id)
{
$product = $this->productRepository->findWithoutFail($id);
if($product){
$product->photo= null;
$product->photo_list= null;
$product->save();
}
return redirect(route('stores.index'));
}
public function deletephoto($id)
{
$product = $this->productRepository->findWithoutFail($id);
Product::Where('id','=',$product->id)->delete();
return redirect(route('stores.index'));
}
Or you can directly do this
Product::find($id)->delete();
Try this
Product::WhereProductId($product->id)->delete();
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());
}