1

Have url for image and need upload in my server.

JS code in HTML:

var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\/)|(\?v=|\&v=))([^#\&\?]*).*/;
var match = link.match(regExp);
var img = "http://i.ytimg.com/vi/"+match[8]+"/hqdefault.jpg";

jQuery.ajax({
    url: 'youtubeimages/upload.php',
    method: 'POST',
    data: img
});

upload.php:

<?php
    if($_FILES["file"]["name"] != '')
    {
     $test = explode('.', $_FILES["file"]["name"]);
     $ext = end($test);
     $name = rand(100, 999) . '.' . $ext;
     $location = './upload/' . $name;  
     move_uploaded_file($_FILES["file"]["tmp_name"], $location);
     echo '<img src="'.$location.'"/>';
    }
?>

But not success... How to upload?

Or how to pass this link a img to this php script?

<?php
$content = file_get_contents("U_R_L");
$fp = fopen("upload/image.jpg", "w");
fwrite($fp, $content);
fclose($fp);
?>
2
  • 3
    Your not uploading an image, but instead just POSTing a string.. you would need to serverside grab the image with curl or fgc (after again doing validation). Commented Aug 31, 2018 at 10:28
  • Please see, my first post edited. Commented Aug 31, 2018 at 11:02

1 Answer 1

0

Okay. I finally created "YouTube Thumbnail downloader", but please correct if you have any other good idea.

JS:

var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\/)|(\?v=|\&v=))([^#\&\?]*).*/;
var match = link.match(regExp);
var videoId = match[8];

jQuery.ajax({
    url: 'youtubeimages/upload.php',
    data: {videoId: videoId}
});

PHP:

<?php
$videoId = $_GET['videoId'];
$path_to_save_thumbnails = "";
$ch = curl_init();
$thubnail_types = array('hqdefault');

foreach($thubnail_types as $type) 
{
    $youtube_thumb_url = 'http://img.youtube.com/vi/'.$videoId.'/'.$type.'.jpg';

    curl_setopt($ch, CURLOPT_URL, $youtube_thumb_url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $image = curl_exec($ch);
    $info = curl_getinfo($ch);

    if($info['http_code'] == 200) {
        file_put_contents($path_to_save_thumbnails.$videoId.'.jpg', $image);
    }
}

curl_close($ch);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.