Why the folder has been created everytime I save an image in Laravel? - storage

After going through a lot of documentation I am able to save an image in Laravel.
But now I am getting an error that every time I save the image it got saved in a separate folder created for that image.
So if I save an image it got saved like
http://localhost/storage/uploads/phpAC6E.tmp.jpg/gedmQBjYgGecrqiuJol6wQs0BKkkMuCko91opWvi.jpeg
Notice the folder phpAC6E.tmp.jpg got created automatically.
The question here is not what I am looking for.
I don't know where I am going wrong I had tried many things but it doesn't work. Below are my code snippets :
filesystem.php configuration-
'local' => [
'driver' => 'local',
'root' => storage_path('app/public/uploads'),
'url' => env('APP_URL').'/storage/uploads',
'visibility' => 'private',
],
My controller function-
public function save(Request $request){
$file = $request->file('image');
$ext = $file->getClientOriginalExtension();
$path = Storage::disk('local')->put($file->getFilename().'.'. $ext, $request->file('image'));
return Storage::url($path);
}
The folders have been created like this.

why not try this code. its working properly. it saves in the public folder by creating first attachment folder outside the root of the domain and saving the file inside. if you would want to save inthe public folder then change to:
$file->move(public_path('attachments'), $name);
if($request->hasfile('filename'))
{
$file=$request->file('filename');
$name=$file->getClientOriginalName();
$file->move(public_path().'/attachments', $name);
DB::table('tblname')->insert([
'filename' => $name,
]);
}

Related

Yii1, prettry url issue

I want to make url like this in Yii1 http://example.com/customer-name. It would list jobs for the customer-name, this customer-name will be changing dynamically for example customer-name can be
customer-name=IBM or customer-name=abc-mn or customer-name=xyz
The urls will be something like this
http://example.com/IBM
http://example.com/abc-mn
http://example.com/xyz
I have tried many tutorials but when I a try nothing works for me. Also I followed the http://www.yiiframework.com/doc/guide/1.1/en/topics.url
You new to configure the main.php config properly and have your controller action ready.
private/protected/config/main.php
'urlManager'=>array(
//path is slash separated format aka www.url.com/controller/action/getparam/getvalue
'urlFormat'=>'path',
'showScriptName'=>false,
'caseSensitive'=>true,
'rules'=>array(
//site is your controller, comapny is your action and the name is get variable actionCompany is waiting for.
'<name>' => 'site/company'
)),
private/protected/controllers/SiteController.php (alos make sure the actioname company is in accessRules if you user acceessControll filter).
public function actionCompany( $name )
{
/* your action code */
$this->render('test', array( 'test' => 'to_view' ) );
}
If this didn't help then you have to give us more of your code.

Can't get Dart asset_pack package example code to run correctly

So I am trying to make a game in Dart and I decided to check out this asset_pack package. I tried to test the example code (below) but asset.imported prints null..why is this?
I did create /web/test/foo.txt in my project folder and put a little bit of text in it but still I get null printed.
main() {
// Construct a new AssetManager.
AssetManager assets = new AssetManager();
// Register the 'test' pack. No url is needed so the empty string suffices.
AssetPack testPack = assets.registerPack('test', '');
// Register asset 'foo' and load it's contents from 'foo.txt'.
// The asset type is 'text' and there are no arguments for the loader
// or importer.
Future<Asset> futureAsset = testPack.loadAndRegisterAsset('foo', 'foo.txt',
'text', {}, {})
futureAsset.then((asset) {
// Print the contents of foo.txt.
print(asset.imported);
});
}
I think your file has to be in the web/ folder or you have to load test/foo.txt. If you run the example you should see an error that the file was not found in the developer console. This code seems to work:
Future<Asset> futureAsset = testPack.loadAndRegisterAsset('foo', 'text',
'test/foo.txt', {}, {})

Twitter Module - Drupal 7

