Symfony2: Unable to access the web directory from the entity class - php

I am trying to use the cookbook example on how to upload fiels with symfony2 :
http://symfony.com/doc/2.0/cookbook/doctrine/file_uploads.html
The problem is that I am not able to get the path of the web folder.
Here are some details: In my web folder, I have a structure like that:
/web
/bundles
/sciforumversion2
/images
And from the entity, I I trying to get the folder path and save my image in the /images folder
For that, I am using:
protected function getUploadRootDir()
{
// the absolute directory path where uploaded
// documents should be saved
return __DIR__ . '/../../../../web/'.$this->getUploadDir();
}
protected function getUploadDir()
{
// get rid of the __DIR__ so it doesn't screw up
// when displaying uploaded doc/image in the view.
return 'sciforumversion2/bundles/images';
}
But I am getting then the exception that the directory can't be created:
Unable to create the "/home/milos/workspace/conference2.0/src/SciForum/Version2Bundle/Entity/../../../../web/sciforumversion2/bundles/images/conference" directory
Any idea on how could I get the web folder url and why the solution proposed by the symfony cookbook is not working is mostly welcome.
Thank you very much.
Thank you.

The path seems to be correct.
Probably you should give it the correct permissions.
Try this first:
chmod 777 /home/milos/workspace/conference2.0/src/SciForum/Version2Bundle/Entity/../../../../web/sciforumversion2/bundles/images/
If that works, it's a permission issue.
Set the owner and group to www-data using:
chown www-data:www-data /home/milos/workspace/conference2.0/src/SciForum/Version2Bundle/Entity/../../../../web/sciforumversion2/bundles/images/
chmod 755 /home/milos/workspace/conference2.0/src/SciForum/Version2Bundle/Entity/../../../../web/sciforumversion2/bundles/images/

Related

Rename files in Laravel's public folder

I have this structure in my public folder:
/images
/player_icons
Ajax.png
Barcelona.png
...
I need to let the admin user rename a file simply by entering a new name, rather than uploading a new file.
I have tried the old school PHP way:
rename('/images/player_icons/Ajax.png', '/images/player_icons/test.png');
This produces the error:
File not found at path: images/player_icons/Ajax.png
I have also tried using the Storage facade, although from the looks of it, this can't work in the public folder:
Storage::move('/images/player_icons/Ajax.png', '/images/player_icons/test.png');
I don't understand why the rename() option doesn't work. If I visit example.com/images/player_icons/Ajax.png in the browser, the image is there.
How do I make this happen?
you may need to give the public_path helper function a try
The public_path function returns the fully qualified path to the
public directory. You may also use the public_path function to
generate a fully qualified path to a given file within the public
directory
However, renaming things on the public directory directly means that you HAVE to give this directory a write permissions or change the ownership for this directory to be owned by the server user ( which is almost will be www-data )
this is kind of a risky thing.
The better approach is to ( symlink ) the storage path ( which is writable by default ) and let the user upload and rename files there
more about this will be found here : File system docs
Use this
rename(public_path('/images/player_icons/Ajax.png'), public_path('/images/player_icons/test.png'));

How to allow access to sub folders in laravel?

I have laravel installed on my server and I want to add a folder called 'projects' in laravel's root folder and access the projects folder from URL.
What should I do to solve this issue? I want to upload some PHP code demos and allow people to view them.
I have tried playing with the .htaccess but nothing seems to work for me.
public_path(); // Path of public/
base_path(); // Path of application root
app_path(); // Path of app/

Upload Images to Laravel-Lumen API

