Hi I'm fairly new to HTML, PHP, MySQL etc.. I am wondering if there is a predefined upload limit using $_FILES
. I ask because when I try to upload 8 images of around 1.5 megabytes the code does not work but when I upload 10 images of around 60 kilobytes the code works fine. Here is my code and feel free to make any criticisms/comments about it:
</head>
<body>
<form action="test.php" method="POST" enctype="multipart/form-data" >
<input type="file" name="image[]" multiple="multiple">
<input type="submit" value="upload">
</form>
<?php
include 'connect.php';
if(!empty($_FILES['image']['tmp_name'])){
$allowed = array('jpg', 'gif', 'png', 'jpeg');
$count = 0;
foreach($_FILES['image']['name'] as $key => $name){
$image_name = $name;
$tmp = explode('.', $image_name);
$image_extn = strtolower(end($tmp)); //can only reference file
$image_temp = $_FILES['image']['tmp_name'][$count];
$count = $count +1;
if(in_array($image_extn, $allowed) === true){
$image_path = 'images/' . md5($image_name) . '.' . $image_extn;
move_uploaded_file($image_temp, $image_path);
mysql_query("INSERT INTO store VALUES ('', '$image_name', '$image_path')") or die(mysql_error());
$lastid = mysql_insert_id();
$image_link = mysql_query("SELECT * FROM store WHERE id = $lastid");
$image_link = mysql_fetch_assoc($image_link);
$image_link = $image_link['image'];
$uploaded[] = $image_link;
}
else{
echo 'Incorrect file type. Allowed: ';
echo implode(', ', $allowed);
}
}
}
if(!empty($uploaded)){
foreach($uploaded as $new){
echo "<a href = $new>$new</a><p></p>";
}
}
else{
echo "Please select an image.";
}
?>
</body>
</html>
Here are the settings you want to change in php.ini:
post_max_size This setting controls the size of an HTTP post, and it needs to be set larger than the upload_max_filesize setting.
upload_max_filesize This value sets the maximum size of an upload file.
Remember to restart your web server after making these changes.
Ref:
A few settings in your php.ini could be causing this. Look into memory_limit, post_max_size, upload_max_filesize. You could also be timing out. The best way to find out specifically is error_reporting(E_ALL);ini_set('display_errors','1');
In php.ini, there will be a post_max_size
and upload_max_filesize
directive.
You should also define a MAX_FILE_SIZE
hidden input within your form, as per http://www.php.net/manual/en/features.file-upload.post-method.php
You are trying to upload an array of files, you will not be able to upload more than 20 files due to max_file_uploads limit in php.ini
which is by default set to 20
.
So you have to increase this limit to upload more than 20 files.
Note: max_file_uploads
can NOT be changed outside php.ini. See PHP "Bug" #50684