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

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.........

Related

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

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,
]);
}

Require json file dynamically in react-native (from thousands of files)

I googled so far and tried to find out the solution but not yet.
I know require() works only with static path, so I want alternative ways to solve my problem. I found this answer here but it doesnt make sense for thousands of resources.
Please advise me the best approach to handle such case.
Background
I have thousand of json files that containing app data, and declared all the file path dynamically like below:
export var SRC_PATH = {
bible_version_inv: {
"kjv-ot": "data/bibles/Bible_KJV_OT_%s.txt",
"kjv-nt": "data/bibles/Bible_KJV_NT_%s.txt",
"lct-ot": "data/bibles/Bible_LCT_OT_%s.txt",
"lct-nt": "data/bibles/Bible_LCT_NT_%s.txt",
"leb": "data/bibles/leb_%s.txt",
"net": "data/bibles/net_%s.txt",
"bhs": "data/bibles/bhs_%s.txt",
"n1904": "data/bibles/na_%s.txt",
.....
"esv": "data/bibles/esv_%s.txt",
.....
},
....
As you can see, file path contains '%s' and that should be replace with right string depends on what the user selected.
For example if user select the bible (abbreviation: "kjv-ot") and the chapter 1 then the file named "data/bibles/Bible_KJV_OT_01.txt" should be imported.
I'm not good enough in react-native, just wondering if there is other alternative way to handle those thousands of resource files and require only one at a time by dynamically following the user's selection.
Any suggestions please.
Instead of exporting a flat file, you could export a function that took a parameter which would help build out the paths like this:
// fileInclude.js
export const generateSourcePath = (sub) => {
return {
bible_version_inv: {
"kjv-ot": `data/bibles/Bible_KJV_OT_${sub}.txt`
}
}
}
//usingFile.js
const generation = require('./fileInclude.js');
const myFile = generation.generateSourcePath('mySub');
const requiredFile = require(myFile);
then you would import (or require) this item into your project, execute generateSourcePath('mysub') to get all your paths.

Laravel 5.1 File not deleting from folder

I want to delete a pdf file form my database as well as my public/uploads folder. It is deleting from the database but not from my public folder.
This is my controller:
public function deleteArticle($id) {
$article = Article::findOrFail($id);
File::delete($article->document);
$article->delete();
return redirect()->back();
}
/*This handles the posting of the file into the folder and storing of the url into the datab
$file = Input::file('document');
$file->move('uploads', $file->getClientOriginalName());
$document = asset('uploads/'.$file->getClientOriginalName());
$newArticle->document = $document;
As you are currently saving a url to the database ( by using the asset() function ) you can't delete the file by using that information.
It is usually enough to save just the document name in the database.
$document = $file->getClientOriginalName();
$newArticle->document = $document;
To delete the file you can then call:
File::delete(public_path('uploads/'.$article->document);
To Link to your file you can use the asset() method
asset('uploads/'.$article->document);
Storing the full URL in database is a bad idea. It will very hard to maintain files later. The best way is store only the filename with extension.
If you only have the filename in database, you can delete the file in this way:
$article = Article:findOrFail($id);
$document = $article->document; // take the image name from database
File::delete('uploads/'.$document); // delete the file
$article->delete() // delete the record from database
Edit
If you still want to use URL in database you can get the image name by using substr() and strpos() function. Example:
$image = substr($article->document,0,strpos("uploads/"));
You can get only the document name from URL and use it to delete the file.
To store only the filename follow this:
$document = $request->file('document')->getClientOriginalName();

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', {}, {})

Drupal 7: Print pdf using dompdf

I need help in installation of dompdf. Where should I place the extracted zip file in the directory? I've followed the INSTALL.txt, and it says "Extract the contents of the downloaded package into one of the supported paths." Does it mean placing into "Modules" folder? if so, an error occurs requesting for ".info". And it's not supplied. Please help, I'm confused! Thanks!
The supported paths are listed in the install.txt file:
supported paths:
* print module lib directory (usually sites/all/modules/print/lib)
* libraries directory (sites/all/libraries)
I prefer the second option, it will keep you from having to do this every time you update the module.
In other words it should look like this sites/all/libraries/dompdf
here's how I loaded it
I moved the folder dompdf-0.5.1 to the /sites/all/libraries folder
I edited dompdf_config.inc.php by replacing the DOMPDF_autoload() function with:
Code:
function DOMPDF_autoload($class) {
/* Add this checking - START */
if (mb_strtolower($class)== 'firephp'){
return;
}
/* Add this checking - END */
$filename = mb_strtolower($class) . ".cls.php";
require_once(DOMPDF_INC_DIR . "/$filename");
}
if ( !function_exists("__autoload") ) {
/**
* Default __autoload() function
*
* #param string $class
*/
function __autoload($class) {
DOMPDF_autoload($class);
}
}
now you should be able use it like so in any other module
Code:
require_once(realpath('.')."/sites/all/libraries/dompdf-0.5.1/dompdf_config.inc.php");
spl_autoload_register('DOMPDF_autoload');
$obj = new DOMPDF();
This worked and I was able to use the DOMPDF object/class.

Resources