Storing image to Public folder Laravel 5.1 - php

I'm trying to save an image to a folder in my laravel application.
I'm getting the error:
fopen(F:\blog\public/usr-data/photos): failed to open stream: Permission denied
Here's my laravel controller which is writing to this folder.
$error = false;
$absolutedir = public_path();
$dir = '/usr-data/photos';
$serverdir = $absolutedir.$dir;
$filename = array();
foreach($_FILES as $name => $value) {
$json = json_decode($_POST[$name.'_values']);
$tmp = explode(',',$json->data);
$imgdata = base64_decode($tmp[1]);
$fileAry = explode('.',$json->name);
$extension = strtolower(end( $fileAry ));
$fname = $card->id.'.'.$extension;
$handle = fopen($serverdir,'w');
fwrite($handle, $imgdata);
fclose($handle);
$filename[] = $fname;
}
I've tried using
Icacls "F:\blog\public/usr-data/photos" /grant Everyone:(OI)(CI)F
But no joy - still the same issue.

You need to set up writing permissions on a storage and public folders and then all folders inside these ones. Use right click on a folder, go to Properties and change folder permissions.
http://m.wikihow.com/Change-File-Permissions-on-Windows-7

EDIT: This will work for Linux only. I'm keeping this answer in case anyone runs into the same problem on a Linux machine.
Try this:
php artisan cache:clear
sudo chmod -R 777 app/storage
composer dump-autoload

Instead of using file write, you can also use the Laravel filesystem storage class. Please try with the below given example.
$image represents the encoded image.
$Id represents the id of the user.
public function imageUpload($image, $Id)
{
//Decode base64 string to image
$profile_image = 'image/jpeg;base64,' . $image;
$new_data = explode(";", $image);
$data = explode(",", $new_data[1]);
$file = base64_decode($data[1]);
$imageFileName = $Id . '.jpeg';
$image_path = '/storage/' . $imageFileName;
Storage::put($imageFileName, $file);
return $image_path;
}

Related

PHP copy command folder

My currency working directory is ../main
I have a script which accepts files to be uploaded.
I have the following code but instead of coping the file into the ../_assets folder, it copies it into the ../main folder with the filename as ../_assets/correct.csv.
Any idea what might be wrong here?
Thanks
const IMPORT_FILE_DIR = "..\_assets\\";
private $file_name = "";
$this->file_name = Page::IMPORT_FILE_DIR . $_FILES['file_in']['name'];
$result= copy( $_FILES['file_in']['tmp_name'],$this->file_name);
Here fileName is = "..\_assets\correct2.csv"

laravel - Uploading directory path from linux folders

