2

Ok I have php code that handles the upload of images. now I have remote post that handles this code:

$postheader = http_build_query($postarray);
$url = parse_url($uri);
$host = $url['host'];
$path = $url['path'];

$results = '';
if ($fp = fsockopen($host, 80, $errno, $errstr, 2))
{
    stream_set_timeout($fp, 30);

    $headers = "POST ". $path ." HTTP/1.0\r\n";     
    $headers .= "User-Agent: \r\n";
    $headers .= "Host: ". $host ."\r\n";
    $headers .= "Accept: */*\r\n";
    $headers .= "Content-Type: application/x-www-form-urlencoded\r\n";
    $headers .= "Content-Length: " . strlen($postheader) . "\r\n";
    $headers .= "Connection: close\r\n\r\n";
    @fputs($fp, $headers . $postheader);
    while($currentHeader = fgets($fp, 4096))
    {
        if (preg_match("/^\r?\n$/", $currentHeader)) { break; }
    }

    while(!@feof($fp))
    {
        $results .= @fgets($fp, 1024);
    }
    @fclose($fp);

The thing is I get the url of the image from that post and it is stored like this:

$file = 'http://someurl.ll/image.jpg'

And script that handles image with: $_FILES['image']

Now if I can say $_FILES['image'] = $file it should go ferder, but it doesn't! I thing that $_FILES['image'] needs not only url of data but also raw uploaded data

So I want to know what is equal to $_FILES['image'], that can can be make of $file = 'http://someurl.ll/image.jpg'?

UPDATE: Hier is what I got as $_FILES['image'] as var_dump

array(5) { ["name"]=> string(17) "logo.png" ["type"]=> string(9) "image/png" ["tmp_name"]=> string(14) "/tmp/phpWUqC5c" ["error"]=> int(0) ["size"]=> int(5674) }

an I want to make the same but from $file = 'http://someurl.ll/image.jpg' so the same array and then to say:

$_FILES['image'] = that array from http://someurl.ll/image.jpg

Because it is remote I would like to handle it as $_FILES['image'], but I cant so the quick solution is to make that remote image same as $_FILES['image']

closed as not a real question by deceze, Dale, Baba, Ed Heal, Mario Sannum Dec 17 '12 at 17:27

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. If this question can be reworded to fit the rules in the help center, please edit the question.

  • Maybe if you can make it clear what you want to do, you can get quick responses. – web-nomad Dec 17 '12 at 12:19
  • Have you tried fopen, as in: $_FILES['image'] = fopen($file,'r'); ? But indeed, its is not very clear what you want. – Sjors Branderhorst Dec 17 '12 at 12:19
  • just want to add url of that image like pathtoimage.jpg to add to $_FILES['image'] – user1405338 Dec 17 '12 at 12:23
  • just updated and cleared the question – user1405338 Dec 17 '12 at 12:33
15

Add this function to your source

/**
 * Add to $_FILES from external url
 * sample usage: addToFiles('google_favicon', 'http://google.com/favicon.ico');
 * @since 17.12.12 17:23
 * @author mekegi
 * @param string $key
 * @param string $url sample http://some.tld/path/to/file.ext
 */
function addToFiles($key, $url)
{
    $tempName = tempnam('/tmp', 'php_files');
    $originalName = basename(parse_url($url, PHP_URL_PATH));

    $imgRawData = file_get_contents($url);
    file_put_contents($tempName, $imgRawData);
    $_FILES[$key] = array(
        'name' => $originalName,
        'type' => mime_content_type($tempName),
        'tmp_name' => $tempName,
        'error' => 0,
        'size' => strlen($imgRawData),
    );
}

And use:

addToFiles('image', 'http://someurl.ll/image.jpg');
  • it saves it in /tmp/ but php kohana say Upload::not_empty it is empty – user1405338 Dec 17 '12 at 14:35
  • I was watching to find php script where does it upload? if I var_dump it it say /tmp/ but when I go there there is not... (this is for normal upload with form) – user1405338 Dec 17 '12 at 14:37
  • path of uploading file is $tempName. – mekegi Dec 17 '12 at 14:43
  • You also have to check if image size is greater than upload-max-filesize. If so, the upload will not work. – web-nomad Dec 17 '12 at 14:51
  • 3
    Thanks this helped me with twitter image upload - works with update_with_media – Dante Cullari Mar 6 '13 at 11:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.