+ Reply to Thread
Results 1 to 5 of 5

Thread: php copy

  1. #1
    becejac is offline x10Hosting Member becejac is an unknown quantity at this point
    Join Date
    Aug 2009
    Posts
    4

    php copy

    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.

  2. #2
    Gouri's Avatar
    Gouri is offline Community Paragon Gouri has a brilliant futureGouri has a brilliant futureGouri has a brilliant future
    Join Date
    Oct 2007
    Location
    India
    Posts
    4,502

    Re: php copy

    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...
    If you feel my post is useful then click to give Reputation (bottom left corner of this post)

    X10 Hosting | News and Announcements | Premium Hosting | VPS Hosting | Prime Membership

    Tech Community | Gouri

  3. #3
    becejac is offline x10Hosting Member becejac is an unknown quantity at this point
    Join Date
    Aug 2009
    Posts
    4

    Re: php copy

    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.

  4. #4
    galaxyAbstractor's Avatar
    galaxyAbstractor is offline Community Advocate galaxyAbstractor is on a distinguished road
    Join Date
    Oct 2007
    Location
    Land of Null and Insanity
    Posts
    5,495

    Re: php copy

    Quote Originally Posted by becejac View Post
    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)

  5. #5
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: php copy

    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 Code:

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

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

    $filename basename(  parse_url($urlPHP_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] ;  // the matched extension, without dot


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

    $ch curl_init($url);
    curl_setopt($chCURLOPT_HEADERtrue);  // we want the header...
    curl_setopt($chCURLOPT_NOBODYtrue);  // NO BODY, ie do not get body of url, ie only the header
    curl_setopt($chCURLOPT_RETURNTRANSFERtrue);  // 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($chCURLOPT_FILE$fp);  // WHERE TO STORE IT
    curl_setopt($chCURLOPT_HEADER0);  // 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 by descalzo; 11-05-2009 at 12:52 PM.
    Nothing is always absolutely so.

+ Reply to Thread

Similar Threads

  1. Ever Been Suspended For Using PHP?
    By dragoneye_xp in forum Off Topic
    Replies: 26
    Last Post: 08-16-2009, 07:17 PM
  2. [PHP] Variables in PHP
    By Bryon in forum Tutorials
    Replies: 15
    Last Post: 01-29-2009, 09:46 AM
  3. PHP Easter Eggs
    By dragoneye_xp in forum Off Topic
    Replies: 3
    Last Post: 06-14-2006, 05:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers