Server freezes when uploading large sized images in php - php

I have simple html form with post method, which sends an image to my action.php file:
<form action="action.php" method="post" enctype="multipart/form-data">
<input type="text" name="name"/>
<input type="file" name="avatar"/>
<input type="submit" name="submit" value="Create"/>
</form>
And my action.php file looks like this:
if(isset($_POST['name'])) {
$error = $_FILES['avatar']['error'];
echo $error;
}
It works fine with small sized images, but when I try to submit images with approximately 2mb size the server just stops responding. It even doesn't send post request to my action.php.
Note: I've already set my upload_max_filesize = 64M, but the same thing is happening. It doesn't show any errors

Change the value of post_max_size = 70M.
Must be greater than or equal to upload_max_filesize.
After modifying php.ini file(s), you need to restart your HTTP server to use new configuration.
If problem persist you could try these solutions (1,2,3).

Related

Php 7 form doesn't work with enctype="multipart/form-data" and method="post"

I am using PHP 7.0.9 with IIS 8. I have a html form to upload an image. The method is POST, the action is correct, and enctype is "multipart/form-data". However, when I submit the form, the page hangs, as if it was trying to upload the selected file endlessly - it weight about 30kb, not a big deal...
Here is my form :
<!DOCTYPE html>
<html>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="text" name="textInput" id="textInput">
<input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>
And here is upload.php :
<?php
echo "ok<br/>";
var_dump($_POST);
?>
As is, when I click on "Upload Image", the page hangs as if it was uploading the file endlessly.
But if I remove the enctype or if I change the action by get, the form is submited and prints :
ok
array(0) { }
The $_POST array is obviously empty, but why doesn't it works with enctype="multipart/form-data" ?
FYI:
file_uploads = On
upload_max_filesize = 20M
max_file_uploads = 20
upload_tmp_dir = C:\Windows\temp
...and C:\Windows\temp is writeable for Everyone. Yes, the group Everyone - I am really annoyed by this, and I don't want any rights problem.
Some tests later...
If I use :
<form action="upload.php" method="get" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="text" name="textInput" id="textInput">
<input type="submit" value="Upload Image" name="submit">
</form>
The result is :
ok
array(3) { ["fileToUpload"]=> string(13) "imageName.png" ["textInput"]=> string(4) "blah" ["submit"]=> string(12) "Upload Image" }
It seems to be a problem between the method and the enctype.
EDIT
For what it worth, I think it's definitly a IIS problem: when I look in the IIS log I see the request for the form :
2017-01-16 13:28:19 X.X.X.X GET /testupload.html - 80 - X.X.X.X Mozilla/5.0+(Windows+NT+6.2;+WOW64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/55.0.2883.87+Safari/537.36 - 304 0 0 1140
But nothing is written when I submit the form; so I don't even know if IIS is receiving the request.
Finally, the culprit was Glimpse. I absolutely don't know why, but once I had removed every traces of the plugin in the web.config, upload via PHP worked again.
"I cannot replicate it with the same php version, but with Apache instead of IIS"
Take a look on your IIS firewall.
use $_FILES (for files) instead of $_POST

file uploading in php not working for more than 20kb (i looked up all possible questions here)

<form action="uploads.php" enctype="multipart/form-data" method="post">
<?php
if(!empty($message)){
echo "<p>{$message}</p>";
}
?>
<input type="hidden" name="MAX_FILE_SIZE" value="1048576" />
<label for="something">Please, upload your file</label>
<input type="file" name = "file_upload" id="something"/>
<input type="submit" name="submit" value="Upload" />
</form>
</body>
</html>
I changed post_max_size = 100M, max_file_size was already 64M. Problem is when I run the code and upload more than 20kb then first time it gives error no. 3 (partial upload problem) and then stop working. I have to restart php to do other thing. Please help..
In your php.ini file, change this variable upload_max_filesize to whatever max_uploaded_size you wish to have.
But remember, to keep your post_max_size greater than or equal to upload_max_filesize.
For more info, see this answer.
PHP change the maximum upload file size

Using reCAPTCHA and enctype="multipart/form-data" together

I have a file upload form on which I also want to use the Google reCAPTCHA.
If I have the following
<form method="post">
<input type="file" name="filename">
<div class="g-recaptcha" data-sitekey="***"></div>
<input id="submit" name="submit" type="submit" value="Submit">
</form>
the I can use the reCAPTCHA, but cannot upload the file.
However, if I use:
<form method="post" enctype="multipart/form-data">
<input type="file" name="filename">
<div class="g-recaptcha" data-sitekey="***"></div>
<input id="submit" name="submit" type="submit" value="Submit">
</form>
I can upload the file successfully, and use the reCAPTCHA provided that the file is a text file. I cannot get it to work if I try to upload a pdf file.
When uploading a text file, the $_POST contains the 'g-recaptcha-response', but when uploading a pdf, the $_POST does not contain the 'g-recaptcha-response'.
Can someone explain what is going wrong here?
EDIT
It seems like it's actually a filesize problem.
Files larger than ~200kb cannot be sent whatever their format.
I have upload_max_filesize = 2M in my php.ini file, so I'm not sure why 200kb is too large...
Any thoughts?
EDIT 2: More information
It looks like the $_FILE contains the error code 3: UPLOAD_ERR_PARTIAL.
I don't see why the file cannot be uploaded completely.
EDIT 3: Getting somewhere
I can now upload files. It seems like I need to put the reCAPTCHA before the file input.
<form method="post" enctype="multipart/form-data">
<div class="g-recaptcha" data-sitekey="***"></div>
<input type="file" name="filename">
<input id="submit" name="submit" type="submit" value="Submit">
</form>
Can anyone elaborate on why this might be the case?
Edit 4: Spoke too soon
Switching the order makes the g-recaptcha-response' appear in $_POST, but I am still getting the error code 3: UPLOAD_ERR_PARTIAL.
Edit 5
It looks like the file is being uploaded correctly, since I can see it in the header parameters (firefox debugger). It seems like php is just not filling in the $_FILE array properly...
I tried the same code on a different server and it worked fine.
Looks like the problem was that the server I set up has some sort of configuration issue. It actually has nothing to do with the interaction of reCAPTCHA and the file input + enctype.
I will update if I figure out what the configuration problem is.

Empty FILES array for large (ish) file uploads

I've got some legacy code that for the life of me I cant see why it's suddenly stopped working.
Very basic upload script:
if($_FILES['csvfile']['name']){
//if no errors...
if(!$_FILES['csvfile']['error']){
//now is the time to modify the future fle name and validate the file
$new_file_name = strtolower($_FILES['csvfile']['tmp_name']); //rename file
//move it to where we want it to be
move_uploaded_file($_FILES['csvfile']['tmp_name'], 'active_leads1.csv');
//echo 'Congratulations! Your file was accepted.';
}else{
//set that to be the returned message
//echo 'Ooops! Your upload triggered the following error: '.$_FILES['csvfile']['error'];
die("Unfortuanatly there was an error: {$_FILES['csvfile']['error']}");
}
}else{
die("Unfortuanatly there was an error: ".print_r($_FILES,true)."");
}
And equally the form is just as basic:
<form action="csvconvert.php" method="post" enctype="multipart/form-data">
<input type="file" name="csvfile" size="25" />
<input type="submit" name="submit" value="Submit" />
</form>
And yet, it fails on a file c. 11MB. Smaller files are ok.
Checked the post limit and upload limit, both are fine (256M and 128M), max input time is 240, and yet if I print_r $_POST and $_FILES they are both empty.
Has anyone come across this before? Any help would be appreciated!
If smaller files are ok, are you sure your upload_max_filesize is larger than 11MB? What is the value of upload_max_filesize when you use echo phpinfo();.

Image hosting with multiple servers

I'm developing an image host and wish to have images uploaded to a separate server from my web content, eg: http://i1.mysite.com instead of http://mysite.com/uploads. But I'm having some trouble figuring out how to do that.
Say I have this form:
<form action="http://mysite.com/upload" method="post" accept-charset="utf-8" enctype="multipart/form-data">
<input type="file" name="image" id="file_upload" />
<input type="submit" value="Upload" id="upload_submit" />
</form>
That will send an image file to /upload, where I can validate the file and save it, but that will be on the same server as the website is hosted, rather than a dedicated storage server. How can I achieve what I want without having the images uploaded on the same server as my web site?
I could always do:
<form action="http://i1.mysite.com/upload" method="post" accept-charset="utf-8" enctype="multipart/form-data">
<input type="file" name="image" id="file_upload" />
<input type="submit" value="Upload" id="upload_submit" />
</form>
which would send the image file to another server, but then when the image upload is complete I'd be redirected to http:/i1.mysite.com/upload.
Anyone have any experience with this and can recommend a course of action? Thank you!
Don't upload to the image server. Such content-specific servers should be optimized for serving up content, and not have to deal with consuming content.
Let the upload form send to your main site's server. You can then use other protocols to transfer the uploaded file(s) to the image servers. rsynch, scp, etc... This way you have all your "control" code in one location, and don't have to worry about synching databases and whatnot between multiple servers - all the data is kept on your main server, and the image servers just passively spit out image data.
I would recommend decoupling these two ideas. First, upload the image to your servers and in a separate process (perhaps a scheduled cron) move the images to other server. You likely do not want the user waiting for two uploads to finish.
Like the others have said, what you're trying to do is not optimal. If you really want to continue to do this, I'd suggest having the form submit to a PHP script which then processes it and places the file where it needs to be and then saves whatever information to the database that is necessary. You'll need to evaluate the best protocol for the data transfer from one server to the other. You'll probably end up using Curl, which you can learn about here and here as well as the curl docs
You could upload the image to your image host, and have it redirect back to your website afterward. One way to do this would be to add hidden "success" and "failure" URL inputs to the form:
<form action="http://i1.mysite.com/upload" method="post" accept-charset="utf-8" enctype="multipart/form-data">
<input type="file" name="image" id="file_upload" />
<input type="submit" value="Upload" id="upload_submit" />
<input type="hidden" name="success" value="http://mysite.com/success" />
<input type="hidden" name="failure" value="http://mysite.com/failure" />
</form>
The upload script on your image host would then redirect to the supplied URL after a successful upload:
<?php
.. handle uploaded file ..
if ($success) {
header ('Location: ' . $_REQUEST ['success']) ;
}
else {
header ('Location: ' . $_REQUEST ['error'] . '?message=' . $message) ;
}
?>

Categories