1

Ok i was wondering if it is possible to use curl and upload a image from my sites directory rather then off my machine, what i need to do exactly is upload a image from my image directory using the file-select in a form, If anyone knows what i need to edit in my code i would appreciate it, i have looked across the web but found nothing in my search, I was thinking that i would need to file get content base64 encode then post am i right in thinking this?

$upload_url = 'http://example.com/settings/upload-img';
$file_path = 'directory/image.png';
$fields = array(
   "MAX_FILE_SIZE" => "",
   "fileselect[]" => $file_path
);

foreach ($fields as $key => $val) {
   $post_data .= $key . '=' . $val . '&';
}

$chr = curl_init();
curl_setopt($chr, CURLOPT_URL, $upload_url);
curl_setopt($chr,CURLOPT_POST, count($fields));
curl_setopt($chr,CURLOPT_POSTFIELDS, rtrim($post_data, '&'));
curl_setopt($chr, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($chr);
curl_close($chr);

What I had in mind for the image to base64_encode() is below, I am not sure if I am correct thinking this may work.

$raw_img = base64_encode(file_get_contents('directory/image.png'));

Then use "fileselect[]" => $raw_img

TURTLE
  • 3,728
  • 4
  • 49
  • 50
  • 1
    Perform the upload using a browser once and dump the network traffic. Then do the same using your approach. You can see the difference and this correct your code. – arkascha Mar 02 '13 at 17:38
  • 1
    @Vineet1982: The OP tries a http upload, not ftp. the ftp protocol is from the 80th. It should not be used any more if other possibilities exist. At least it should be used in its encrypted variants ftps or sftp. – arkascha Mar 02 '13 at 17:38
  • If upload-pic.php is your page, I would think it would be easier to create another php endpoint that just handled this type of upload. In this case you just drop the base 64 into a variable and pull it out on the other side. You might want different rules for the automated process. – Kevin Seifert Mar 02 '13 at 17:42
  • I think it can be done through curl i have the idea but have to try it and let u know – Vineet1982 Mar 02 '13 at 17:43
  • Like I say in the title it is on an external site, Which I do not own . – TURTLE Mar 02 '13 at 17:45

1 Answers1

0
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
curl_setopt($ch, CURLOPT_URL, _VIRUS_SCAN_URL);
curl_setopt($ch, CURLOPT_POST, true);
// same as <input type="file" name="file_box">
$post = array(
    "file_box"=>"@/path/to/myfile.jpg",
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 
$response = curl_exec($ch);
?>

It seems that many want to know what is _VIRUS_SCAN_URL?

_VIRUS_SCAN_URL

Its the new host virus scanning for developers. Internally all the files sent to Virus Scanning Module to check any virus to cause damage to Server or not.

Service is developed by http://virusade.com but it is still in beta mode and _VIRUS_SCAN_URL and @ there api PROTOCAL.

Simply they fetch the document from the server scans it and if non-virus then sends it to the uploading server.

Vineet1982
  • 7,730
  • 4
  • 32
  • 67
  • What is that ` _VIRUS_SCAN_URL` meant to express?!? and what does the `@` do inside the file path? – arkascha Mar 02 '13 at 17:59
  • Just to do inbuilt function of curl and @ pointer to the file or say temp link – Vineet1982 Mar 02 '13 at 18:04
  • Sorry, impossible to understand that, could you try again in other words? And what about that ` _VIRUS_SCAN_URL` ? Any documentation about that `@` if it is something to do with how curl behaves? – arkascha Mar 02 '13 at 18:06
  • Virus scanning function to save server from virus attacks – Vineet1982 Mar 02 '13 at 18:08
  • _WHY_? Documentation? Explanation? Or shall we believe it is your VOODOO? And you dont really wanna claim that setting that header protects anyone magically from a virus, do you? – arkascha Mar 02 '13 at 18:09
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/25429/discussion-between-arkascha-and-vineet1982) – arkascha Mar 02 '13 at 18:12
  • 1
    Ok, the make things clear: the `@` in front of the file path brings the curl extension not to use the specified string (the path) as value, but actually the files content. About that strange 'virus protection' - no idea what that is meant to be... – arkascha Mar 02 '13 at 18:25
  • 1
    This is a _very_ good example why one should NOT simply copy something someone else posted. Ask what it does first. Understand what it does. In this case (aparently, since it is just a claim) that option hands out the file to some external "service provider". This quite certainly is NOT what most people want, since no one knows what that "service provider" does with the files. – arkascha Mar 02 '13 at 18:41
  • @arkascha You are right but virus checking of uploaded files are also important as the spammers, hackers are there its only safegaurd and if someone doesn't doing those things then nobody has the problem to get the files scanned as nothing would be found – Vineet1982 Mar 02 '13 at 18:45
  • The reason why viruses are a problem is not uploaded files, but unsafe systems. People should stop using that unsafe MS-Windows and use better systems. Those are available and really easy and comfortable to use. I don't see why anyone operating an internet server should burden the task to protect people who refuse to learn what is obvious. It is their problem. Sorry. "Virus scanning" is the attempt to cure symptoms, not the cause. That makes little sense, it only costs money, bandwidth and delivers a false sense of security. - and - btw - what do viruses have to do with hackers and spammers? – arkascha Mar 02 '13 at 18:54