With my API i need to upload images from mobile app to my server and save the image path to database so i have the following issues :
Where to save the images? I tried to save them in the storage/app
under images folder (which is work fine)
public function fileUpload(Request $request) {
if ($request->hasFile('image')) {
$image = $request->file('image');
$name = time().'.'.$image->getClientOriginalExtension();
$destinationPath = storage_path('/app/images');
$image->move($destinationPath, $name);
return response()->json(['data'=>"image is uploaded"]);
}
}
and make a symbolic link to this folder in the public folder but its
not working due to access permission error which will lead to the
other issue.
What permission should i gave to the storage folder to make the whole
operation works save the images and the saved link is readable (even 777 didn't work) Access forbidden!
sudo chmod -R 777 storage
Any ideas will be much appreciated
Storage directory is not open to user's requests, public directory is, you need to create a symbolic link from storage directory to public directory:
php artisan storage:link
This command will create a symbolic link from public/storage to storage/app/public, in this case you can store your file under storage/app/public and make it accessible from the web too:
$image = $request->file('image');
$image->storeAs('public', $name); // => storage/app/public/file.img
URL::asset('storage/'.$name); // => http://example.com/stoage/file.img
I would advice you try to create a folder in the public folder and store your files there. You can use base_path()."/public/uploads"
The best place to store the images would inside the storage folder and create a symbolic link in the public folder.
sudo chmod -R 777 storage should work. Make sure you are accessing the right url.
To access the image you should use something like the below urls. My folder structure looks like /storage/app/public/images/user/avatar/
Local: http://localhost/project-name/public/storage/images/user/avatar/HdXzVnKxzUSdAssvXaoM2n0ixzEEG7jlK8acLiHV.png
Production: http://example.com/storage/images/user/avatar/3133L07guCv8shINNZM30c2Ux1HdfpFt04YdLRpf.png
You can check this gist to figure out if you are missing something: https://gist.github.com/danish-shaikh/9c328288a817309f93238c6b5d48ed7e

mkdir function not working properly

I have folder hierarchy as Bucharest/Waterfall/a.php. My code snippet for making directory on server is as follows:
if(!is_dir($this->folder)){
$old_umask = umask(0);
mkdir($this->folder, 0777);
umask($old_umask);
}
chmod($this->folder, 0777);
// Moves current file to upload destination
if(move_uploaded_file($current['tmp_name'],$uploadFile))
return true;
All files are uploaded to the server. Now the issue is that the parent folder ,i.e. Bucharest has permission 755 while inner folder has permission 777. $this->folder has value as Bucharest/Waterfall. It gives 755 permission to Bucharest while 777 permission to waterfall. According to my code the 777 permission should also be given to Bucharest.
I have also tried chmod but all in vain. I want to give full permission to parent folder.
is_dir($this->folder); tells you that the file isn't a directory, not that it doesn't exist. and of course you can't mkdir if the directory is already there.
Also, if the directory already exists, according to the documentation chmod will not do anything if the user under which php is running isn't the same as the user who owns that directory.
Is the folder you are checking a subfolder already? If so, create the parent folders firts.
i.e.
mkdir('/tmp/test1/test2/test3/test4');
will fail if '/tmp/test1/test2/test3' doesn't exist

Magento. Destination folder is not writable or does not exists

Hey I'm stuck with the following problem, plz help.
I get "Destination folder is not writable.." when trying to add an image to a product, but the permission for all needed folders is 777! I had deleted all files on server, didn`t touch DB, reinstalled Magento from scratch with new DB, and everything is OK.
But when I switched to previous DB (change settings in the local.xml) the bug appeared again.
How can the DB impact the folder permissions?
UPDATE:
Thanx a lot, we found out that Magento jump from this method:
public function getBaseMediaUrl()
{
return Mage::getBaseUrl('media') . 'catalog/product';
}
to the following method:
public function getBaseTmpMediaUrl()
{
return Mage::getBaseUrl('media') . 'tmp/catalog/product';
}
Does anybody know why and how????
There's only one spot in the Magento code base that uses that error language.
File: lib/Varien/File/Uploader.php
...
if( !is_writable($destinationFolder) ) {
throw new Exception('Destination folder is not writable or does not exists.');
}
...
Add some temporary debugging code right above this
...
if( !is_writable($destinationFolder) ) {
Mage::Log($destinationFolder);
//or
var_dump($destinationFolder);
throw new Exception('Destination folder is not writable or does not exists.');
}
...
This will let you know the exact folder Magento wants to write to, but can't. Examine this folder, and you'll find it's not writable. Reviewing the edge cases on is_writable may also shed some light on the subject.
Goto : lib/Varien/File/Uploader.php
Temporary change the following code and try to upload image. Now in error message you can see folder path.
In folder path you have to give file permission 777 and it will work as normal. After the error is resolved revert your code to as it wass.
if( !is_writable($destinationFolder) ) {
var_dump($destinationFolder);
throw new Exception(''.$destinationFolder.'Destination folder is not writable or does not exists.');
}
I got the below error while uploading images in Magento then I did the below steps and that worked for me.
Cd /var/www/
chmod 755 -R /html
chown apache:apache -R /html
setenforce 0
then restart apache ..
Magento 2
This is the file in Magento 2 where the error come from:
vendor/magento/framework/File/Uploader.php
At line 256 you can temporarily place this code to get the unwritable/unexisting folder:
if( !is_writable($destinationFolder) ) {
error_log($destinationFolder);
// or
// throw new Exception('Destination folder is not writable or does not exists.');
throw new Exception($destinationFolder);
}
Otherwise you can check you have these folders and that are writable by the web server:
pub/media/catalog/
pub/media/catalog/category
pub/media/catalog/product
pub/media/images
pub/media/wysiwyg/
It may be the expired certificate from the Plesk administration (it was my case).
I tried the steps above, but it did not work. From there I tried to access the files through FileZilla to give the permissions at once to all folders, hence an error message about the expired certificate. It is not the SSL certificate of the store itself, but the administration of Plesk. I created a new self-signed certificate, applied its Plesk administration and everything went back to normal.
This worked for me. I leave here my contribution.
Good luck
I had the same problem:
Sign in to your SSH:
Give media folder permissions through run this command:
cd public_html/media
find . -type d -exec chmod 777 {} \;
My issue is solved after changing the permissions of the media folder. Just go locate the media folder on your server, right click->change permissions->set value 777 for folder permissions.

Categories