Today In this quick tutorial I am going to tell you how you can Download am an image from Url and stored in an image folder in laravel 5.5. Also, we will save the image name into the database. So How will you do that? Generally, let me tell you one thing you can do this type of functional module in many ways. Maybe with making separate controllers or direct handling in route methods right.

So here we go and let’s start to build it within an example.

Database migration file:-



<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePhotosTable extends Migration {
	/**
	 * Run the migrations.
	 *
	 * @return void
	 */
	public function up()
	{
		Schema::create('photos', function(Blueprint $table)
		{
			$table->increments('id');
			$table->string('title'); //title....
			$table->string('filename'); //filename....
			$table->timestamps(); //the migrate....
		});
	}
	/**
	 * Reverse the migrations.
	 *
	 * @return void
	 */
	public function down()
	{
		Schema::drop('photos');
	}
}

#1:- Create a View inside your Resources->viewfolder.

View:- Index.blade.php


<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>

<div class="container">

	<h1>Image Save from URL</h1>

	<form action="/urlfilesave" method="post" enctype="multipart/form-data">
		<input type="hidden" name="_token" value="{{csrf_token()}}">

		<div class="form-group">
			<label for="">URL</label>
			<input type="text" class="form-control input-sm" name="url">
		</div>

		<div class="form-group">
			<label for="">Title</label>
			<input type="text" class="form-control input-sm" name="title" autocomplete="off">
		</div>
		<button class="btn btn-primary">Save</button>
	</form>

</div>

#2:- Also you need to Create a folder called images or whatever in you are going to store images inside the public directory.

#3:- Create a Model called Photo.php inside app folder.



<?php namespace App;

use Illuminate\Database\Eloquent\Model;

class Photo extends Model {

	protected $fillable = ['title','filename'];

}


#4:- Now, after doing these all it’s time to create functional routes route->web.php


<?php


use App\Photo;

Route::get('/', function(){
	return View::make('index');
});


Route::post('urlfilesave',function(){

	$url = Input::get('url');
	$title = Input::get('title');
	$extension = pathinfo($url, PATHINFO_EXTENSION);

	$filename = str_random(4).'-'.str_slug($title).'.'. $extension;

	//get file content from url
	$file = file_get_contents($url);
	$save = file_put_contents('images/'.$filename, $file);

	if($save){
	

		DB::beginTransaction();
		try {

			Photo::create([
				'title' => $title,
				'filename'=> $filename
			]);
			var_dump('file downloaded to images folder and saved to database.......');
			DB::commit();

		} catch (Exception $e) {
			//delete if no db things........
			File::delete('images/'. $filename);
			DB::rollback();
		}

	}

});


Note:- Please input the url with http://

Generate key by following command

php artisan key:generate

All database configuration will alter in .env file.

Database will migrate by following command

php artisan migrate

Krishna Gupta

Krishna Ballabh Gupta is a Gold Medalist Full Stack Developer & Algorithm Designer and Founder of TechnologyShouters. He generally Blogs about Python, JavaScript, jQuery, AngularJS, Database, PHP, AJAX and something that interesting.

Related Posts

Create Account



Log In Your Account



1Subscribe to Our Live Updates.