I'm trying to upload a file. Heres my controller
if ($request->hasFile('photo')) {
$destinationPath = 'images/quizzes/'; // upload path
$extension = $file->getClientOriginalExtension(); // getting image extension
$fileName = rand(11111,99999).'.'.$extension; // renameing image
$request['photo'] = $fileName;
$request['image_path'] = $destinationPath;
$file->move($destinationPath, $fileName); // uploading file to given path
$quiz = Quiz::create($request->all())
}else{
\Session::flash('flash_message_delete','Please select files to upload.');
return redirect()->back();
}
It gives me FileNotFoundException in File.php line 37:The file "" does not exist. This was working fine the other day, idk what happes..i try to interchange the arrangement like:
$destinationPath = 'images/quizzes/'; // upload path
$extension = $file->getClientOriginalExtension(); // getting image extension
$fileName = rand(11111,99999).'.'.$extension; // renameing image
$request['photo'] = $fileName;
$request['image_path'] = $destinationPath;
$quiz = Quiz::create($request->all());
$file->move($destinationPath, $fileName); // uploading file to given path
I wont have error like this, and it save to the database, THE PROBLEM is the photo field instead of the $filename it has wierd /tmp/somestrings inserted.
Related
I created a form to store article with an image , and generate a resized version as thumbnail from it.
I want the image to be renamed after the article slug and stored in the "public/img/articles-images " directory but i keep receiving : "Image source not readable" error
This is the image upload handler function in my controller :
private function handleRequest($request)
{
$data = $request->all();
if ($request->hasFile('image')) {
$image = $request->file('image');
$fileName = $request->slug;
$successUploaded = $image->storeAs('img/articles-images', $fileName);
if($successUploaded) {
$width = config('cms.image.thumbnail.width');
$height = config('cms.image.thumbnail.height');
$extension = $image->getClientOriginalExtension();
$thumbnail = str_replace(".{$extension}", "_thumb.{$extension}", $fileName);
Image::make('img/articles-images' . '/' . $fileName)
->resize($width, $height)
->save('img/articles-images' . '/' . $thumbnail);
}
$data['image'] = $fileName;
}
return $data;
}
storeAs() method, which receives the path, the file name, and the (optional) disk as its arguments :
$successUploaded = $request->file('image')->storeAs(
'images', $fileName
);
I solved it ! Apparently there was no need to storeAs() method at all , the new code is like below :
if ($request->hasFile('image')) {
$image = $request->file('image');
$fileName = $request->slug.'.' .$image->getClientOriginalExtension();
$destination = $this->uploadPath;
$successUploaded = $image->move($destination, $fileName);
// //
I have difficulty understanding the file save and retrieval in Laravel. I managed to get the file saved into correct path
$fileNameWithExt = $request->file('Agreement_file')->getClientOriginalName();
$fileName = pathinfo($fileNameWithExt, PATHINFO_FILENAME);
$extention =$request->file('Agreement_file')->getClientOriginalExtension();
$filenameToStore = $fileName . '_' . $lab_id. '.'.$extention;
$request->Agreement_file->storeAs('agreements', $filenameToStore );
However not I want to create an a-tag to download the file, but cannot manage to get to download the file.
{{$filenameToStore}}
The file download but I get the error "Failed- Server Problem". I do not want to use a same link as these files are confidential and should not be able to be downloaded outside the app.
Something like that?
Step 1:
Create table files id | filename | user_id.
Step 2:
Create model File
Step 3:
Add file row in table.
$fileNameWithExt = $request->file('Agreement_file')->getClientOriginalName();
$fileName = pathinfo($fileNameWithExt, PATHINFO_FILENAME);
$extention =$request->file('Agreement_file')->getClientOriginalExtension();
$filenameToStore = $fileName . '_' . $lab_id. '.'.$extention;
$request->Agreement_file->storeAs('agreements', $filenameToStore );
File::create([
'filename' => $filenameToStore,
'user_id' => Auth::id()
]);
Step 4:
Create controller method download.
public function download(){
$filename = $request->input('filename');
$file = File::where('user_id', Auth::id())
->where('filename', $filename)
->firstOrFail();
$path = Storage::path('agreements/' . $filename);
if(Storage::exists($path)){
return Response::download($path, $filename);
}
}
Step 5:
Replace your link:
{{$filenameToStore}}
Or you can build it based on the file ID.
When i save image with my form i change the file name. Everything works nice except file extension. When i change file name the extension is missing and file is saved without it. Please help me to solve my problem i want to store also image extension.
This is my code where i update image name.
$image = $request->file('image');
if ($image) {
$imgPath = 'public/post-image/';
$imgName = uniqid('img_');
$store = $request->file('image')->storeAs(
$imgPath, $imgName
);
$post->image = $imgName;
}
Try this:
$image = $request->file('image');
if ($image) {
$imgPath = 'public/post-image/';
$imgName = uniqid('img_') . '.' . $image->extension();
$store = $image->storeAs($imgPath, $imgName);
$post->image = $imgName;
}
I want to change image name with current time stamp in php.
My code are below :
$logo = basename($_FILES['file']['name']);
$logo = image1.jpg
move_uploaded_file($_FILES['file']['tmp_name'], '../timeTableImg/' . $_FILES['file']['name']);
i get image name in $logo. But Actually i want
$logo = 1484900616.jpg
move_uploaded_file($_FILES['file']['tmp_name'], '../timeTableImg/' . $_FILES['file']['name']);
file name may be dynamic. its may be jpg , png, jpeg. Also want to move file with new name.
Use move_uploaded_file function.
Example:
$file_path = "uploads/";
$newfile = date('m-d-Y_H:i:s')'.jpg';
$filename = $file_path.$newfile;
if(!file_exists($filename))
{
if(move_uploaded_file($_FILES['file']['tmp_name'],$filename))
{
// Other codes
}
}
else
{
echo 'file already exists';
}
Try this.
$path = $_FILES['file']['name'];
$ext = pathinfo($path, PATHINFO_EXTENSION);
$logo = time().'.'.$ext;
And to use the new name. put this.
move_uploaded_file($_FILES['file']['tmp_name'], '../timeTableImg/' . $logo);
I am trying to upload an image using Laravel it is working to the point that the images are being successfully placed into the destination folder but when I try to view them within my IDE I am told they are corrupt and that netbeans cannot open them.
I am wondering if my upload process is causing this?
This is my code:
public function handleCreate(){
$book = new Book;
$book->title = Input::get('title');
$book->desc = Input::get('desc');
//Img uploading...
$destinationPath = '';
$filename = '';
if(Input::hasFile('cover')){
$file = Input::file('cover');
$destinationPath = public_path().'/img/';
$filename = str_random(6) . "_" . $file->getClientOriginalName();
$uploadSuccess = $file->move($destinationPath, $filename);
}
$book->cover = $filename;
$book->save();
return Redirect::route('books')->with('msg', 'test');
}
Any help is much appreciated. I know the image is not corrupt before uploading as I can view on my desktop. The issue is only after uploading it.