Path issues in windows when using storeAs - Laravel 5.4 - php

I am trying to upload a file to public/attachments/foo.jpg in Laravel using storeAs(), it is working correctly in ubuntu but not in windows.
if($isValidated){
$newFileName = '';
foreach($files as $upload){
$fileName = preg_replace('/\s+/', '_', pathinfo($upload->getClientOriginalName())['filename']);
$newFileName = $fileName.'_'.$upload->uploadTime.'.'.$upload->getClientOriginalExtension();
$upload->storeAs('public/attachments', $newFileName);
}
}
This block of code successfully uploads a file in /public/attachments/foo.jpg
But when I try this in windows platform I get a error saying fopen ... failed to open stream : Invalid aruguments.
I have attached the screenshot of the error .
NOTE :
I have added symlink like so php artisan:storage link
Using Laravel 5.4

Problem is in the name of the file. It contains a colon :, which is not allowed on Windows.

Related

PHP - Converting Excel to PDF (Phpspreadsheet) - Operation not permitted

I am trying to convert an Excel file to PDF (Base64).
This is the code that converts the Excel to PDF:
$spreadsheet = $this->objPHPExcel = \PhpOffice\PhpSpreadsheet\IOFactory::load("MyExcelFile.xlsx");
$class = \PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf::class;
\PhpOffice\PhpSpreadsheet\IOFactory::registerWriter('Pdf', $class);
$this->objPHPExcel = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Pdf');
$this->objPHPExcel->writeAllSheets();
//Save the file. (THIS IS WHERE THE ERROR OCCUR)
$this->objPHPExcel->save(storage_path() . '/app/temp_files/' . $newFileName);
Everything works locally, but whenever I try to run the same code on my Laravel Forge server, I get below error:
unlink(/tmp/imagick-3.4.0.tgz): Operation not permitted
If I trace the error, it is in this specific line:
$this->objPHPExcel->save(storage_path() . '/app/temp_files/' . $newFileName);
As said, this code runs fine locally. The temp file $newFileName is created inside my /temp_files folder.
What am I doing wrong?
Ok so the solution to this was rather tricky. I found out that it had nothing to do with Phpspreadsheet but rather Mpdf.
The problem was that the file "imagick-3.4.0.tgz" file permission was set to read only. Mening that unlink could not work on this specific file. This goes all the way back to when I first installed the imagick library.
The solution was to go to the /tmp folder and delete the imagick-3.4.0.tgz file manually. This folder should actually be deleted when doing the imagick installation.

laravel path of files in (windows) localhost

This test is on localhost/windows/php/apache/mysql
For example:
in root of Public folder:
$path = '/upload/images/accounts/12005/thumbnail_profile_eBCeBawXWP.jpg';
I want use it:
$img->save($path);
I get this error:
"Can't write image data to path
(/upload/images/accounts/12005/thumbnail_profile_eBCeBawXWP.jpg)"
I try to fix it:
$path = public_path($path);
$img->save($path);
So I get this error:
"Can't write image data to path
(D:\Server\data\htdocs\laravel\jordankala.com\public_html\ /upload/images/accounts/12005/thumbnail_profile_pH657T62fl.jpg)
probably, in real server (linux), I won't have this problem and the code words.
Now how can I manage this error? (I want it works in windows localhost and real linux server)
You can use this to save at your path
$file = $request->file('image_field_name');
$destinationPath = 'upload/images/accounts/12005/';
$uploadedFile = $file->move($destinationPath,$file->getClientOriginalName()); //move the file to given destination path

laravel 5.5 get file url from storage

I am working on a project in which i want to get the link of the file which i uploaded in the storage-app-public directory of laravel. I followed this link StackOverflow question, but when I try to run following command in Vagrant:
php artisan storage:link
It gives following error:
[Symfony\Component\Console\Exception\CommandNotFoundException]
There are no commands defined in the "storage" namespace.
you need to follow this code, I think It will be helpful for you.
$imageTempName = $userdata->file('image_media')->getPathname();
$imageName = $userdata->file('image_media')->getClientOriginalName();
upload the file to the database within the directory using Laravel.
<?php
public static function upload_file(Request $userdata){
$imageTempName = $userdata->file('image_media')->getPathname();
$imageName = $userdata->file('image_media')->getClientOriginalName();
$path = base_path() . '/public/uploads/images/';
$userdata->file('image_media')->move($path , $imageName);
$data=array('media'=>$imageName);
$insert=with(new UserModel)->uploadFile($data);
}
?>

move_uploaded_file() working on localhost but on server

The following code works perfect on my localhost but it shows the following errors on my live server
Warning: move_uploaded_file(.../uploads/76948893.jpeg): failed to open stream: No such file or directory
Warning: move_uploaded_file(): Unable to move '/tmp/phppxvRs8' to '.../uploads/76948893.jpeg'
What it does is simple, it takes the images on the array ["pictures"] which comes from a html form and save every image on the folder ".../uploads/" using a random numeric name as name of the file and keeping the original extension.
Any one knows how to make it work on my server?
//Image Uploader
$images=[];
$directory = '.../uploads/';
foreach ($_FILES["pictures"]["error"] as $key => $error) {
$new_file_name = rand (10000000,99999999);
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["pictures"]["tmp_name"][$key];
$name = $_FILES["pictures"]["name"][$key];
/* echo '<br>';
echo $directory.$new_file_name.".".substr($_FILES['pictures']['type'][$key],strpos($_FILES['pictures']['type'][$key], "/")+1);*/
if(move_uploaded_file($_FILES['pictures']['tmp_name'][$key],
$directory
.$new_file_name
.".".substr($_FILES['pictures']['type'][$key],strpos($_FILES['pictures']['type'][$key], "/")+1))) {
array_push($images,$new_file_name.".".substr($_FILES['pictures']['type'][$key],strpos($_FILES['pictures']['type'][$key], "/")+1));
$images_validator=true;
}else{
//Error
}
}
}
There could be many reason for this, Check the following
You need WRITE Permission for the uploads directory. I assume your local machine runs windows & your hosting environment is linux
Like #Darren suggests, use absolute path. change the $directory to $directory = getcwd() . 'uploads/';
If this runs on your local machine but not on server there are 2 easy answers I can think off right off the back. 1. The folder doesnt exist on the server, 2. as someone comment you dont have permission to write/read to that folder on the server....I would check the server configuration to make sure your app pool or users have read/write permissions to the folder

php fopen in ubuntu file systen

I want to download a file using php. Everything is working fine on windows, but when I try to run the same php code on my ubuntu 12.04 just an empty file gets downloaded without any content. When I try to run the following code:
<?php
$filename = '/root/my_folder/filename.pdf';
if(file_exists($filename))
$f = fopen( '/root/my_folder/filename.pdf', 'r') or exit('unable to open file');
else
echo 'file does not exists';
?>
It always display the 'file does not exists'. Is the issue with setting path to my folder ?
Can anyone help me with this. I'm new in Ubuntu. Thanks
your apache user does not hae the permission to access /root/myfolder/filename.pdf . You will have to either add apache user to the group that is accessing /root/myfolder or change the ownership of filename.pdf or change the permissions on /root/myfolder

Categories