Laravel rename file while uploading


Laravel rename file while uploading – The Laravel rename file while uploading is used to rename the file or image during uploading.


Laravel rename file while uploading | with full Example.

Let us understand how to use Laravel rename file while uploading.

Full example of rename file while uploading.

Now here i am going to explain how to rename file during uploading.

Create a controller page and save as [RenameController.php].

Let’s look at a simple example.

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;

class RenameController extends Controller 
{
	public function renameDisplay()
	{
		return view('Rename');
	}
	public function renameFile(Request $request)
	{
	if (Input::hasFile('file'))
	{
        $file = Input::file('file');
	$destinationPath = '../files';
	$filename = rand().time();
        $file ->move($destinationPath, $filename);
	echo "Uploaded</br>";
	}
	}
}

Then create a view page and save as [Rename.blade.php]

Let’s look at a simple example.

<!DOCTYPE html>
<html>
<head>
    <title>Laravel Rename file</title>
    <link rel="stylesheet" href="http://getbootstrap.com/dist/css/bootstrap.css">
</head>
<body>
<form action="rename" method="post" enctype="multipart/form-data">
<br><br><br><br>
<table align="center">
<tr><td><b>File Rename</b></td>
<td><input type="file" name="file"></td></tr>
<tr><td>
<input type = "hidden" name = "_token" value = "<?php echo csrf_token() ?>"></td></tr>
<tr>
<td></td>
<td>
<input type="submit" name="upload" value="rename"></td>
</tr>
</table>
</form>
</body>
</html>

Route Path:-

Route::get('/RenameDisplay','RenameController@renameDisplay');
Route::post('/rename','RenameController@renameFile');
Laravel rename file while uploading | with full Example

Storage Folder:-

Laravel rename file while uploading | with full Example

Advertisements

Add Comment

📖 Read More