FileNotFoundException in File.php line 41 - php

In laravel i am making an application that uploads a file and the user can download that same file.
But each time i click to download i get this error.
The view code:
<h5>Download</h5>
The route code:
Route::get('/download/{fileName}/{fileType}', 'FilesharingsController#download');
The controller code:
public function download($fileName, $fileType){
$downloadPath = public_path(). '/assests/' . $fileName ;
$headers = array(
'Content-Type: application/octat-stream',
'Content-Type: application/pdf'
);
return Response::download($downloadPath, $fileName . '.' . $fileType, $headers);
}
Please not that when i upload a file i remove its extension.
Example: if i upload 'sample.pdf' it is saved as 'sample'.
I have no clue what is wrong as the path in the error is the correct path.
And the file exists in that path. Plz help
The folder Structure:
And the code used to upload the file is:
// Save uploaded file
if ($this->request->hasFile('file') && $this->request->file('file')->isValid()) {
$destinationPath = public_path() . '/assests/';
$fileName = $filesharing->fileName . '.' . $this->request->file('file')->guessClientExtension();
$this->request->file('file')->move($destinationPath, $fileName);
}

Your $downloadPath is missing the file extension. The second parameter of Response::download is the file name shown to the user.
Your variable should look like this:
$downloadPath = public_path() . '/assets' . $fileName . '.' . $fileType;

Related

Laravel move() is not uploading images everytime

I use code below:
$image_name = 'image' . time() . '.' . $request->file('image')->getClientOriginalExtension();
$destinationFolder = public_path('images');
$request->file('image')->move($destinationFolder, $image_name);
but sometime its not working, images are not storing. Im using heroku as host.
Try to use Storage facade :
$path = \Storage::putFile('images', $request->file('image'));
Laravel will generate a name automatically, About Storage facade, just see this. :)
I made function for upload file and if there is no uploaded file do it again till its upload
$imageFile->move($destinationFolder, $fileName);
if(file_exists(public_path('images') . '/' . $fileName))
return public_path('images') . '/' . $fileName;
else
self::uploadImage($imageFile, $fileName);
I used the code and it's working fine.
self::$image = $request->file('image');
self::$imageName = self::$image->getClientOriginalName();
self::$directory = 'category-images/';
self::$image->move(self::$directory, self::$imageName);
return self::$directory.self::$imageName;

Uploaded file just 7 byte in laravel

I'm using laravel and i make an upload file function. The file is uploaded, but the file size is just 7 byte from 8MB
Here's my code:
if(Input::file('audio')){
$file = Input::file('audio');
$filename1 = $slug . '-' . time() . '.' . $file>getClientOriginalExtension();
$path1 = Storage::disk('uploads')->put($filename1, 'uploads');
$part->audio = $filename1;
}
Here's the result :
file property result
Link to the Docs,
You need to provide the file contents to the second argument of
put() method,
change
$path1 = Storage::disk('uploads')->put($filename1, 'uploads');
To
$path1 = Storage::disk('uploads')->put($filename1, file_get_contents($file));

Image Upload is not working in a Laravel Project hosted in a shared server

