मैं वर्तमान में एक छवि को हटाने की कोशिश कर रहा हूं जब कोई उपयोगकर्ता प्रकाशन से पहले अपनी पोस्ट अपडेट करता है।
सब कुछ ठीक काम करता है, छवि डेटाबेस और पोस्ट पेज में बदल जाती है, लेकिन मैं पिछली छवि को हटाना चाहता हूं।
यहाँ मेरा नियंत्रक है
public function updatePost(Request $request){
$data = $request->all();
$postid = $request['id'];
$isExist = Post::where('id', $postid)->first();
if($isExist){
if ($request->hasFile('image')) {
$file = $request->File('image');
//Get filename with extension
$fileNameToStoreWithExt = $file[0]->getClientOriginalName();
//Get just filename
$filename = pathinfo($fileNameToStoreWithExt, PATHINFO_FILENAME);
//Get just ext
$extension = $file[0]->getClientOriginalExtension();
//File to store
$fileNameToStore = $filename . '_' . time() . '.' . $extension;
//Upload Image
$path = $file[0]->storeAs('image', $fileNameToStore);
$file[0]->move('storage/image', $fileNameToStore);
File::delete(public_path('storage/image'.$isExist['image']));
Post::where('id', $postid)->update([
'title' => $data['title'],
'category' => $data['category'],
'content' => $data['content'],
'image' => $path
]);
return response()->json([
'status'=>'200',
'response'=> 'successfully updated'
]);
}else{
Post::where('id', $postid)->update([
'title' => $data['title'],
'category' => $data['category'],
'content' => $data['content']
]);
return response()->json([
'status'=>'200',
'response'=> 'successfully updated'
]);
}
}else{
return response()->json([
'error'=> 'post does not exist'
]);
}
}
मैंनें इस्तेमाल किया:
फ़ाइल :: हटाएं (public_path ('भंडारण/छवि'। $ isExist ['छवि']));
लेकिन इसने काम नहीं किया
मेरा डिलीट फंक्शन
public function deletePost($id){
$post = Post::where('id',$id)->first();
// dd($post);
if(!$post){
return response()->json([
'status' => '500',
'error' => 'post not found'
]);
}
Storage::disk('public')->delete('/storage/image'. $post['image']);
Post::where('id', $id)->delete();
return response()->json([
'status'=> '200',
'response'=> 'Post successfully deleted'
]);
}
5 जवाब
आप सामान्य पीएचपी डिलीट फाइल कीवर्ड @unlink . का उपयोग कर सकते हैं
if (file_exists($image)) {
@unlink($image);
}
आप विशिष्ट पथ से फ़ाइल को हटाने के लिए File
का उपयोग कर सकते हैं
$file= "image path here";
\File::delete($file);
अपलोड की गई फ़ाइल को सार्वजनिक dir . से हटाएं
या
आप इसके लिए unlink
का उपयोग कर सकते हैं
$image_path = "image path here";
if (file_exists($image_path)) {
@unlink($image_path);
}
use Illuminate\Support\Facades\Storage;
Storage::delete('file.jpg'); // delete file from default disk
Storage::delete(['file.jpg', 'file2.jpg']); // delete multiple files
Storage::disk('your_disk')->delete('file.jpg'); // delete file from specific disk e.g; s3, local etc
कृपया लिंक देखें https://laravel.com/docs/6.x/filesystem
यदि आप लार्वा फ़ाइल-सिस्टम दस्तावेज़ीकरण देखते हैं, तो आप देखेंगे कि वहाँ हैं एकाधिक डिस्क लार्वा समर्थन। स्टोरेज से किसी फाइल को इस तरह से डिलीट करने के लिए आप स्टोरेज फेकाडे का इस्तेमाल कर सकते हैं
use Illuminate\Support\Facades\Storage;
Storage::disk('local')->delete('folder_path/file_name.jpg');
सार्वजनिक निर्देशिका के लिए पथ इस तरह होना चाहिए।
Storage::disk('local')->delete('public/image/'.$filename);
इफ स्टेटमेंट करना आसान है और अपडेट करने पर पुरानी इमेज को डिलीट कर दें! यह कोड एक उदाहरण है इसे अपनी आवश्यकताओं के अनुसार संपादित करें।
if ($request->hasFile('file')) {
Storage::delete($myImage->file); // If $file is path to old image
$myImage->file= $request->file('file')->store('name-of-folder');
}
एक और :
File::delete(public_path('images/'. $oldFilename));
यहां देखें: https://laracasts .com/discuss/channels/laravel/delete-old-image-from-public-folder-after-updating
संबंधित सवाल
जुड़े हुए प्रश्न
नए सवाल
php
PHP एक व्यापक रूप से उपयोग किया जाता है, उच्च-स्तरीय, गतिशील, वस्तु-उन्मुख, और व्याख्या की गई स्क्रिप्टिंग भाषा मुख्य रूप से सर्वर-साइड वेब विकास के लिए डिज़ाइन की गई है। PHP भाषा के बारे में सवालों के लिए इस्तेमाल किया।
$file_path =public_path('/images/') .$banners->photo; if(file_exists($file_path)){ unlink($file_path); }