It doesnt see image if I upload. Image input name="projects[]".
I want move file to path but it doesnt see file in my PHP.
My code:
public function processProject()
{
if (!empty($_FILES['projects']['name']) and empty(Message::$msgs)) {
$upl = Upload::instance(3145728, "png,jpg");
$upl->process("projects", UPLOADS . '/' . $safe->category . '/', "MEM_");
}
if (!empty($_FILES['projects']['name'])) {
$data['cover'] = $upl->fileInfo['fname'];
}
}
Related
I'm using Laravel 9 and Image Intervetion to resize uploaded images:
public static function resize($file, $fileName)
{
$path = self::route();
foreach (self::size() as $key => $value) {
$resizePath = self::route() . "{$value[0]}x{$value[1]}_" . $fileName;
Image::make($file->getRealPath())
->resize($value[0], $value[1], function ($constraint) {
$constraint->aspectRatio();
})
->save(storage_path($resizePath));
$urlResizeImage[] = ["upf_path" => $resizePath, "upf_dimension" => "{$value[0]}x{$value[1]}"];
}
self::$urlResizeImage = $urlResizeImage;
}
But the line ->save(storage_path($resizePath)); returns this error:
Can't write image data to path
So in the Image Facade of Intervention, there's a #file_put_contents:
public function save($path = null, $quality = null, $format = null)
{
$path = is_null($path) ? $this->basePath() : $path;
// dd($path);
if (is_null($path)) {
throw new NotWritableException(
"Can't write to undefined path."
);
}
if ($format === null) {
$format = pathinfo($path, PATHINFO_EXTENSION);
}
$data = $this->encode($format, $quality);
$saved = #file_put_contents($path, $data);
if ($saved === false) {
throw new NotWritableException(
"Can't write image data to path ({$path})"
);
}
// set new file info
$this->setFileInfoFromPath($path);
return $this;
}
And I tried removing # from #file_put_contents to see what's going wrong here, but then I got this:
file_put_contents(C:\xampp\htdocs\project\storage\upload/1401/11/images/questions/107200x200_1671289517402.jpg): Failed to open stream: No such file or directory
So it basically says that it can not find the $path and when I uncomment dd($path), this is the output:
C:\xampp\htdocs\project\storage\upload/1401/11/images/questions/108200x200_1671289517402.jpg
So what's going wrong here?
How can I properly save the resized images into this directory?
Please I beg you to help me with this because it's been a week that I'm struggling with this error and got headache!
UPDATE #1:
Here is the route():
public static function route()
{
return "upload/" . jdate()->format('Y') . "/" . jdate()->format('m') . "/" . self::$typeFile . "/" . strtolower(self::$catType)."s" . "/" . self::$objId;
}
And I changed it to this:
public static function route()
{
return "upload".DIRECTORY_SEPARATOR.jdate()->format('Y').DIRECTORY_SEPARATOR.jdate()->format('m').DIRECTORY_SEPARATOR.self::$typeFile.DIRECTORY_SEPARATOR.strtolower(self::$catType)."s".DIRECTORY_SEPARATOR.self::$objId;
}
But still the same error occurs :(
I upload from vue 4-5 images to different folders in laravel.
public function uploadImageso2orders(Request $request, $id)
{
$o2order = O2order::findOrFail($id);
$name = $o2order->contactname;
$name2 = str_replace(' ', '_', $name);
$image = $request->file('file');
$imageName = $name2.'.'.time().'.'.$image->extension();
$wwwPath = 'https://api2.api.sk/storage/';
$image->move(storage_path('app/o2/servisne' . '/' . $name2),$imageName);
$imagePath = 'https://api2.api.sk/storage/app/o2/' . ('servisne' . '/' . $name2);
$o2order->servisny = $imagePath;
$o2order->save();
}
This is working fine. In storage/app/o2/servisne_listy/ is folder created and multiple images stored. In sql is written full path to this folder.
I need to show this image or images, sometimes its one sometimes more, not same.
I have this:
public function showImageso2orders(Request $request, $id)
{
$o2order = O2order::findOrFail($id);
$servisny = $o2order->servisny;
return response()->json(['servisny' => $servisny], 200);
}
Its shows only the path from sql, but i need path to every file which is in this directory.
Thanks
May be this help you
$path = storage_path('app/o2/servisne');
$files = File::files($path)
I am uploading an image and while saving to the database I am adding path to the location along with the filename.
eg : product/name/L4I84qXltYvCCDsAeQzi7fzZfKF3WTRa1LPOPGxj.jpeg
Why adding location where I can call them through asset()?
The table is also used by API application where it requires the url format like this.
My Controller Cdde
//! Product Image
if ($request->hasFile('product_image')) {
$product_image = $request->file('product_image');
$product_image_extension = $product_image->getClientOriginalExtension();
$product_image_filename = Str::slug($request->input('product_code')) . '.' . Str::random(3) . "." . $product_image_extension;
$product_image_location = public_path('assets/products/' . $product_image_filename);
Image::make($product_image)->resize(300, 300)->save($product_image_location);
}
Products::create([
//Other Fields
'image' => 'assets/products/' . $product_image_filename
]);
I am wondering how do I remove the image and add a new image
For comparison, I need the image file name. Since I am using the path, I am getting the path prefixed to the Image Name.
use Illuminate\Support\Facades\File
$image_path = "/images/filename.ext"; // Value is not URL but directory file path
if(File::exists($image_path)) {
File::delete($image_path);
}
use File;
#unlink(public_path() . 'uploads/documents/' . $users->document1);
I have a fully working file upload which works when a user creates a course. I use a service which contains the following code to upload the files and to update the form data with the image paths that need to be persisted.
public function handleCourseUploads($request, $formData, $originalCourse)
{
if($formData->getImage() !== null)
{
$courseImagePath = $this->upload($formData->getImage(), $formData->getFrameworkID() . '/course-image/');
$formData->setImage($courseImagePath);
}
foreach ($formData->getModules() as $module)
{
if($module->getImageLink() !== null)
{
$moduleImage = $this->upload($module->getImageLink(), $formData->getFrameworkID() . '/' . $module->getTitle() . '/');
$module->setImageLink($moduleImage);
}
foreach ($module->getDownload() as $download)
{
if($download->getDownloadLink() !== null)
{
$downloadItem = $this->upload($download->getDownloadLink(), $formData->getFrameworkID() . '/' . $module->getTitle() . '/downloads/');
$download->setDownloadLink($downloadItem);
}
}
}
return $formData;
}
When I use the same function when the edit page is submitted it says that null cannot be inserted. The formdata shows the "image link" as null which is obviously the problem. But how do I get around this?
I write a edit function to update news's info, delete previous image from web root and insert new image:
code is below:
if(unlink($data['News']['image_url']['tmp_name'], WWW_ROOT . 'media/' . $data['News']['image_url']['name'])) //delete image from root and database
{
echo 'image deleted.....'; //success message
}
I can't delete old image and insert new image,how can i correct my function ?
Here your data can not find existing data. use this code
$data1 = $this->News->findById($newsid);
$this->request->data = $data1;
$directory = WWW_ROOT . 'media';
if(unlink($directory.DIRECTORY_SEPARATOR.$data1['News']['image_url']))
{
echo 'image deleted.....';
}
Pass filepath as first argument of unlink():
unlink(WWW_ROOT . 'media/' . $data['News']['image_url']['name'] . '/' . $data['News']['image_url']['tmp_name']);
Also make sure that you have proper permissions to perform this operation in directory containing image.