Unable to upload selected image if number of selected image exceeds 10 - php

I write a code to upload a image in specific location and display a number of uploaded image count. It will work perfectly when I select below 8 images. But this same code does not worked when if I select 10 to 20 images. I really don't have any idea why it would work when selecting minimum number of images and not working if I select a larger number of images. Please find my below code
storeimage.php
<?php
require_once 'pdoconnectionusingclass.php';
?>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Multiple File Ppload with PHP</title>
</head>
<body>
<form action="storeimagename.php" method="POST" enctype="multipart/form-data">
<div>
<select name="moviename">
<option value = "">---Select---</option>
<?php
try
{
$dbobj=new database();
$dbobj->openconnection();
$sql='select * from tbl_movie';
$query=$dbobj->getdata($sql);
if(isset($query))
{
foreach ($query as $row)
{
echo '<option value='.str_replace(' ','_', $row['movie_name']).'>'.$row['movie_name'].'</option>';
}
}
$dbobj->closeconnection();
}
catch(Exception $e)
{
echo $e->getMessage();
}
?>
</div>
<br/>
<div>
<input type="file" id="file" name="files[]" multiple="multiple" accept="image/*" />
<input type="submit" value="Upload!" />
</div>
</form>
</body>
</html>
storeimagename.php
<?php
require_once 'pdoconnectionusingclass.php';
$valid_formats = array("jpg", "png", "gif", "bmp");
$max_file_size = 1048576 *10; //100 kb
$path = "uploads/"; // Upload directory
$count = 0;
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
// Loop $_FILES to exeicute all files
$total_image=count($_FILES['files']['name']);
$dbobj=new database();
$dbobj->openconnection();
for($i=0;$i<$total_image;$i++)
{
foreach ($_FILES['files']['name'] as $i => $name) {
if ($_FILES['files']['error'][$i] == 4) {
continue; // Skip file if any error found
}
if ($_FILES['files']['error'][$i] == 0) {
if ($_FILES['files']['size'][$i] > $max_file_size) {
$message[] = "$name is too large!.";
continue; // Skip large files
}
elseif( ! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats) ){
$message[] = "$name is not a valid format";
continue; // Skip invalid file formats
}
else{ // No error found! Move uploaded files
// echo $_FILES['files']['tmp_name'][$i];
// echo $_FILES['files']['name'][$i];
if(move_uploaded_file($_FILES["files"]["tmp_name"][$i], $path.$name))
{
$count++; // Number of successfully uploaded file
$gallery.=','.$name;
}
}
}
}
}
if(count==18)
{
$sql='insert into tbl_movie_gallery values((select movie_id from tbl_movie where movie_name='.str_replace('_',' ',$_POST['moviename']).')'.$gallery.');';
$dbobj->insertdata($sql);
}
//unset($_FILES['files']);
$dbobj->closeconnection();
}
?>
Program is terminated when executing this $total_image=count($_FILES['files']['name']); line if i select 10 to 20 files. $total_image displays count as 0. Can any one help what is the problem is here? Thanks in advance.

PHP has a setting for the maximum amount of files you can upload at once. The default is 20. You'll need to change this in your php.ini file.
The setting you're looking for is max_file_uploads
More information

Related

