32,000+ SUBSCRIBERS CAN'T GO WRONG !!!

phpans
GET TOP ARTICLES, SNIPPETS, SCRIPTS IN YOUR INBOX FOR FREE!

Delivered by GET EMAIL UPDATES FeedBurner
We always respect your privacy and take protecting it seriously.
We won't Spam You

PHP Secure Image Upload From URL

, 0,
PHP Secure Image Upload From URL
Hi geeks, In this tutorial, I am going to show you How to upload an image from URL using PHP with a secure way. We have already published an article about PHP Secure Image Upload now we are going to show you PHP Secure Image Upload From URL. This method helps you to insert an image from the web easily. You can insert images from other websites. I hope you have already seen this system in WordPress media upload system. WordPress media upload has two figure. Upload from computer and media insert from URL. This same like this. Let's start our main session. We should make this script secure. So we should add some additional security. Follow my instructions step by step.

Step-1. Create Functions For Image Upload


We have to create necessary functions for image upload using URL. Create a PHP file name as class.php and write down these code bellow.

class.php


<?php

class Image
{


public function featch_file($url)
{
return basename($url);
}


public function valid($file)
{
$allow = array('jpg','png','jpeg','gif');
$break_file = explode('.',$file);
$extension = end($break_file);
if(in_array($extension,$allow))
{return true;}
else
{return false;}
}

public function insert_file($url,$directory)
{
$new_file = self::featch_file($url);
$path = $directory.$new_file;
$data = file_get_contents($url);

if (file_put_contents($path,$data))
{
return true;
}
else
{
return false;
}
}

public function insert($url,$dir)
{
$image = self::featch_file($url);
if(self::valid($image))
{

if(self::insert_file($url,$dir))
{
echo $image. ' inserted successfully';
}
else
{
echo 'Unknown error!';
}
}
else
{
echo 'Invalid file type';
}

}

}
?>

We have created our necessary functions. featch_file() functon retrive file name from a URL, valid() function will check requested file is valid image. insert_file() function insert image to server. we have used file_put_contents() function for save image in our server. We can also use copy() function for save image to our server. Our next used function is insert(). This function process whole upload system. Our functions are ready to use. Now we need to see how to use these functions to upload image through URL. Now we are going to our next step.

Step-2. HTML and PHP markups for Display Uploader


In this step, we have to define our necessary HTML and PHP codes for display the uploader. Create a file name as index.php or upload.php and write down this code bellow. I have created a file name as index.php in my demo version. You can create another as you want.

index.php


<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd"> 
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="UTF-8">
<title>PHP Secure Image Upload From URL</title>
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<link href="styles/styles.css" rel="stylesheet">
</head>
<body>
<div class="container">
<h1 class="text-center">PHP Secure Image Upload From URL</h1>
<div class="content text-center">
<?php
include_once 'class.php';
$image = new Image;

if(isset($_POST['url']))
{
$url = $_POST['url'];

$image->insert($url,'images/');
}
?>
</div>
<div class="content text-center">

<form class="form-inline" method="post">
<div class="form-group">
<label for="channel">Image Url</label>
<input type="text" name="url" class="form-control" placeholder="Image Url">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>

</div>
</div>
</body>
</html>

Here we have written codes for display uploader and process upload system. Full uploading process has completed into this code. Here a form to put image URL which you want to upload and a submit button. When you click on submit with an image URL then our uploading process will work.
if(isset($_POST['url']))
{
$url = $_POST['url'];

$image->insert($url,'images/');
}

This is our PHP code for upload. $image->insert($url,'images/'); has two parameter. One for URL another for the destination that means where uploaded image will be stored. I have used images/ as my upload directory. You can change as you want, don't forget to put a slash after the directory name. Otherwise, this function uploads image in the current directory where your script will be located.
php-secure-image-upload-from-url-demo
We have used bootstrap for displaying uploader. You can change like yours. We added a stylesheet for this. If you want to create an instant demo for check whole system then create a directory name as styles and create a CSS file in that directory name as styles.css and write these stylesheet codes.

styles.css


body
{
background-color: #0F6D53;
font-family: verdana;
color: #FFF;
}
a,a:hover,a:active,a:link{color:#FFF;transition: all 0.1s ease;-webkit-transition: all 0.1s ease;-moz-transition: all 0.1s ease;}
a:hover{font-style:italic;text-decoration:none}
.header
{
padding: 10px;
font-size:25px;
}
.title
{
padding: 10px;
font-size:20px;
}
.content
{
padding: 20px;
}


Here is our stylesheet which is used on demo. Now our image uploader is completely ready. I hope you understand this. I have attached full project with this article if you want you can download and use this. You are fully free to edit and use this code in your projetcs. If you have any question or comment about this PHP Secure Image Upload From URL article please don't hesitate to do that, please comment your question in comment section. We always appreciate valuable comments.

Download PDF
Signup & Get $25 Free
Is this article helpful for you?





captcha

  • Connect
  •       
Top
Create an free account and start learning & sharing! Sign Up