I make variable that contain the path of the directory where the uploading files will be stored. I wonder this works very well on windows but not on linux ubuntu. Perhaps the way I mention directory is wrong.
Following controller on Laravel;
public function Store(Request $request){
$data = array();
$data['product_name'] = $request->product_name;
$data['product_code'] = $request->product_code;
$data['details'] = $request->details;
$image = $request->file('logo');
if($image){
$image_name = date('dmy_H_s_i');
$ext=strtolower($image->getClientOriginalExtension());
$image_full_name=$image_name.'.'.$ext;
$upload_path='home/laravel/udemy/kash';
$image_url=$upload_path.$image_full_name;
$success = $image->move($upload_path,$image_full_name);
$data['logo']=$image_url;
$product=DB::table('products')->insert($data);
return redirect()->route('product.index')
->with('success','Product Created Successfully');
}
I found solution finally. $upload_path='$home/laravel/udemy/kash/';

Drupal 8: The file could not be created

I have below code to upload excel file provided by API in base64_encode format.
I would like to download that file and save in to file system.
public function import(Request $request) {
if ($request->getMethod() === 'POST') {
$data = json_decode($request->getContent(), TRUE);
$directory = 'public://import/';
// create directory if it does not exist
file_prepare_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
$uri = $directory. $data['name'];
$files_data = preg_replace('#^data:application/\w+;base64,#i', '', $data['data']);
$file_data = base64_decode($files_data);
$file = file_save_data($file_data, $uri, FILE_EXISTS_RENAME);
echo $file->id();
}
}
It goes to below function and gives error, although I have created tmp folder on project root and given needed 777 permission.
/drupal/web/core/includes/file.inc
function file_unmanaged_save_data($data, $destination = NULL, $replace = FILE_EXISTS_RENAME) {
// Write the data to a temporary file.
$temp_name = drupal_tempnam('temporary://', 'file');
if (file_put_contents($temp_name, $data) === FALSE) {
\Drupal::messenger()->addError(t('The file could not be created.'));
return FALSE;
}
// Move the file to its final destination.
return file_unmanaged_move($temp_name, $destination, $replace);
}
What I am missing? It is not converting and saving file.
Usually this happen because of wrong temporary location and permission, what I suggest you is go to /admin/config/media/file-system and make sure you set Temporary Directory correctly. if its fine. check the permission of it. you need to set it writeable by webservice.
Fixed
On URL : domain.com/admin/config/media/file-system
Temporary directory -> Folder location and permission issue.

File upload not working with laravel $file on some machine

In my Laravel project I created a page to upload the files and I use the $file of laravel it works fine for some system only but for some system it shows an error as shown in image below.
Function I am using to upload files in model
public function add_document_sub_cert($req)
{
$subcontractor_id = $req['subcontractor_id'];
$reference_id = $req['reference_id'];
$files = $req->file("uploaded_doc0");
$i = 0;
foreach($files as $file){
$i++;
$ext = $file->guessClientExtension();
$name = $file->getClientOriginalName();
$file_name_1 = str_replace(".".$ext,"",$name);
$path = $file->storeAs('subcontractor/','avc'.$i.'.jpg');
if($path){
$document = new Document();
$document->doc_name = 'avc.jpg';
$document->module = 'subcontractor';
$document->reference_id = $reference_id;
$document->save();
}
}
}
Your error says that you didn't specify a filename. I see that your variable $file_name_1 is never used. Haven't you forgotten to use it somewhere?
Without knowing how your class Document works, it's impossible to tell you exactly where is the bug.

Zend_Form_Element_File the move_uploaded_file function don't return anything

I am trying to upload a picture. I have Form_Zend and I use:
$image = new Zend_Form_Element_File('image');
$image->setLabel('Upload an avatar:')
->setMaxFileSize(8388608)
// ->setDestination('./usersImages')
->setDescription('Click Browse and choose an image');
$image->addValidator('Count', false, 1);
$image->addValidator('Size', false, 8388608);
$image->addValidator('Extension', false, 'jpg,jpeg,png,gif');
$this->addElement($image, 'image');
My controller action code:
if ($form->image->isUploaded()) {
$values = $form->getValues();
$source = $form->image->getFileName();
$extention = substr($source, strrpos($source, '.', -1));
$date = date('mdYhisa', time());
$new_image_name = 'avatar_' . $date . '_' . $idUser . $extention;
$destination = "C:\\xampp\\tmp\\Srututututut.png";
$image_saved = move_uploaded_file($source, $destination);
if ($image_saved) {
$data = array(
'img' => $new_image_name,
);
$userDT->update($data, 'id=' . $idUser);
}
}
}
But this move_uploaded_file is not returning nothing :/
What I have done:
Checked if the file is uploading - yes it is in: C:\xampp\htdocs\Story\public\usersImages (if I set destination in this form element) or
C:\xampp\tmp (if I dont set it)
I was wondering about access to this folders but if it save there this images I think it has rights but I set in the apache:
<Directory "C:/xampp/htdocs/Story/public/usersImages">
Allow from All
</Directory>
I was even tried use this function only in C:\xampp\tmp folder:
$source: C:\xampp\tmp\database.png
$destination: C:\xampp\tmp\Srututututut.png
And still nothing :/
Do You have any suggestions?
I think that the problem is with $source = $form->image->getFileName();. The reason is that it will return a name of the file uploaded rather than where it was uploaded to (i.e. its temporary localization).
Thus, I think your source should be as follows:
$fileInfo = $mainForm->image->getTransferAdapter()->getFileInfo();
$source = $fileInfo['image']['tmp_name'];
// to check if the source really points to the uploaded file.
var_dump(file_exists($source));
Ok,
I have no idea why this function is not working. I have changed my idea to set the $form->image destination first in the controller and then rename it and it is working.
Thanks for help guys ;D

Categories