Uploading image using PHP causes website to go down - why?
6 replies, posted
I'm trying to upload and overwrite an image on my website. It has no problem doing a very small image, but when I upload an image that's 800kb, it freaks out, and my website stops loading for 5-10 minutes. What would cause this?
This is the form code:
[code]
<form action="doeditmain.php?section=featuredpic" method="post" enctype="multipart/form-data">
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" id="Submit" value="Submit" />
</form>
[/code]
And this is the actual PHP code:
[code]
// Upload and Rename File
if (isset($_POST['submit'])) {
$filename = $_FILES["file"]["name"];
$file_basename = substr($filename, 0, strripos($filename, '.')); // get file extention
$file_ext = substr($filename, strripos($filename, '.')); // get file name
$filesize = $_FILES["file"]["size"];
$allowed_file_types = array('.png');
if (in_array($file_ext,$allowed_file_types))
{
// rename file
$newfilename = "header_cuttingboard" . $file_ext;
if (file_exists("../assets/header/" . $newfilename))
{
move_uploaded_file($_FILES["file"]["tmp_name"], "../assets/header/" . $newfilename);
echo "success!";
}
else
{
echo "fail!"
}
}
elseif (empty($file_basename))
{
echo "Please select a file to upload.";
}
elseif ($filesize > 200000)
{
echo "The file you are trying to upload is too large.";
}
else
{
echo "Only these file types are allowed for upload: " . implode(', ',$allowed_file_types);
unlink($_FILES["file"]["tmp_name"]);
}
}
[/code]
Shared hosting or do you have enough control over the hosting environment to check logs?
Shared hosting :(
Sounds like you need a new host.
Judging by the amount of indentation, I'm guessing that there is much more code than what you are showing us here, maybe the fault is at another place in your code?
You don't mention wether the file actually gets uploaded or not. (But it probably doesnt since you set your own limit to 200kb in your code.)
What is your INI settings for upload_max_filesize, post_max_size & max_execution_time ?
When you say that your website "goes down" do you mean that it doesnt load in new tabs either? If so, that is probably because that the session has not been closed with session_write_close().
Session data is saved in a file that gets locked when you call session_start, when a file is locked it cannot be accessed by any other process, that means that you cant do stuff concurrent with the same session, so you have to close the session before you can start a new script using it (If not the second script will be blocked till the lock is removed when the first script has ended).
Also shared hosting sucks, sure its cheap. But what you get is what you pay for. A VPS is a little more expensive, but you get full control of your enviroment.
Throw me a PM if you're looking for a cheap VPS I will be able to give you a discount.
[PHP]
elseif ($filesize > 200000) {
echo "The file you are trying to upload is too large.";
}
[/PHP]
Aren't you telling it to not get bigger than 195 kilobyte?
Also as reeferdk [URL="http://facepunch.com/showthread.php?t=1272831&p=40798914&viewfull=1#post40798914"]says[/URL], it does look like there are more.
Could you put it on pastebin?
Sorry, you need to Log In to post a reply to this thread.