简体   繁体   中英

PHP / CURL / ASP.NET / C# - Send files bigger than 9mb

I want to send a file from PHP (v5.4) via CURL to an ASP.NET WebApi (C#). I thought this should be easy but I'm facing some strange behaviour I can't explain:

php script:

<?php   
    $files = array();
    $files['file_content'] = '@'.realpath('./myfile.jpg');

    $url = 'https://localhost:44307/api/v1/File';

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, trim($url));

    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $files);
    curl_setopt($ch, CURLOPT_SSLVERSION, 3); 
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_NOPROGRESS, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    // ignore ssl verifyer
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

    curl_exec($ch);

    echo 'ErrorMsg: '.curl_error($ch).'<br>';
    echo "ErrorNr.:".curl_errno($ch).'<br>';    

    curl_close($ch);
?>

php.ini:

upload_max_filesize = 1024M
max_file_uploads = 20
memory_limit = 1024M
post_max_size = 1024M

Web.config:

<system.web>
    <httpRuntime executionTimeout="1800" maxRequestLength="2097152" requestValidationMode="2.0" />
</system.web>
<system.webServer>
    <security>
        <requestFiltering>
            <requestLimits maxAllowedContentLength="2147483648" /> 
        </requestFiltering>
    </security>        
</system.webServer>

When I execute the script with an file bigger than 9mb (also with eg two 5mb-files) I get the following result after a few minutes depending on the size of the file:

ErrorMsg: SSL read: error:00000000:lib(0):func(0):reason(0), errno 10054
ErrorNr.:56

When I change to an file of 9mb or less everything works fine.

Also: I get the same error when calling CURL from cmd (Windows 7):

curl --form upload=@myfile.jpg --form press=OK https://localhost:44303/api/v1/File -k -sslv3 

=> Everything happens on my local machine, so no firewall and no vpn.

I have tried multiple combinations of parameters but I don't make any progress. Actually it is more guessing than real testing. Maybe there is just one little thing I overlooked all the time?

Thanks a lot in advance!

After guessing for almost one day I had the idea of just publishing the asp.net part to an Azure Website as it might be a problem of my local configuration.

As I published it, I had to remove this line of my php curl configuration:

//curl_setopt($ch, CURLOPT_SSLVERSION, 3); 

With the Azure Website I can now upload bigger files - just as it should be.

I tried to understand why it is working on Azure but not on localhost - but actually I did not found the answer yet. Looks like some configuration problem of the IIS express and Visual Studio 2013. Maybe this helps someone.

This restriction of uploading only 9MB can be caused by using the secure https URL instead of the non secure http URL.

So in the php script you should change the line 5 into:

    $url = 'http://localhost:44307/api/v1/File';

The appropriate curl command would be:

    curl --form upload=@myfile.jpg --form press=OK http://localhost:44303/api/v1/File -k -sslv3

When using the secure URL, the upload stops actually in less than ten seconds, as can be seen in the curl progress meter. The connection is closed a couple of minutes after this, giving the false appearance that the actual problem would be in the server (The error code 56 indicates that the connection was reset by peer).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM