In this exercise, I will reveal to you how to make a picture from URL and save it as a picture in a required index utilizing PHP. You need to enter finish URL path of the picture in the content box and after that click a button to get an image with specified file name.
I compose here fundamental basic script to get and spare it as a picture document from URL. Before running with this post, you should think about file_get_contents() and file_put_contents() methods in PHP.
file_get_contents() is the preferred way to get the contents of a file into a string. It will use memory mapping methods. file_put_contents() is use to write a string to a file it will done with the help of fopen(), fwrite() and fclose() functions.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
<html> <head> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"> </head> <body> <div class="row"> <div class="col-md-offset-2 col-md-6"> <form method="post" action=""> <div class="form-group"> <label for="email">Place images URL:</label> <input type="text" class="form-control" id="image_url" name="image_url" placeholder="Place Image URL" required> </div> <button type="submit" class="btn btn-success" id="sumbit" name="sumbit" >Submit</button> </form> </div> <div class="col-md-3"> <?php if(isset($_POST['sumbit'])) { $image_url = $_POST['image_url']; // Remove all illegal characters from a url $image_url = filter_var($image_url, FILTER_SANITIZE_URL); if (!filter_var($image_url, FILTER_VALIDATE_URL) === false) { $image_data = file_get_contents($image_url); $new_image = 'uploads/'.md5(date('dmYhis')).'.jpg'; $move_image =file_put_contents($new_image, $image_data); if($move_image) { echo "<img src='$new_image'>"; } } else { echo("<b>$image_url</b> is not a valid URL"); } } ?> </div> </div> </body> </html> |