How store path image with laravel 5.3 ?
if(Input::hasFile('image')){
$file= Input::file('image');
$file->move(public_path().'/',$file->getClientOriginalName());
$blog->title =$file->getClientOriginalName();
}
$blog->save();
Please try it may be working.
$file= Input::file('image');
if(!empty($file)){
$filename = $file->getClientOriginalExtension();
// please identify you upload location
$request->file('image')->move( base_path() . '/public/uploads/', $filename );
$insertdata = array('title'=>$filename);
DB::table('table')->insert($insertdata );
}
Related
Is there any way to use liip imagine_filter without copying the image source to a public path?
I can not see how resolvers/loaders have to be set up to load images from a non public file location and store them likewise.
I defined a watermark filter with a watermark image placed outside public path - which works without problems. But ONLY applied on images placed IN public path.
I am on Symfony 5 and "liip/imagine-bundle": "^2.6"
I have the same problem as you and i found this solution, of course is a little workaround but it works very well.
So I copy the file from $remoteWatermak into the server. if the $localWatermark is setted I check if the file exist.
$arrContextOptions=array(
"ssl"=>array(
"verify_peer"=>false,
"verify_peer_name"=>false,
),
);
if($localWatermark){
$filesystem = new Filesystem();
if(!$filesystem->exists($localWatermark)){
$contents = file_get_contents($remoteWatermark,false,
stream_context_create($arrContextOptions)); // get file
file_put_contents($localWatermark , $contents);
}
}else{
$contents = file_get_contents($remoteWatermark,false,
stream_context_create($arrContextOptions)); // get file
$file = "uploads/watermarks/" . uniqid();
file_put_contents($file , $contents);
$localWatermark = $file;
}
I build a csv export using the admin folder. The file is well uploaded into my public folder.
But when I try to download it using the return, I have an error:
The file "/public/exportCSV.csv" does not exist
I can't understand why, I hope you have an idea. Thanks. I'm under Symfony 4.
$admins = $userRepository->findByRole(User::ROLE_ADMIN);
$filename='exportCSV';
$extension='csv';
$request = Request::createFromGlobals();
if($request->query->get('exportCSV')!= null){
$output = fopen($filename.'.'.$extension, 'w');
fputcsv($output, array("Id","Nom","Prénom","Activé","Dernière connexion","Date d'inscription","Url avatar","Email","Username"));
foreach ($admins as $admin){
$id=$admin->getId();
$lastname=$admin->getLastName();
$firstname=$admin->getFirstName();
$activeState=$admin->getActiveState();
if($activeState){
$active='Oui';
}else{
$active='Non';
}
$lastConnectedAt=$admin->getLastConnected();
if($lastConnectedAt==null){
$lastConnected=" ";
}else{
$lastConnected=$lastConnectedAt->format('Y-m-d H:i:s');
}
$createdAt=$admin->getCreatedAt();
if($createdAt==null){
$created=" ";
}else{
$created=$createdAt->format('Y-m-d H:i:s');
}
$urlAvatar=$admin->getUrlAvatar();
$mail=$admin->getEmail();
$username=$admin->getUsername();
$csvLine= array($id,$lastname,$firstname,$active,$lastConnected,$created,$urlAvatar,$mail,$username);
fputcsv($output,$csvLine);
}
return $this->file('/public/'.$filename.'.'.$extension);
}
You must use the correct path to webserver "public" dir. Check https://stackoverflow.com/a/48585423/3497902
In your example, you can do same like ...
$publicDir = $this->getParameter('kernel.project_dir') . '/public/'; # Your controller must extend AbstractController
$output = fopen($publicDir . $filename.'.'.$extension, 'w');
I'm developing with the yii2 framework. I need to render some reports which should have some images. Everything is working in my excel file. But in PDF there are no images.
What I have in excel:
What I have in PDF:
My test code looks like this:
public function run($format = self::EXCEL) {
$this->format = $format;
if ($this->format == self::PDF) {
$rendererName = \PHPExcel_Settings::PDF_RENDERER_MPDF;
$rendererLibraryPath = Yii::getAlias('#vendor/mpdf/mpdf/');
if (!\PHPExcel_Settings::setPdfRenderer($rendererName, $rendererLibraryPath)) {
throw new BadRequestHttpException('Export pdf fail');
}
$this->headerContentType .= 'pdf';
$this->headerFilename .= date('d_m_Y') . '.pdf';
} elseif ($this->format == self::EXCEL) {
$this->headerContentType .= 'vnd.ms-excel';
$this->headerFilename .= date('d_m_Y') . '.xls';
} else {
throw new Exception('Unknown format for export');
}
$this->objPHPExcel->setActiveSheetIndex(0);
$activeSheet = $this->objPHPExcel->getActiveSheet();
$activeSheet->setTitle('Sample' . date('d_m_Y'));
$objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setWorksheet($activeSheet);
$activeSheet->getColumnDimension('B')->setWidth(50);
$activeSheet->getRowDimension(2)->setRowHeight(80);
$activeSheet->setCellValue('A2','img -> ');
$activeSheet->setCellValue('B2',' ');
$objDrawing->setCoordinates('B'.2);
$objDrawing->setOffsetX(10)->setOffsetY(10);
$objDrawing->setName('Sample_image');
$objDrawing->setDescription('Sample_image');
$objDrawing->setPath('/home/vladimir/projects/temp/img.jpg');
$objDrawing->setWidth(50)->setHeight(50);
header($this->headerContentType);
header($this->headerFilename);
header('Cache-Control: max-age=0');
$objWriter = \PHPExcel_IOFactory::createWriter($this->objPHPExcel, $this->format);
$objWriter->save('php://output');
exit;
}
In this string:
$objDrawing->setPath('/home/vladimir/projects/temp/img.jpg');
I should write a relative path to "web" directory of my project and also I should have this image into "web" directory.
$objDrawing->setPath('img/img.jpg');
/path_to_project/web/img/img.jpg
I was stuck in CI3 and if you are using CI put the images folder outside of application folder and add path as
$objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setName('Logo');
$objDrawing->setDescription('Logo');
$objDrawing->setPath('uploads/organizations/1.jpg');
$objDrawing->setHeight(36);
$objDrawing->setWorksheet($this->excel_reader_writer->getSheetByName($download_section));
The File Structure is
/- application
/- system
/- user_guide
/- uploads
I need a way to upload files to a custom location in stead of the upload folder from wordpress itself.
I googled somewhat but probably using the wrong words.
Can somebody give me a push in the right direction?
Note that I don't ask for the complete script to do this.
I want to understand what I am doing rather than just copy and paste :-)
M.
You can write to any location that the web server process has write permissions. If you want to save an uploaded file to a different path under /wp-content/uploads you could use the wp_upload_dir() function.
if (isset($_FILES['file']) && (!empty($_FILES['file']['tmp_name']))){
if ($_FILES['file']['error'] == UPLOAD_ERR_OK) { // no errors
// get upload directory
$upload_dir = wp_upload_dir();
// append the custom dir (and trailing slash!)
$my_upload_dir = $upload_dir . "my-dir/";
// get the filename of the upload (you might want to filter this)
$filename = $_FILES['file']['name'];
// move the file to your custom directory location and change permissions
#move_uploaded_file( $_FILES['file']['tmp_name'], $my_upload_dir . $filename );
#chmod( $my_upload_dir . $filename, 0644 );
}
}
I am trying to make a link to download a file with symfony2. It does download a file, but it's useless as it is zero octect. I don't know how to make it work. Does anybody know how to do?
The file is in web/uploads/documents/.
Here is the code in the controller:
// here I get the name of the file
$response = new Response();
$response->headers->set('Content-type', 'application/octet-stream');
$response->headers->set('Content-Disposition', sprintf('attachment; filename="%s"', $filename));
return $response;
I tried to write the whole path instead of $filename but it changes all the slashes in underscore.
You forgot to set the content:
$response->setContent(file_get_contents($localFilePath));