Jump to content

CURL Uploading Files


mme

Recommended Posts

Hi,

 

I am trying upload files to a remote server using CURL however It is not sending all the values in the array.

 

$auth_local='testing123';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $auth_remote_url[$auth_no] );
curl_setopt($ch, CURLOPT_POST, TRUE);
$post_array = array(
	"auth"=>$auth_local,
	"auth_e"=>$auth_e,
                "my_file"=>"@".$myfile,
	"md5"=>$md5,
	"check"=>"0",
                "upload"=>"Upload");
curl_setopt($ch, CURLOPT_USERAGENT, "Test Upload");
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_array);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);  
$response = curl_exec($ch );
curl_close($ch );

 

When I check the value of $_POST['auth'] it is blank.

 

When I remove

"my_file"=>"@".$myfile,

It does read the correct value

 

Why is this not returning a value?

 

Thanks,

 

mme

Link to comment
Share on other sites

What's the value of $myfile?

 

$myfile=$_FILES['uploaded_file']['tmp_name'];

 

What is the remote server going to do with that?  Just trying to figure out why that's a parameter, since it's a local file that the remote server can't do anything with.

 

My guess is that there's something that's messing up the post data between the '@' symbol an the file name, like an invalid character; unfortunately I'm really not too familiar with CURL.

Link to comment
Share on other sites

What's the value of $myfile?

 

$myfile=$_FILES['uploaded_file']['tmp_name'];

 

What is the remote server going to do with that?  Just trying to figure out why that's a parameter, since it's a local file that the remote server can't do anything with.

 

My guess is that there's something that's messing up the post data between the '@' symbol an the file name, like an invalid character; unfortunately I'm really not too familiar with CURL.

 

Thanks for your quick reply,

 

Basically what it is doing is sending the uploaded file to another server which processes the $_POST['my_file'] variable as a file upload and stores the file on the server.

 

if (!empty($_FILES['my_file'])) 
{
move_uploaded_file($_FILES['my_file']['tmp_name'],$filename);
}

 

Hope that helps,

 

Thanks,

 

-mme

Link to comment
Share on other sites

Hm, ok, I just looked up the curl_setopt() command and it appears that you're doing it correctly.  Two thoughts:

 

1) Are you sure there's an uploaded file?  Maybe do a var dump of $post_array to make sure that $myfile is defined.

 

2) Maybe the file size is too big and is being rejected by the destination server?

Link to comment
Share on other sites

jdavidbakr is right. I think you're confusing the file name with file data.

 

Sending $_FILES['uploaded_file']['tmp_name'] via POST variable my_file will just give the temporary location of that uploaded file on the local server. The data of that file will still be on the local server so you will have no data to save on the remote server.

 

I think you need to use CURLOPT_INFILE and pass the file data into that. Check http://php.net/manual/en/function.curl-setopt.php. I've never tried to do this before so not positive on the steps you need to take to do it. 

Link to comment
Share on other sites

Thanks for your quick reply's,

 

jdavidbakr is right. I think you're confusing the file name with file data.

 

Sending $_FILES['uploaded_file']['tmp_name'] via POST variable my_file will just give the temporary location of that uploaded file on the local server. The data of that file will still be on the local server so you will have no data to save on the remote server.

 

I think you need to use CURLOPT_INFILE and pass the file data into that. Check http://php.net/manual/en/function.curl-setopt.php. I've never tried to do this before so not positive on the steps you need to take to do it. 

 

Hmm I just tried it with a smaller file which seemed to work without any problems. However It seems to get errors with larger files (2MB). Is there a limit as to how big an array value can be to post?

 

Hm, ok, I just looked up the curl_setopt() command and it appears that you're doing it correctly.  Two thoughts:

 

1) Are you sure there's an uploaded file?  Maybe do a var dump of $post_array to make sure that $myfile is defined.

 

2) Maybe the file size is too big and is being rejected by the destination server?

 

1. It seems to work with smaller files

2. According to my host it has a max filesize of 10MB

Link to comment
Share on other sites

do phpinfo() on the remote server and look at the upload_max_filesize and post_max_size vars.

 

you may be able to up these via ini_set('upload_max_filesize','10M'); etc or else use htaccess to do it.

 

Thanks, I have figured out what the problem is.

 

post_max_size : 2M

 

Is there any way to increase this?

 

EDIT: already tried this but it does not seem to have any effect:

 

Top of the page.

ini_set('post_max_size','10M');

 

And also in my .htaccess

php_value post_max_size 20M

 

Thanks,

 

-mme

Link to comment
Share on other sites

if you do ini_set('post_max_size','10M'); or php_value post_max_size 20M in htaccess then run phpinfo() does it show the changes?

 

ie.

ini_set('post_max_size','10M');
phpinfo();

 

If you can't see the changes in phpinfo you may be at the mercy of your provider. I have had issues with ini_set not working but htaccess has always worked for me.

Link to comment
Share on other sites

if you do ini_set('post_max_size','10M'); or php_value post_max_size 20M in htaccess then run phpinfo() does it show the changes?

 

ie.

ini_set('post_max_size','10M');
phpinfo();

 

If you can't see the changes in phpinfo you may be at the mercy of your provider. I have had issues with ini_set not working but htaccess has always worked for me.

 

No, I tried with both however no changes are showing so it looks like I am going to look for a different host.

 

Thanks for all your replies and help.  8)

Link to comment
Share on other sites

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.