Twitter Module is working finde with my Drupal 7 site. I wanted to make a tweek so that nodes that are not hidden get tweeted, I was able to do this by altering twitter_post_node_insert in twitter/twitter_post . All I did was add a new condition of !$node->hidden . It works great.
function twitter_post_node_insert($node) {
if (!empty($node->status) && !empty($node->twitter) && !empty($node->twitter['post'])
&& !$node->hidden) { ......
My problem is that this code in the Twitter Module only gets called when I directly edit a node and save it. Now, I would like to have said code called also when I edit my node programmatically, where I save it with $node_wrapper->save(); . The twitter code won't get called. I've also tried with node_save($node); , instead of using my $node_wrapper. Nothing.
I also tried including the file twitter_post.module located in twitter/twitter_post, and then calling the function in charge of posting the tweet :
module_load_include('module', 'twitter', '../twitter/twitter_post/twitter_post');
twitter_post_node_update($node);
Nothing happens and no errors are shown. What I'd like is to know what Drupal 7 function gets called in its core when you edit a node through its interface and then save it. That way I can just put that function in the code where I edit my node programmatically so that the Twitter code will also get called. Or, does anyone have a better approach?
Thanks.
After looking into the Twitter module this is what I have done in order to have the nodes published programmatically.
I added the following function to twitter/twitter_post/twitter_post.module . It's a copy of the function function twitter_post_node_insert($node), found on the same file. I made a copy so that it would not print out the message of "tweet posted succesfully". That way I call the copied function from another place to post the tweet.
/**
* Function called from custom .module to insert tweets from "Editar Pesos" tab
*/
function twitter_post_node_custom_insert($node) {
if (!empty($node->status) && !empty($node->twitter) && !empty($node->twitter['post']) && !$node->hidden ) {
module_load_include('inc', 'twitter');
$twitter_account = twitter_account_load($node->twitter['account']);
$replacements = array(
'!title' => truncate_utf8($node->title, 90, false, true),
'!url' => url('node/' . $node->nid, array('absolute' => TRUE, 'alias' => TRUE)),
'!url-alias' => url('node/' . $node->nid, array('absolute' => TRUE))
);
// Only generate the shortened URL if it's going to be used. No sense
// burning through TinyURLs without a good reason.
if (strstr($node->twitter['status'], '!tinyurl') !== FALSE) {
$replacements['!tinyurl'] = twitter_shorten_url(url('node/' . $node->nid,
array('absolute'=> TRUE)));
}
$status = strtr($node->twitter['status'], $replacements);
return twitter_set_status($twitter_account, $status);
}
}
And following is the "magic", which gets called whenever I want the node posted as a tweet.
function post_to_twitter($node){
module_load_include('module', 'twitter', '../twitter/twitter_post/twitter_post');
$twitter = array(
'account' => getTwitterUid(),
'post' => 'POST',
'status' => "!title !tinyurl"
);
$node->twitter = $twitter;
return twitter_post_node_custom_insert($node);
}
function getTwitterUid(){
return db_query("select twitter_uid from {twitter_account} where screen_name = :screen_name
limit 1", array(":screen_name" => 'YOUR_TwitterScreenName'))->fetchField();
}
Hope this can help anyone who was looking for the same thing as I.

DbTableGateway is not writing my session information to database table

I'm trying to use DbTableGateway to store my session information in a MySQL database--but my "sessions" table is remaining empty. It never contains any rows. Here's my code (more or less copy/pasted from here):
$dbAdapter = new Zend\Db\Adapter\Adapter(array(
'driver' => 'pdo_mysql',
'database' => 'db-name',
'username' => 'username',
'password' => 'password!'
));
$tableGateway = new \Zend\Db\TableGateway\TableGateway('session', $dbAdapter);
$saveHandler = new \Zend\Session\SaveHandler\DbTableGateway($tableGateway, new \Zend\Session\SaveHandler\DbTableGatewayOptions());
$manager = new \Zend\Session\SessionManager();
$manager->setSaveHandler($saveHandler);
$someContainer = new Container('SomeSessionNamespace');
$someContainer->aBitOfData = 'tasty morsel of data';
And here's a video demonstration of me using this code:
http://screencast.com/t/UDDUs6OZOib
As you can see in the video, session information is preserved between requests, but it's not being stored in the database.
I added breakpoints to every function in Zend\Session\SaveHandler\DbTableGateway, and the only one that's getting hit is in __constructor. So the constructor is getting called, but apparently it never gets used for anything else.
What am I missing?
I'm using Zend Framework 2.2.2 on PHP 5.3.
-Josh
I found some modules to do that if you need to implement this quickly
https://github.com/Nitecon/DBSessionStorage
https://github.com/gabriel403/G403SessionDb
To use your current code, please check:
options of ** DbTableGatewayOptions** (id, data, lifetime, etc..)
$options = new \Zend\Session\SaveHandler\DbTableGatewayOptions();
$options->setDataColumn('data');
$options->setIdColumn('id');
$options->setLifetimeColumn('lifetime');
$options->setNameColumn('name');
$options->setModifiedColumn('modified');
the start of you SessionManager $manager->start();
Check in application.config.php and make sure the Application module is at the top level
Also make sure that in
'vendor/composer/autoload_namespaces.php' and
'vendor/composer/autoload_static.php'
zend and zendxml library path added or not
eg : 'Zend' => array(vendorDir . '/ZF2/library'),
'ZendXml' => array(vendorDir . '/ZF2/library')

Upload file Yii The second argument to copy() function cannot be a directory

I'm trying to upload file but get this error message :
move_uploaded_file() [<a href='function.move-uploaded-file'>function.move-uploaded-file</a>]: The second argument to copy() function cannot be a directory
I think there's something problem with this file but I've no idea to solve it..
<?php
class FileUploadController extends CController {
public function actionUpload() {
$model = new FileUpload();
$form = new CForm('application.views.fileUpload.uploadForm', $model);
if ($form->submitted('submit') && $form->validate()) {
$form->model->image = CUploadedFile::getInstance($form->model, 'image');
if($model->validate())
{
$model->image->saveAs('/opt/lampp/htdocs/upl/images');
Yii::app()->user->setFlash('success', 'File Uploaded');
$this->redirect(array('upload'));
}
}
$this->render('upload', array('form' => $form));
}
}
?>
You either need to check all files existed at '/opt/lampp/htdocs/upl/images' and check if the same named file is available or not, if available then just rename the file with extra "_1" every time, or you can always upload the file by renaming the file into some machine name sort of thing see the code below,
$name = rand(1000,9999) . time(); // rand(1000,9999) optional
$name = md5($name); //optional
$model->image->saveAs('/opt/lampp/htdocs/upl/images/' . $name . '.jpg');
This is what I usually do with file uploads, provided that you're saving the files references into the database or in any text file.
EDIT
Get Extension.
In case if you're required to get extension of the file rather then of hard-coded, you can use $model->image->getExtensionName(); it will get you the extension of the uploaded file without . (dot)
Finally, I solved it by myself:
The problem was located in line
$model->image->saveAs('/opt/lampp/htdocs/upl/images');
It should be :
$model->image->saveAs('/opt/lampp/htdocs/upl/images/images.jpg');
Now, there's another problem: when I upload 'new image', it will be replace the old file, I want the file(s) being uploaded not replaced the old file or I need something like rename as new file. Does anyone knows?
goto cuploadedfile.php .
over write this function with this
if($this->_error==UPLOAD_ERR_OK)
{
if($deleteTempFile)
return move_uploaded_file($this->_tempName,$file."/".$this->getName());
elseif(is_uploaded_file($this->_tempName))
return copy($this->_tempName, $file."/".$this->getName());
else
return false;
}
thank u.........

Resources