I have deployed a Laravel project in a shared hosting. I have changed my .env file and copied all files from the public folder to the main directory and deleted the public folder. Now the problem is, whenever I am trying to upload an image, I am getting an internal server error. I suppose the problem is the Image Intervention is not getting the right folder to save the image. I have tried the both ways given below:
if ($request->hasfile('admin_pro_pic')) {
$image = $request->file('admin_pro_pic');
$filename = time() . '.' . $image->getClientOriginalExtension();
$location = public_path('/images/admin/' . $filename);
Image::make($image)->resize(950, 700)->save($location);
$admin->admin_pro_pic = $filename;
}
and
if ($request->hasfile('admin_pro_pic')) {
$image = $request->file('admin_pro_pic');
$filename = time() . '.' . $image->getClientOriginalExtension();
$location = '/images/admin/' . $filename;
Image::make($image)->resize(950, 700)->save($location);
$admin->admin_pro_pic = $filename;
}
But None of these is working. Any possible Solution?
Use laravel base_path function, so your code will look like this
if ($request->hasfile('admin_pro_pic')) {
$image = $request->file('admin_pro_pic');
$filename = time() . '.' . $image->getClientOriginalExtension();
$location = base_path().'/images/admin/' . $filename;
Image::make($image)->resize(950, 700)->save($location);
$admin->admin_pro_pic = $filename;
}
Answer Update
Issue was fileinfo extension missing or disbaled.
Try This.
use Storage;
use File;
if(!empty($request->file('admin_pro_pic')))
{
$file = $request->file('admin_pro_pic') ;
$fileName = $file->getClientOriginalName() ;
$destinationPath = public_path().'/images/' ;
$file->move($destinationPath,$fileName);
$admin->image=$fileName;
}
Create imges inside public directory.
I am handling it like this:
// check for defined upload folder inside .env file, otherwise use 'public'
$publicUploadDir = env('UPLOAD_PUBLIC', 'public/');
// get file from request
$image = $request->file('admin_pro_pic');
// hasing is not necessary, but recommended
$new['path'] = hash('sha256', time());
$new['folder] = 'images/admin/';
$new['extension'] = $file->extension();
// store uploaded file and retrieve path
$image->storeAs($publicUploadDir, implode($new, '.'));

PHP - move_uploaded_file not working to copy file in same folder

Having some trouble with rewriting a photo file. I need the file name to get rewritten as a random string. The file uploads fine - I can't seem to get it copy the file and rewrite the file name to the random string. The file is going to stay in the directory.
The function is working fine and I can rewrite file name in the database, but it will not rewrite the actual file in the folder. The folder permissions are rwxr-xr-x (755).
Any thoughts?
function AfterUpdate(){
$file = $this->file_attachment;
$path_parts = pathinfo($file);
$newFilename = $path_parts['dirname'] . "/" . uniqid() . "." . $path_parts['extension'];
$file_src = $_SERVER['DOCUMENT_ROOT'] . $file;
$newfile_src = $_SERVER['DOCUMENT_ROOT'] . $newFilename;
if (move_uploaded_file($file_src, $newfile_src)){
$this->file_attachment = $newFilename;
}
}
$newFilename contains a path location I guess by looking at the $path_parts['dirname'] . "/" . uniqid() . "." . $path_parts['extension'];.
$newFilename should just be the new file name with extension.
move_uploaded_file will only move files from one folder to another or the same, that already exists. But will not create a folder for you.
Simple fix. Replace move_uploaded_file with rename. The file will not be moved, just renamed.
$file = $this->file_attachment;
$path_parts = pathinfo($file);
$newFilename = $path_parts['dirname'] . "/" . uniqid() . "." . $path_parts['extension'];
$file_src = $_SERVER['DOCUMENT_ROOT'] . "/" . $file;
$newfile_src = $_SERVER['DOCUMENT_ROOT'] . "/" . $newFilename;
if (rename($file_src, $newfile_src)){
$this->file_attachment = $newFilename;
}

file_get_contents() cannot read PHP tags <?php ?>

I am using cakePHP and got stuck with this riddle.
Time ago i made function that inserts custom routes dynamically in file using file_get_contents() and string replace functions.
$filename = "301routes.php";
$path = SERVER_PUBLIC_PATH . 'app' . DS . 'config';
$add_data = "Router::connect('".$old."', array('controller'=>'index_router', 'action'=>'redirect_301','".$new."'));\n";
$file_data = file_get_contents($path . DS . $filename);
$file_data = str_replace("<?php\n","<?php\n".$add_data,$file_data);
file_put_contents($path . DS . $filename, $file_data);
this worked, but now i tried to put exactly the same function in other project and its working only when there is no PHP tag. Problem is with file_get_contents() function becouse it reads file well when content is:
// routes here
Router::connect('/articles/category/en/test', array('controller'=>'index_router', 'action'=>'redirect_301','/articles/category/en/test-old'));
but when i change beginning like this
<?php
Router::connect('/articles/category/en/test', array('controller'=>'index_router', 'action'=>'redirect_301','/articles/category/en/test-old'));
?>
file_get_contents() returns only part of file:
string(154) "'index_router', 'action'=>'redirect_301','/articles/category/en/test-old')); ?>"
Can someone help me with this?
To run php codes and assign to $file_data variable,
ob_start();
$file_data = file_get_contents($path . DS . $filename);
ob_end_flush();
Try: Create new file 301routes.php in config directory then read write permission of file.
$filename = "301routes.php";
$path = __DIR__;
$add_data = "Router::connect('".$old."', array('controller'=>'index_router', 'action'=>'redirect_301','".$new."'));\n";
$file_data = file_get_contents($path . DS . $filename);
$file_data = str_replace("<?php\n","<?php\n".$add_data,$file_data);
file_put_contents($path . DS . $filename, $file_data);

Categories