I'm not sure where the problem lies. But the code won't unlink the file.
$file_path = 'assets/upload/audio/';
if(!empty($data['audio_data']) && $data['data']->tstatus == 'C'){
foreach($data['audio_data'] as $rw){
//print_r($file_path.$rw->audio_file_name);exit;
unlink(FCPATH .$file_path.$rw->audio_file_name);
}
}
Try to use this.
unlink('assets/upload/audio/'.$rw->audio_file_name);
Try to use base_url() instead of FCPATH
Update:- And before this check file in the directory
if(file_exists($file_path.$rw->audio_file_name)){
unlink(base_url($file_path.$rw->audio_file_name));
else{
echo 'File does not exists';
}
Related
Is there a way to zip and download files and folders which are in Amazon S3 bucket, together in Laravel? I Want to zip the three folders and one file in the picture together and download it
Here's a half baked solution in a route file. Hope it helps.
https://flysystem.thephpleague.com/docs/adapter/zip-archive/
composer require league/flysystem-ziparchive
I put this in routes/web.php just to play with.
<?php
use Illuminate\Support\Facades\Storage;
use League\Flysystem\Filesystem;
use League\Flysystem\ZipArchive\ZipArchiveAdapter;
Route::get('zip', function(){
// see laravel's config/filesystem.php for the source disk
$source_disk = 's3';
$source_path = '';
$file_names = Storage::disk($source_disk)->files($source_path);
$zip = new Filesystem(new ZipArchiveAdapter(public_path('archive.zip')));
foreach($file_names as $file_name){
$file_content = Storage::disk($source_disk)->get($file_name);
$zip->put($file_name, $file_content);
}
$zip->getAdapter()->getArchive()->close();
return redirect('archive.zip');
});
You'll definitely want to do something different than just plopping it in the public dir. Maybe stream it out straight out as a download or save it somewhere better. Feel free to post comment/questions and we can discuss.
I did it the following way after looking at some solutions by streaming the zip directly to the client by using https://github.com/maennchen/ZipStream-PHP :
if ($uploads) {
return response()->streamDownload(function() use ($uploads) {
$opt = new ArchiveOptions();
$opt->setContentType('application/octet-stream');
$zip = new ZipStream("uploads.zip", $opt);
foreach ($uploads as $upload) {
try {
$file = Storage::readStream($upload->path);
$zip->addFileFromStream($upload->filename, $file);
}
catch (Exception $e) {
\Log::error("unable to read the file at storage path: $upload->path and output to zip stream. Exception is " . $e->getMessage());
}
}
$zip->finish();
}, 'uploads.zip');
}
I have been going round and round with this. I have uploads working with the follow:
public function store(Tool $tool)
{
If(Input::hasFile('file')){
$file = Input::file('file');
$name = $file->getClientOriginalName();
$path=Storage::put('public',$file); //Storage::disk('local')->put($name,$file,'public');
$file = new File;
$file->tool_id = $tool->id;
$file->file_name = $name;
$file->path_to_file = $path;
$file->name_on_disk = basename($path);
$file->user_name = \Auth::user()->name;
$file->save();
return back();
}
however when I try to download with:
public function show($filename)
{
$url = Storage::disk('public')->url($filename);
///$file = Storage::disk('public')->get($filename);
return response()->download($url);
}
I get the FileNotFound exception from laravel
However, if I use this instead:
$file = Storage::disk('public')->get($filename);
return response()->download($file);
I get
FileNotFoundException in File.php line 37: The file "use calib;
insert into
notes(tool_id,user_id,note,created_at,updated_at)
VALUES(1,1,'windows server 2008 sucks',now(),now());" does not exist
which is the actual content of the file...
It can obviously find the file. but why wont it download?
Try this:
return response()->download(storage_path("app/public/{$filename}"));
Replace:
$file = Storage::disk('public')->get($filename);
return response()->download($file);
With:
return response()->download(storage_path('app/public/' . $filename));
response()->download() takes a path to a file, not a file content. More information here: https://laravel.com/docs/5.4/responses#file-downloads
If any one still could not find their file even though the file clearly exists then try
return response()->file(storage_path('/app/' . $filename, $headers));
It could be due to a missing directory separator or it isn't stored inside the public folder.
Good afternoon everyone !
I have this simple code in the controller
public function descargarRutaGPX($id){
$buscar = Ruta::find($id);
$file = Storage::get('ddfbdsfv.gpx');
echo $file;
}
If the file exists the browser show "Preppend text" , but if the file doesn't exists the browser shows FileNotFoundException in FilesystemAdapter.php line 61.
Could anyone help to me ?
UPDATED
$buscar = Ruta::find($id);
if (Storage::has('aaaa.gpx')) {
$file = Storage::get('aaaa.gpx');
return response()->download($file);
}else{
echo "NO";
}
This is the error.
The file "Prepended Text" does not exist
You'll probably want to check and make sure the file exists as well as download the file if it does. Try changing your code to the following
public function descargarRutaGPX($id){
$buscar = Ruta::find($id);
If (Storage::has('ddfbdsfv.gpx') {
$file = Storage::get('ddfbdsfv.gpx');
return response()->download($file);
} else {
// file not found. Do something to alert the user
}
}
Magento admin throws an exception:
Could not determine temp directory, please specify a cache_dir manually
It is fresh instalation on new hosting package.
Usually it will happen in shared web hosting, but also some times on individual server, if the permission of tmp folder is set wrong.
Many people suggest to modify the file:
/lib/Zend/Cache/Backend/File.php to fix this problem. However, it may be a trap when you upgrade your Magento, as this file resides as core file of Magento. I recommend to use Magento's override feature.
Firstly, copy /lib/Zend/Cache/Backend/File.php to /app/code/local/Zend/Cache/Backend/File.php.
Then on line 91 or near this line, you will find:
'cache_dir' => null,
Change to:
'cache_dir' => "var/tmp/",
You can change the cache folder wherever you want.
Now create a directory named tmp(or whatever name you have given above) under var folder and change the permission to 777 if necessary.
This is only the permission issue. Just set the 777 permission to the cache directory and you are all done. try it.
For more details you can follow the link.
When ever you set the permission be sure it is recurrsively set..
chmod 777 -R /var/cache
this is the function
public function getTmpDir()
{
$tmpdir = array();
foreach (array($_ENV, $_SERVER) as $tab) {
foreach (array('TMPDIR', 'TEMP', 'TMP', 'windir', 'SystemRoot') as $key) {
if (isset($tab[$key])) {
if (($key == 'windir') or ($key == 'SystemRoot')) {
$dir = realpath($tab[$key] . '\\temp');
} else {
$dir = realpath($tab[$key]);
}
if ($this->_isGoodTmpDir($dir)) {
return $dir;
}
}
}
}
$upload = ini_get('upload_tmp_dir');
if ($upload) {
$dir = realpath($upload);
if ($this->_isGoodTmpDir($dir)) {
return $dir;
}
}
if (function_exists('sys_get_temp_dir')) {
$dir = sys_get_temp_dir();
if ($this->_isGoodTmpDir($dir)) {
return $dir;
}
}
// Attemp to detect by creating a temporary file
$tempFile = tempnam(md5(uniqid(rand(), TRUE)), '');
if ($tempFile) {
$dir = realpath(dirname($tempFile));
unlink($tempFile);
if ($this->_isGoodTmpDir($dir)) {
return $dir;
}
}
if ($this->_isGoodTmpDir('/tmp')) {
return '/tmp';
}
if ($this->_isGoodTmpDir('\\temp')) {
return '\\temp';
}
Zend_Cache::throwException('Could not determine temp directory, please specify a cache_dir manually');
}
defined in file lib/Zend/Cache/Backend.php
http://www.webtechnologycodes.com/magento-error-could-not-determine-temp-directory-please-specify-a-cache_dir-manually/
Create tmp folder in root of your magento installation with 777 permissions.
Open lib/Zend/Cache/Backend/File.php
Find $_options property and change line: 'cache_dir' => null, to 'cache_dir' => 'tmp',
Refresh page.
Create an info.php and check for the path beneath upload_tmp_dir to be writable for the webserver.
<?php phpinfo();
Otherwise set the path in your hosting environment.
Beware that this setting can not be placed within .htaccess files but some hosters allow individual php.ini files to be placed in your docroot:
upload_tmp_dir = /path/to/docroot/var/tmp/
I actually managed to make an installation zip for a plugin that I made and in the installation script file, I included this code in the install function in order to copy some file in a folder within joomla in another directory.
class plgVmCustomPayeddownloadsInstallerScript {
function install($parent) {
$src = "plugins/vmcustom/payeddownloads/payeddownloads.php";
$destination = "components/com_virtuemart/controllers";
if(!JFile::move($src, $destination,JPATH_ROOT)){
echo "tried to move from ".$src." to ".$destination;
return false;
}
}
After the installation I keep getting an error in joomla, "unable to rename the file" and the file that was supposed to be moved commanded by the install function did not, despite the fact that the files within the installation.xml did actually get coppied and installed correctly.
Also within the installation script In the install function I included some sql which is executed normally without any problems.
and I also tried in the postflight function with no success.
Also I do not get any specific erros from the php_error.log
I also tried to create this weird test, with the above tester.php file in my root application of my joomla installation.
<?php
/**
* #package Joomla.Site
* #copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
* #license GNU General Public License version 2 or later; see LICENSE.txt
*/
// Set flag that this is a parent file.
define('_JEXEC', 1);
define('DS', DIRECTORY_SEPARATOR);
if (file_exists(dirname(__FILE__) . '/defines.php')) {
include_once dirname(__FILE__) . '/defines.php';
}
if (!defined('_JDEFINES')) {
define('JPATH_BASE', dirname(__FILE__));
require_once JPATH_BASE.'/includes/defines.php';
}
require_once JPATH_BASE.'/includes/framework.php';
// Mark afterLoad in the profiler.
JDEBUG ? $_PROFILER->mark('afterLoad') : null;
// Instantiate the application.
$app = JFactory::getApplication('site');
// Initialise the application.
$app->initialise();
// Mark afterIntialise in the profiler.
JDEBUG ? $_PROFILER->mark('afterInitialise') : null;
// Route the application.
$app->route();
// Mark afterRoute in the profiler.
JDEBUG ? $_PROFILER->mark('afterRoute') : null;
// Dispatch the application.
$app->dispatch();
// Mark afterDispatch in the profiler.
JDEBUG ? $_PROFILER->mark('afterDispatch') : null;
// Render the application.
$app->render();
// Mark afterRender in the profiler.
JDEBUG ? $_PROFILER->mark('afterRender') : null;
//plugins/vmcustom/payeddownloads/payeddownloads.php to
//components/com_virtuemart/controllers
jimport('joomla.filesystem.file');
$src = JPATH_ROOT."/plugins/vmcustom/payeddownloads/payeddownloads.php";
$destination = JPATH_ROOT."/components/com_virtuemart/controllers/";
echo $src."<br>";
echo $destination."<br>";
JFile::move($src,$destination);
?>
The file does not get moved from payeddownloads to controllers folder and it is not causing any error.
Also I need to mention that php.ini has error_reporting = E_ALL and display_errors = On.
Also the php_error.log captures the errors. If I type for example echo "lala"oo it will log the error and it will show it.
So I suspect that JFile::move has a bug and its not throwing any errors even though the file is not copied. Any suggestions please?
Try using the following instead. Few tweaks make to your code:
function install($parent) {
$src = JPATH_SITE . "/plugins/vmcustom/payeddownloads/payeddownloads.php";
$destination = JPATH_SITE . "/components/com_virtuemart/controllers";
if(JFile::exists($src)){
echo "File Exists!";
}
else{
echo "File Doesn't Exist!";
}
if(JFolder::exists($destination)){
echo "Destination Folder Exists!";
}
else{
echo "Destination Folder Doesn't Exist!";
}
JFile::move($src, $destination); // Move file
if(JFile::move($src, $destination)){
echo "File Successfully Moved!";
}
else{
echo "failed to move from ".$src." to ".$destination;
}
}
Even thought the thread is old, I found it having the same issue, maybe the solution could work for anybody else.
JFile::move requires a source and destination FILE path. In the above code the paths should look like follows.
$src = JPATH_SITE . "/plugins/vmcustom/payeddownloads/payeddownloads.php";
$destination = JPATH_SITE . "/components/com_virtuemart/controllers/payeddownloads.php";