LPology / Simple-Ajax-Uploader

Javascript file upload plugin with progress bar support. Works in all major browsers, including IE7+, Chrome, Firefox, Safari, and Opera. No dependencies - use it with or without jQuery.
995 stars 268 forks source link

Uploading Images from a URL #41

Closed dashawk closed 10 years ago

dashawk commented 10 years ago

Hello again,

Can we upload images or files from a given URL?

I am trying to upload photos from my facebook albums. I have a modal window from bootstrap containing my facebook albums, when I click an album, my photos will show up. What I want is that, when I click on a photo, it should upload that photo in my photos folder.

Maybe you could help me on this one.

Thanks

LPology commented 10 years ago

Well I've never used Facebook's API, but it doesn't seem like it would be too tough. This is just off the top of my head, but here's how I'd guess you go about it:

  1. Get the URL of the photo
  2. Send the photo URL to a server-side script in an ajax request
  3. From the server-side script, retrieve the photo using the URL
  4. Save the file

Here's a very simple example of how your server-side script for retrieving photos may look:

<?php

if (!isset($_REQUEST['img_url'])) {
  exit;
}

// Retrieve the image
$response = file_get_contents($_REQUEST['img_url']);

if ($response === false) {
  exit(json_encode(array('success' => false)));
}

// Save it somewhere
$save = file_put_contents('/savePath', $response);

if ($save === false) {
  exit(json_encode(array('success' => false)));
}

echo json_encode(array('success' => true));

This code hasn't been tested and probably doesn't work, and you probably need to add some checks to make sure malicious URLs aren't being sent and whatnot, but that's generally how I think I'd do it.

Hope this helps, let me know how it goes!

ETA: I'm gonna go ahead and close this issue since it isn't directly about the plugin, but I'm still interested in hearing about what you figure out.