Copy image from url using timthumb

958 views Asked by At

I'm currently working on an uploading, resizing & cropping script. I'm having some trouble copying an image from a URL and saving it onto my server.

This is my code where the error occurs. I replaced my domain with [MYDOMAIN] - so overlook that :)

if(in_array($extension, $extensions_allowed) )
{
    $name = explode(".".$extension, $_FILES['image']['name']);
    $name = $name[0]."_".time();

    move_uploaded_file($_FILES['image']['tmp_name'], "temp/".$name.".".$extension);
    // File uploaded to temporary folder, now lets replace the newly created temp image with a resized image
    $source = 'timthumb.php?src=http://[MYDOMAIN]/includes/crop/temp/'.$name.'.'.$extension.'&w=398';   // Set source to be the resized image
    $dest = $_SERVER['DOCUMENT_ROOT'].'/includes/crop/temp/r'.$name.'.'.$extension;                             // Destination = temp folder, rdy to be cropped.
    if(copy($source, $dest)) {
        // If the image was transfered/copied successfully
        unlink("temp/".$name.".".$extension);   // Remove old temp img. "not the resized"
        // The old one has been removed, so now the new file can take its place, lets rename it.
        $current = 'temp/r'.$name.'.'.$extension;
        $new = 'temp/'.$name.'.'.$extension;
        rename($current, $new); // Old temp name becomes the new.
    }
    else {
        echo "Couldnt copy file, try again.";
        die();
    }


    $_SESSION['image']['extension'] = $extension;
    $_SESSION['image']['name'] = $name;
    //REDIRECT ON SUCCESS
    header("Location: /includes/crop/edit.php");
}

I don't feel that it's necessary to write the full code since I know it's working, and it only goes wrong here.

So my move_uploaded_files() works fine but the error occurs in the if statement just below.

if(copy($source, $dest)) {
    // If the image was transfered/copied successfully
    unlink("temp/".$name.".".$extension); // Remove old temp img. "not the resized"
    // The old one has been removed, so now the new file can take its place, lets rename it.
    $current = 'temp/r'.$name.'.'.$extension;
    $new = 'temp/'.$name.'.'.$extension;
    rename($current, $new);   // Old temp name becomes the new.
}
else {
    echo "Couldnt copy file, try again.";
    die();
}

My error message:

Warning: copy(timthumb.php?src=http://[MYDOMAIN]/includes/crop/temp/SummerVibe Cover_1389227432.jpg&w=398): failed to open stream: No such file or directory in /home/[MYHOST]/public_html/pt/includes/crop/index.php on line 108

Couldnt copy file, try again.

If you're wondering where line 108 is, it's the line where the if(copy(ect)) starts.

Hope someone out there can help. :)

kindest regards, SmK1337

1

There are 1 answers

0
Xyz On

Your error is (or was, as it is a fairly old question), that you are trying to copy a relative path to a php script. (timthumb.php?src=...)

First of all. Your error means that this path does not exist, and this can be for two main reasons:

  1. timthumb.php is a relative path, which means php will try to resolve it relative to your current working directory, which can vary based on how you execute your script.
  2. You will try to copy the file actual content of the file, not execute it. This is certainly not what you want.
  3. ?src= on a file path is not an actual query string, but just a part of the filename. Certainly, this file does not exist.

What you should do instead is either send a request to the timthumb.php script via http.

copy can copy a file over http, in which case (if it is a php script on a correctly configured web server) it will be ran.

copy('http://[MYDOMAIN]/path/to/timthumb/script/' . $source, $dest);