i want to insert image name in db where to put sql query i m new to php [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
<?php include 'connection.php';?>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Multiple File Upload with PHP</title>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" id="file" name="files[]" multiple="multiple" accept="image/*" />
<input type="submit" value="Upload!" />
</form>
</body>
</html>
<?php
$valid_formats = array("jpg", "png", "gif", "zip", "bmp");
$max_file_size = 1024*1000; //100 kb
$path = "uploads/"; // Upload directory
$count = 0;
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
// Loop $_FILES to exeicute all files
foreach ($_FILES['files']['name'] as $f => $name) {
if ($_FILES['files']['error'][$f] == 4) {
continue; // Skip file if any error found
}
if ($_FILES['files']['error'][$f] == 0) {
if ($_FILES['files']['size'][$f] > $max_file_size) {
$message[] = "$name is too large!.";
continue; // Skip large files
}
elseif( ! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats) ){
$message[] = "$name is not a valid format";
continue; // Skip invalid file formats
}
else{ // No error found! Move uploaded files
$ext = pathinfo($_FILES['files']['name'][$f], PATHINFO_EXTENSION);
$uniq_name = uniqid() . '.' .$ext;
move_uploaded_file($_FILES["files"]["tmp_name"][$f], $path . $uniq_name);
// $count++; // Number of successfully uploaded file
}
}
}
?>
You have to add your insert query inside the else statement when file successfully uploaded

How can upload Image on local server

After more 80 times (with use of all hint in this site also) without result. (I work on Debian Server and windows 10)
I do it on Internet but not on local server
my code are as:
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<form action="upload_OneImage.php" method="post" enctype="multipart/form-data">
<input type="file" name="photoU" /><br />
<input type="submit" name="submit" value="Upload" />
</form>
<?php
$valid_file = NULL;
if (isset($_POST['submit'])) {
//if they DID upload a file...
if ($_FILES['photoU']['name']) {
//if no errors...
if (!$_FILES['photoU']['error']) {
//check the size of file and validate it's size
$new_file_name = strtolower($_FILES['photoU']['name']);
//change the all the capital letter to lower letter
echo "file_name= $new_file_name <br />";
if ($_FILES['photoU']['size'] > (1024000)) { //can't be larger than 1 MB
$valid_file = FALSE;
$message = "Oops! Your file's size is too large.";
} else {
$valid_file = TRUE;
}
//if the test was successfull
if ($valid_file) {
//move it to directory that name "upload"
move_uploaded_file($_FILES["file"]["tmp_name"][0], "upload/" . $_FILES["file"]["name"][0]);
//move_uploaded_file($_FILES['photoU']['tmp_name'], 'upload/' . $new_file_name);
$message = 'Congratulations! Your file was accepted.<br />';
}
}
//if there is an error...
else {
//set that to be the returned message
$message = 'Sorry! Your upload triggered the following error: ' . $_FILES['photoU']['error'];
}
}
echo "$message ";
}
?>
</body>
</html>
I have a directory as "images". It contains the imges. And I have another directory as "upload" to keep the uploads files.
I also want to show the image in small size durring upload.
Can somebody tell me what is problem?

php upload multiple file with predefined file name

I want to upload multiple files with predefined file name, is it possible? how to retrieve the name from php?
How to retrieve the name 'doc1' and 'doc2' from the php?
My code:
<?
$valid_formats = array("jpg", "png", "gif", "zip", "bmp");
$max_file_size = 1024*1024; //1MB
$path = "uploads/doc/"; // Upload directory
$count = 0;
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
// Loop $_FILES to exeicute all files
foreach ($_FILES['files']['name'] as $f => $name) {
if ($_FILES['files']['error'][$f] == 4) {
continue; // Skip file if any error found
}
if ($_FILES['files']['error'][$f] == 0) {
if ($_FILES['files']['size'][$f] > $max_file_size) {
$message[] = "$name is too large!.";
continue; // Skip large files
}
elseif( ! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats) ){
$message[] = "$name is not a valid format";
continue; // Skip invalid file formats
}
else{ // No error found! Move uploaded files
if(move_uploaded_file($_FILES["files"]["tmp_name"][$f], $path.$name))
$count++; // Number of successfully uploaded file
}
}
}
}
?>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Multiple File Ppload with PHP</title>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
Doc1: <input type="file" id="file" name="files['doc1']" multiple="multiple" accept="image/*" /><br>
Doc2: <input type="file" id="file" name="files['doc2']" multiple="multiple" accept="image/*" /><br>
<input type="submit" value="Upload!" />
</form>
</body>
</html>
Do check php code below that will return all details of files you uploaded.
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
{
echo "File Names:<br>Doc1= ".$_FILES['files']['name']["'doc1'"];
echo "<br>Doc2= ".$_FILES['files']['name']["'doc2'"];
echo "<br><br>";
echo "File Types:<br>Doc1= ".$_FILES['files']['type']["'doc1'"];
echo "<br>Doc2= ".$_FILES['files']['type']["'doc2'"];
echo "<br><br>";
echo "Tmp Name:<br>Doc1= ".$_FILES['files']['tmp_name']["'doc1'"];
echo "<br>Doc2= ".$_FILES['files']['tmp_name']["'doc2'"];
echo "<br><br>";
echo "Error:<br>Doc1= ".$_FILES['files']['error']["'doc1'"];
echo "<br>Doc2= ".$_FILES['files']['error']["'doc2'"];
echo "<br><br>";
echo "Size:<br>Doc1= ".$_FILES['files']['size']["'doc1'"];
echo "<br>Doc2= ".$_FILES['files']['size']["'doc2'"];
echo "<br><br>";
}
?>

Multifile upload

Looking for the following solutions:
Form html / php provide the ability to add multiple graphic files (up to 11 files, max 5MB).
When sending to a server script should do the following:
check if the file has a good extension (jpg, png);
re-name each file according to the formula < string up to 32 characters > _ < 000 to 010 >. < extension >;
add file names to the table (one file = one column, row);
compress to a maximum resolution of 1280x1024;
change the proportions of graphics on 4: 3 images while maintaining the same proportions;
lessen the size of the file and save to the server in the appropriate folder.
I'm interested in possibly easy to use solution.Thank you very much for your help.
try it,
HTML Code
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Multiple File Ppload with PHP</title>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" id="file" name="files[]" multiple="multiple" accept="image/*" />
<input type="submit" value="Upload!" />
</form>
</body>
</html>
PHP CODE
<?php
$valid_formats = array("jpg", "png");
$max_file_size = 1280*1024;
$path = "uploads/"; // Upload directory
$count = 0;
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
// Loop $_FILES to exeicute all files
foreach ($_FILES['files']['name'] as $f => $name) {
if ($_FILES['files']['error'][$f] == 4) {
continue; // Skip file if any error found
}
if ($_FILES['files']['error'][$f] == 0) {
if ($_FILES['files']['size'][$f] > $max_file_size) {
$message[] = "$name is too large!.";
continue; // Skip large files
}
elseif( ! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats) ){
$message[] = "$name is not a valid format";
continue; // Skip invalid file formats
}
else{ // No error found! Move uploaded files
if(move_uploaded_file($_FILES["files"]["tmp_name"][$f], $path.$name))
$count++; // Number of successfully uploaded file
}
}
}
}
?>

How to use a php variable in other php script on the same php page

The below is my source code where I want to use the variable $r1 which is at the bottom php script in another php script which is on top on the same page. I need a simple solution which solves this problem. I want to use that variable in the update query which is present in the code.
<?php
$con=mysql_connect("localhost","root","") or die("could not connect to db");
mysql_select_db("test");
$valid_formats = array("jpg", "png", "gif", "zip", "bmp","MP4","3GP");
$max_file_size = 1024*10000; //100 kb
//$path = "uploads/"; // Upload directory
$count = 0;
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
// Loop $_FILES to execute all files
foreach ($_FILES['files']['name'] as $f => $name) {
if ($_FILES['files']['error'][$f] == 4) {
continue; // Skip file if any error found
}
if ($_FILES['files']['error'][$f] == 0) {
if ($_FILES['files']['size'][$f] > $max_file_size) {
$message[] = "$name is too large!.";
continue; // Skip large files
}
elseif( ! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats) ){
$message[] = "$name is not a valid format";
continue; // Skip invalid file formats
}
else{ // No error found! Move uploaded files
move_uploaded_file($_FILES["files"]["tmp_name"][$f],"uploads/" . $_FILES["files"]["name"][$f]);
// Number of successfully uploaded files
$file="uploads/".$_FILES["files"]["name"][$f];
/* $sql = "Update media set path = '$file' where username = '$r1'";
if (!mysql_query($sql))
{
die('Error: ' . mysql_error());
}*/
}
}
}
}
$sql="select * from media";
$query=mysql_query($sql);
while($row=mysql_fetch_array($query))
{
$image=$row[2];
if($image!='')
{
echo "<img src='".$row['path']."' width='175' height='200' />";
}
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Multiple File Upload with PHP - Demo</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="wrap">
<h1><a>Multiple File Upload with PHP</a></h1>
<?php
# error messages
if (isset($message)) {
foreach ($message as $msg) {
printf("<p class='status'>%s</p></ br>\n", $msg);
}
}
# success message
if($count !=0){
printf("<p class='status'>%d files added successfully!</p>\n", $count);
}
?>
<p>Max file size 100kb, Valid formats jpg, png, gif</p>
<br />
<br />
<!-- Multiple file upload html form-->
<form action="" method="post" enctype="multipart/form-data">
<span style="font-size:12pt;">Channel: </span> <select name="channels">
<option value="">Channel</option>
<?php
$sql_query="SELECT * FROM media";
$result1 = mysql_query($sql_query) or die(mysql_error());
while($data=mysql_fetch_row($result1))
{
?>
<option value="<?php $r1 = $data[0]; echo $r1;?>" ><?php $r1 = $data[1]; echo $r1 ?></option>
<?php
}
?>
</select>
<input type="file" name="files[]" id="image" multiple="multiple">
<input type="submit" value="Upload">
</form>
</div>
</body>
</html>
If you want to have the value of the <select name="channels"> you can use this
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
$r1 = $_POST["channels"];
//your code here
}
Hope this help.

Categories