laravel file uploading in artisan command line - laravel

I'm writing a Artisan command to do batch image uploads to the application.
Trying to look for the equivalent command to do the following
$file = Input::file('photo');
Becaues I'm in the command line the Input class doesn't work.
I tried File::get('/path/to/file') but it didn't return the same object.

Input::file() will only receive files via form POST:
<form method="POST" enctype='multipart/form-data' action="index.php">
<input type=file name=upload>
<input type=submit name=press value="OK">
</form>
What means that you cannot use Input::file() unless you are doing something like this to test your upload
curl --form upload=/path/to/file http://your/site/url/upload
If you are trying to send files via command, like
php artisan upload /path/to/file
You'll need to get this filename, read the file from disk and do whatever you need to do with it.

Related

How to STR Replace and echo in laravel

Files that are uploaded to my site have their names changed and extention change to bin. So for example a uploaded file will be dfd84848.bin, using the bin file worked fine with modelviewer but im trying to test babylon.js and it doesnt work with .bin only if i change .bin to .glb.
This code {{uploaded_asset($detailedProduct->cad_file)}} provides link like uploads/all/p1YZX9TBc7EmimFx4rXnSFKvVor8EttpOUUpylLL.bin
I want to change .bin to .glb and echo it.
i tried this
#php
$previewfile = uploaded_asset($detailedProduct->cad_file);
$replaced = Str::replace('.bin', '.glb', $previewfile );
#endphp
to show i used this <model url="{$replaced}"> but it never echoed url
Edit
So now that it echos as name.glb it doesnt work because the file doesnt exist on server. When 3d models such as .glb, .gltf are uploaded they are renamed to .bin i dont know why. The uploader is AizUploadController.php
To echo a value you should use double curly braces:
<model url="{{ $replaced }}">
Docs: https://laravel.com/docs/8.x/blade#displaying-data

How can I send file data with post request with browsing from file loaction, just set file loaction by default

How can I send file data with post request without browsing from file loaction, just set file loaction by default.
<form id="data_post" >        
<input type="file" name="file" value='C:/file/new.pdf'>  
<input type="text" name="Param"> 
</form>
but this not getting the file, when going to send it through rest post request.    
I don't think that you can set a default file location.
Check this: Post

How to read a .docx file via PHP Word sample code in Codeigniter

Am trying to read a .docx file which is inside my project folder via PHP Word library. I have included autoloader like this :
include_once 'vendor/autoload.php'; in my controller.
CODE FOR FUNCTION IN CONTROLLER:
function test_phpword()
{
$a=base_url();
$path=$a."".'123.docx';
$source =$path;
echo date('H:i:s'), " Reading contents from `{$source}`";
$phpWord = \PhpOffice\PhpWord\IOFactory::load($source);
echo write($phpWord, basename(__FILE__, '.php'), $writers);
}
BUT GETTING ERROR LIKE BELOW:
06:18:42 Reading contents from http://localhost/myproject/123.docx
Fatal error: Uncaught Exception: Cannot find archive file. in /opt/lampp/htdocs/myproject/vendor/phpoffice/common/src/Common/XMLReader.php:51
Try this
Change
$path=$a."".'123.docx';
to
$path='123.docx';
And put 123.docx beside your php script file. make sure the two files are in the same place. Run your php script again.
If this helps and works fine you can check the file path and make proper change to your php program.
While loading the path, you have to give relative path to the doc file as shown below,
$random_name = 123.docx;
$contents = \PhpOffice\PhpWord\IOFactory::load('../uploads/'.$random_name);
I don't know whats your $base_url, but it will not work if it is absolute path like http://path/to/doc/file.
I have worked on it and tested. Hope it helps.

Apache runs the image files as PHP files

I updated PHP to PHP7, but the apache configuration went wrong. It doesn't read files directly and PHP deal with them. I did a test, I wrote <?php echo "hello world"?> into a txt file and renamed it as test.jpg. And it shows 'hello world' when you visit the jpg file. I can't find where the error is.
This is the test url: http://shouke.luopan.me/test.jpg
Try to change header content type :
header("Content-type: image/jpg");

Set an URL in laravel from environment variable

I have a form in my HTML, in the action, I would like to set the page where I want to send the data:
<form action="wherever.com" method="post" target="_blank" style="display: inline;">
The problem is Laravel, in my .env file I set this:
WHEREVER_FORM="http://wherever.com"
Ok, now I want to set this env variable in my ACTION, I have tried with this:
action="env('WHEREVER_FORM')"
Also this:
action="env(WHEREVER_FORM)"
And this:
action="{{env('WHEREVER_FORM')}}"
I know that I can do this from a env variable, but I don´t find how to do it, in the Laravel Documentation I found this:
$env = env('APP_ENV');
// Return a default value if the variable doesn't exist...
$env = env('APP_ENV', 'production');
This is not enough to me.
Someone can help me?
I think your .env file setup is ok:
WHEREVER_FORM="http://wherever.com"
And also:
<form action="{{env('WHEREVER_FORM')}}" method="post" target="_blank" style="display: inline;">
However, Please make sure that you reboot your application after changing .env file. You can do this by:
php artisan serve

Resources