Jump to content

Recommended Posts

I am trying to upload images to Imageshack directly form my site with Imageshack API, using the code bellow:

 

index.htm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml">

 

<head>

  <title></title>

</head>

<body>

    <form action="curl-form.php" method="post" enctype='multipart/form-data'>

        <input type="file" id="foto" name="foto" />

        <input type="submit" value="Enviar" />

    </form>

</body>

</html>

 

img.php

<?php

$data = array();

$data['fileupload'] = '@' . $_FILES["foto"]['tmp_name'];

$data['key']        = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';

$data['email']      = 'email@email.com';

$data['rembar']    = 'yes';

$data['optsize']    = '96x96';

$data['xml']        = 'yes';

 

$post_str = '';

foreach($data as $key=>$val) {

$post_str .= $key.'='.urlencode($val).'&';

}

$post_str = substr($post_str, 0, -1);

 

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'http://www.imageshack.us/upload_api.php');

curl_setopt($ch, CURLOPT_POST, TRUE);

curl_setopt($ch, CURLOPT_HEADER, FALSE);

curl_setopt($ch, CURLOPT_TIMEOUT, 240);

curl_setopt($ch, CURLOPT_POSTFIELDS, $post_str);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

$response = curl_exec($ch );

curl_close($ch );

 

if (strpos ( $response, '<' . '?xml version="1.0" encoding="iso-8859-1"?>' ) === false)

{

    return NULL;

}

else

{

    $xml = new SimpleXMLElement ( $response );

    return array (

    "image" => $xml->image_link,

    "thumb" => $xml->thumb_link );

}

?>

 

When I upload an image, the following error occurs:

 

"Sorry, but we've detected that unexpected data is received. Required parameter 'fileupload' is missing or your post is not multipart/form-data"

 

What is wrong with the code?

How can I send enctype='multipart/form-data' with cURL?

Link to comment
https://forums.phpfreaks.com/topic/176860-upload-images-to-imageshack-using-curl/
Share on other sites

I tried using different code:

<?php

$postData = array();

$postData['fileupload'] = "@" . $_FILES['foto']['tmp_name'];

$postData['submit']    = "Submit";

$postData['key']        = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

$postData['email']      = "email@email.com";

$postData['rembar']    = "yes";

$postData['optsize']    = "96x96";

$postData['xml']        = "yes";

 

$ch = curl_init();

 

curl_setopt($ch, CURLOPT_URL, "http://www.imageshack.us/upload_api.php");

curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);

curl_setopt($ch, CURLOPT_POST, 1 );

curl_setopt($ch, CURLOPT_POSTFIELDS, $postData );

 

$response = curl_exec( $ch );

curl_close($ch );

 

echo $response . "<br />";

?>

 

and the different error message was sent:

Wrong file type detected for file php6JrCec:application/octet-stream

cURL  sent file using their temp name and the type verification of the Imageshack was refused.

How can I send files to Imageshack using cURL?

please use code tags [ code]

 

Wrong file type detected for file php6JrCec:application/octet-stream

 

then set the Content-type to the type of the file!

 

curl_setopt ( $ch , CURL_HTTPHEADER , "Content-type: ".$_FILES['foto']['type'] )

I found a solution. I don´t know if this is the better solution, but works fine.

 

index.htm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"    
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>  
<title></title>
</head>
<body>    
<form action="imagem.php" method="post" enctype='multipart/form-data'>        
<input type="file" id="foto" name="foto" />        
<input type="submit" value="Enviar" />    
</form>
</body>
</html>

 

imagem.php

<?php
preg_match("/\.(gif|bmp|png|jpg|jpeg){1}$/i", $_FILES['foto']['name'], $ext);
$imagem_nome = md5(uniqid(time())) . "." . $ext[1];
rename($_FILES['foto']['tmp_name'], "/tmp/" . $imagem_nome);

$postData = array();
$postData['fileupload'] = "@/tmp/" . $imagem_nome;
$postData['submit']     = "Submit";
$postData['key']         = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$postData['rembar']     = "yes";
$postData['xml']        = "yes";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.imageshack.us/index.php");
curl_setopt($ch, CURLOPT_POST, true );
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_TIMEOUT, 240);curl_setopt($ch, CURLOPT_POSTFIELDS, $postData );

$response = curl_exec( $ch );
curl_close($ch );

echo $response;
?>

 

I did not use the $_FILES['foto']['type'] function to get the file extension, because if the file type is jpg, this function returns pjpg, generating an error of invalid format in the Imageshack.

The Imageshack then returns a xml that be parsed, with links to the image.

 

I have another question. When I renamed the file in the temp directory, this file will be automatic deleted or I need to delete the file manually?

  • 1 year later...

you also need to add the type of file it is in the fileupload field

 

$postData['fileupload'] = "@/tmp/".$imagem_nome.";type=".$_FILES['data']['type'];

 

as well as use the right api script

 

curl_setopt($ch, CURLOPT_URL, "http://www.imageshack.us/upload_api.php");

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.