php copy

becejac

New Member
Messages
4
Reaction score
0
Points
0
Hello,

does anyone know how to create a script which would copy a picture from some server and store it on my server, with only using a link to that image ? Which permissions do I need ?

I have created a script to manage music videos, and there is a small thumb 100x100 for each. User adds only a link to that picture. The problem is that those pictures can be very big, and it can take a lot of time to load them all. So, It would be the best if that picture could be copied to the local server, renamed and resized.

The simple solution would be to copy that image to the hard drive, and then to upload it to the server, but if it is possible, I would like to avoid this solution.
 

Gouri

Community Paragon
Community Support
Messages
4,565
Reaction score
245
Points
63
Yes this possible using php file upload feature. The file is in remote server and you can give the link and code will gets the file ans stores to your server. Remaining resize and rename functionalities you have add to script.

Check this link http://bgallz.org/62/php-upload-file-from-url/

This is basic idea...
 

galaxyAbstractor

Community Advocate
Community Support
Messages
5,508
Reaction score
35
Points
48
Hello again,

I am getting errors:

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'eastside'@'localhost' (using password: NO) in

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in

I have no idea how could I solve this, so I hope you can help me.

is this on x10 or on your computer? If it's on x10, you should use this info:

host: localhost
username: *cpanel username*_*database username*
password: database user password
database: *cpanel username*_database name*
(change the ** stuff)
 

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
The script that is linked to has too many problems.
Why use mysql_real_escape_string() if you aren't going to connect to mySQL?
filesize( $url ) fails with x10's settings. You either have to load until you get to your size limit and then fail, or send a HEAD request to find out the size of the file.

OK, with some caveats:

1. If you use this to download copyrighted material, or with the intent to set up a phishing page, you are on your own.
2. If the site with the image has hotlink protection, no go. Talk to the guy who supplied the link.
3. If the link to the image is 'nonstandard', no go.
4. Resizing the image is up to you.
5. Sanatizing the user supplied URL is up to you.

I use cURL.
First, I check to see if the file extension is one I can use.
Second, I use cURL with a HEAD request to find the size of the file. Set your own limit in the script.
Then, I finally get the file and store it locally. Since I tested it on known files, I used the remote name. You might want to use 'username.jpg' or similar.
Up to you to figure out how to resize and convert to common format.
Save thumbnail and unlink downloaded original.

PHP:
$url = 'http://somesite.com/images/cuddlykoala.png' ;

// FIRST, FIND IF THE FILE TYPE (BY LOOKING AT THE EXTENSION) IS SUPPORTED

$filename = basename(  parse_url($url, PHP_URL_PATH) ) ;  // get just the filename

$valid_exts = '/\.(jpg|jpeg|gif|png)$/i' ;  // add image extension as you want...

if( ! preg_match( $valid_exts , $filename , $match ) ) {
  
   echo "$filename does not have a supported extension.";
   exit ;
}

$extension = $match[ 1 ] ;  // the matched extension, without dot


// SECOND FIND THE SIZE OF THE FILE...

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, true);  // we want the header...
curl_setopt($ch, CURLOPT_NOBODY, true);  // NO BODY, ie do not get body of url, ie only the header
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  // output results as string

$data = curl_exec($ch);
curl_close($ch);
if ($data === false) {  ## this is false if cURL failed.
  echo 'cURL failed to get HEAD request.';
  exit;
}

$contentLength = 'unknown';
$status = 'unknown';

if (preg_match('/^HTTP\/1\.[01] (\d\d\d)/', $data, $matches)) {
  $status = (int)$matches[1];
}
if (preg_match('/Content-Length: (\d+)/', $data, $matches)) {
  $contentLength = (int)$matches[1];
}

if( $status != 200 ){
   echo "Sorry, HEAD request returned a status of $status" ;
   exit;
}

if( $contentLength == 'unknown' ){
   echo "Sorry, HEAD request did not return content length";
   exit ;
}

if( $contentLength > 500000 ){
   echo "Sorry, a filesize of $contentLength is too large" ;
   exit ;
}


//  FINALLY, GET THE FILE ITSELF ...

$ch = curl_init($url); 

$fp = fopen( $filename, "w");   // open a local file to write to, using their filename
                                // probably should use your own, like 'username.' . $extension 

curl_setopt($ch, CURLOPT_FILE, $fp);  // WHERE TO STORE IT
curl_setopt($ch, CURLOPT_HEADER, 0);  // DO NOT WANT THE HEADER

$data = curl_exec($ch); 

curl_close($ch); 

fclose($fp);

if ($data === false) {
  echo 'cURL failed to get image.';
  exit;
}

// use GD etc to resize file and maybe change format so all thumbnails are consistent
// store thumbnail and unlink $filename 

echo "<a href='/$filename' >Go To Local Copy</a>" ;
 
Last edited:
Top