Upload image in specific folder php Cloudinary - php

I'm trying to upload images to folders in my cloudinary account but I can't get it to work, it always saves to the default home folder.
I have two folders in there called categories and products.
This is what I have right now
\Cloudinary\Uploader::upload(
$request->file('image'),
$options = array('public_id' => '/home/categories/' . $request->name . '.' .$extension)
);
I also did '/categories/' . $request->name . '.' .$extension
I get this
Cloudinary\Error
public_id (/home/categories/asdasdf.jpg) is invalid
The docs are here, I can't see what I'm doing wrong
https://cloudinary.com/documentation/image_upload_api_reference#upload_method
How can I fix this?

The correct way is like this
\Cloudinary\Uploader::upload(
$request->file('image'),
$options = array('public_id' => 'categories/' . $request->name)
);
This saves the file at /home/categories/file

Related

Can't write image data to path (upload/brand/1710068361905783.png)

My Project Works Perfectly On Local Server But After I Have Upload It To Public Server . All Add Images Functions Give Me Same Error
Can't write image data to path (upload/....
I Am Using Laravel 8
here is my code when i add brand image
$image = $request->file('brand_image');
$name_gen = hexdec(uniqid()).'.'.$image->getClientOriginalExtension();
Image::make($image)->resize(300,300)->save('upload/brand/'.$name_gen);
$save_url = 'upload/brand/'.$name_gen;
Brand::insert([
'brand_name_en' => $request->brand_name_en,
'brand_name_ar' => $request->brand_name_ar,
'brand_slug_en' => strtolower(str_replace(' ', '-',$request->brand_name_en)),
'brand_slug_ar' => str_replace(' ', '-',$request->brand_name_ar),
'brand_image' => $save_url,
If you haven't created your path's folder, you will get that error. You need to create first it.

Issue with file edit in PHP using file_put_contents (Laravel)

Good afternoon everyone, I have run across a weird issue which I can't put my finger on and I am hoping somebody can help me figure out what is causing this problem.
To provide some context, I allow the user to store an array of images on a product, and after they are stored using laravel-stapler package which is configured in the following way:
public function __construct(array $attributes = [])
{
parent::__construct($attributes);
$this->hasAttachedFile('image', [
'styles' => [
'thumbnail' => '500x500#',
'large' => '800x800#'
],
'url' => '/media/image/:id/:style/:filename',
'default_url' => '/img/category-placeholder-greyscale.jpg',
'convert_options' => [
'jpeg_quality' => 60
]
]);
}
The images are saved in three folders:
../path-to-image/original/file-name
../path-to-image/thumbnail/file-name
../path-to-image/large/file-name
After I save these images I use croppie.js to rotate and edit them. After the user edits and crops them and they submit it the image is sent as a base64 to a controller and the controller is shortened below to only the relevant parts:
$imageData = $request->get('imagebase64');
list(, $imageData) = explode(';', $imageData);
list(, $imageData) = explode(',', $imageData);
$imageData = base64_decode($imageData);
// $image is loaded up through dependency injection
$path = public_path('/path' . '/large/' . $image->image_file_name);
$path2 = public_path('/path' . '/original/' . $image->image_file_name);
file_put_contents($path, $imageData);
file_put_contents($path2, $imageData);
This works on my local machine just fine, the image is saved in both folders and I get a new cropped and edited image, but on my server this doesn't work, the first file_put_contents doesn't work and doesn't store a new image into the /large folder but the second file_put_contents works and stores a new image into the /original folder.
I am not sure why does this happen and would appreciate any help you can give me. I also do not think it is due to permissions because I gave the folder for the images the right permission but I can't be certain. The code doesn't crash also it just executes without saving the first image
you might have to edit the .htaccess file to give permissions to these files

503 Unavailable Service on Form Submit

I created a project with PHP Laravel and Vue JS. and configured it with Amazon AWS's basic plan. In the beginning, it worked well without any issues. But now, when I try to add a new blog post or edit already existed blog post with heavy content, it is showing 503 Service Unavailable error. I developed the algorithm for the blog as shown below.
Tables:
blogs - This table is used to store lightweight data of the post like title, featured image, URL, etc.
posts - This table is used to store the actual content of the post. It contains three columns like blog id, content, order. Here I am
using text datatype for content which will accept nearly 70k characters.
Algorithm:
When I submit a blog post, first it will create a row in the blogs table with lightweight data. And after that, the actual post content will be split into an array with each item contains 65k characters. And every item will be stored in the posts table as a new row with the blog id created in the blogs table. At the time of retrieving the post, it will join the rows in the posts table and will display the actual post.
Note: The above process is working fine without any issues.
Problem:
The actual problem is suddenly it started to show 503 error when I try to add a new post or edit the existed post with images(produces heavy amount of characters), even the post is being created in blogs table and incomplete amount of characters are adding in the posts table, while adding the rest of the content its showing 503 error.
Note: Even it is working fine on my localhost and another Bluehost server.
I tried to reduce the content splitting with 25k characters, but the result is showing the same.
if($request->hasFile('image')) {
$filenameWithExt = $request->file('image')->getClientOriginalName();
$filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
$extension = $request->file('image')->getClientOriginalExtension();
$fileNameToStore = 'post_' . time() . '.' .$extension;
$path = public_path('uploads/posts/' . $fileNameToStore);
if(!file_exists(public_path('uploads/posts/'))) {
mkdir(public_path('uploads/posts/'), 0777);
}
Image::make($request->file('image')->getRealPath())->resize(900, NULL)->save($path);
}else {
$fileNameToStore = NULL;
}
if($request->hasFile('author_image')) {
$filenameWithExt = $request->file('author_image')->getClientOriginalName();
$filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
$extension = $request->file('author_image')->getClientOriginalExtension();
$authorImage = 'author_' . time() . '.' .$extension;
$path = public_path('uploads/authors/' . $authorImage);
if(!file_exists(public_path('uploads/authors/'))) {
mkdir(public_path('uploads/authors/'), 0777);
}
Image::make($request->file('author_image')->getRealPath())->resize(50, 50)->save($path);
}else {
$authorImage = NULL;
}
$blog = Blog::create([
'title' => $request->title,
'image' => $fileNameToStore,
'category' => $request->category,
'meta_description' => $request->meta_description,
'meta_keywords' => $request->meta_keywords,
'url' => $request->url,
'meta_title' => $request->meta_title,
'author_name' => $request->author_name,
'author_image' => $authorImage,
'author_linkedin' => $request->author_linkedin,
'popular' => $request->popular
]);
$contents = str_split($request->post, 65000);
$i = 1;
foreach($contents as $key => $item)
{
Post::create([
'post' => $blog->id,
'content' => $item,
'order' => $i
]);
$i++;
}
I expect the output to be redirect back to blogs page with success message "Post has been created successfully", but the actual result is
Service Unavailable
The server is temporarily unable to service your request due to maintenance downtime or capacity problems. Please try again later.
It looks like you need to increase values of post_max_size and upload_max_filesize in php.ini.
Guide for AWS: https://aws.amazon.com/ru/premiumsupport/knowledge-center/wordpress-themes-2mb/
I also recommend using transactions for your case. This will help to avoid partially created posts. https://laravel.com/docs/5.8/database#database-transactions

How to Zip mutiple files using Zend Compress Filter?

I have multiples files to be compressed into one unique file (zip), but it seems that it creates a new file everytime the loop restart, how do I do that? there is no enough documentation about this, here is my code:
$file1 = "test1.xls";
$files2 = array(
"test2.xls", "test3.xls"
);
$filter = new Zend_Filter_Compress(array(
'adapter' => 'Zip',
'options' => array(
'archive' => BASE_PATH .'/public/' . $this->configuracoes->get('media') . '/temp/zipped'.date('d-m-Y-H-i-s').'.zip'
)
));
$compress = $filter->filter($file1);
foreach($files2 as $file){
$compress = $filter->filter($file);
}
This only result a zip with test3.xls inside.
Worth looking at this link, seems to be the same answer where you open the zip, add files, then close it outside the loop.
Create zip with PHP adding files from url
An old question but I found this looking for something similar.
You can pass a directory to the filter function and it will recursively add all files within that directory.
i.e
$compress = $filter->filter('/path/to/directory/');

How can I programatically add images to a drupal node?

I'm using Drupal 6.x and I'm writing a php class that will grab an RSS feed and insert it as nodes.
The problem I'm having is that the RSS feed comes with images, and I cannot figure out how to insert them properly.
This page has some information but it doesn't work.
I create and save the image to the server, and then I add it to the node object, but when the node object is saved it has no image object.
This creates the image object:
$image = array();
if($first['image'] != '' || $first['image'] != null){
$imgName = urlencode('a' . crypt($first['image'])) . ".jpg";
$fullName = dirname(__FILE__) . '/../../sites/default/files/rssImg/a' . $imgName;
$uri = 'sites/default/files/rssImg/' . $imgName;
save_image($first['image'], $fullName);
$imageNode = array(
"fid" => 'upload',
"uid" => 1,
"filename" => $imgName,
"filepath" => $uri,
"filemime" => "image/jpeg",
"status" => 1,
'filesize' => filesize($fullName),
'timestamp' => time(),
'view' => '<img class="imagefield imagefield-field_images" alt="' . $first['title'] . '" src="/' . $uri . '" /> ',
);
$image = $imageNode;
}
and this adds the image to the node:
$node->field_images[] = $image;
I save the node using
module_load_include('inc', 'node', 'node.pages');
// Finally, save the node
node_save(&$node);
but when I dump the node object, it no longer as the image. I've checked the database, and an image does show up in the table, but it has no fid. How can I do this?
Why not get the Feedapi, along with feedapi mapper, and map the image from the RSS field into an image CCK field you create for whatever kind of node you're using?
It's very easy, and you can even do it with a user interface.
Just enable feedapi, feedapi node (not sure on the exact name of that module), feedapi mapper, and create a feed.
Once you've created the feed, go to map (which you can do for the feed content type in general as well) on the feed node, and select which feed items will go to which node fields.
You'll want to delete your feed items at this point if they've already been created, and then refresh the feed. That should be all you need to do.
Do you need to create a separate module, or would an existing one work for you?
Are you using hook_nodeapi to add the image object to the node object when $op = 'prepare'?
If you read the comments on the site you posted, you'll find:
To attach a file in a "File" field
(i.e. the field type supplied by
FileField module) you have to add a
"description" item to the field array,
like this:
$node->field_file = array(
array(
'fid' => 'upload',
'title' => basename($file_temp),
'filename' => basename($file_temp),
'filepath' => $file_temp,
'filesize' => filesize($file_temp),
'list' => 1, // always list
'filemime' => mimedetect_mime($file_temp),
'description' => basename($file_temp),// <-- add this
), );
maybe that works. You could also try to begin the assignment that way:
$node->field_file = array(0 => array(...
If you are just grabbing content wich has images in, why not just in the body of your nodes change the path of the images to absolute rather than local, then you won't have to worry about CCK at all.
Two other things that you may need to do
"fid" => 'upload', change this to the fid of the file when you save it. If it isn't in the files table then that is something you will need to fix. I think you will need feild_file_save_file()
2.you may need an array "data" under your imageNode array with "title", "description" and "alt" as fields.
To build file information, if you have filefield module, you can also use field_file_load() or field_file_save_file(). See file filefield/field_file.inc for details.
Example:
$node->field_name[] = field_file_load('sites/default/files/filename.gif');

Categories