Here is the code i made, im expecting it to work but somewhere there must be an error. I can't figure out myself, Please help.
<?php
if(isset($_POST['submit'])){
$max_size = 500000;
$image_upload_path = "images/products/";
$allowed_image_extension = array('jpg','jpeg','png','gif');
for($i=0;$i<2;$i++)
{
//check if there is file
if((!empty($_FILES['image[]'][$i])) && ($_FILES['image[]']['error'][$i]==0))
{
//check extension
$extension = strrchr($_FILES['image']['name'][$i], '.');
if(in_array($extension,$allowed_image_extension))
{
//check file size.
if($_FILES['image']['size'][$i] > $max_size)
{
echo "file too big";
}
else if($_FILES['image']['size'][$i] < 1)
{
echo "file empty";
}
else
{
//we have pass file empty check,file extension check,file size check.
$the_uploaded_image = $_FILES['image']['tmp_name'][$i];
$the_uploaded_image_name = $_FILES['image']['name'][$i];
//replace empty space in filename with an underscore '_'
$the_uploaded_image_name = preg_replace('/\s/','_',$the_uploaded_image_name);
//get the file extension
$the_uploaded_image_extension = explode(',',$the_uploaded_ima开发者_开发问答ge_name);
$the_new_image_name = $the_uploaded_image_name."".md5(uniqid(rand(),true))."".$the_uploaded_image_extension;
$save_image_as = $the_new_image_name;
//check file exist
if(file_exists($image_upload_path."".$the_new_image_name))
{
echo "file".$image_upload_path."".$the_new_image_name." already exist";
}
else
{
if(move_uploaded_file($the_uploaded_image,$save_image_as))
{
echo "image".$the_uploaded_image_name." uploaded sucessfully";
//set the image path to save in database column
}
else
{
echo "there was an error uploading your image.";
}
}
}
}
else
{
echo "extension not allowed";
}
}
else
{
echo "please choose file to upload";
}
}
}
?>
<html>
<head><title>image upload</title></head>
<body>
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="image[]"/>
<input type="file" name="image[]"/>
<input type="submit" value="submit"/>
</form>
</body>
</html>
This is my new PHP code . Im getting both the result as found found not found not found. Will someone tell me what am i doing wrong here. The if else condition is seems to be not working as both the conditions are giving ouput. Why?
<?php
if(isset($_POST["submit"])) {
echo $_POST["submit"];
echo "<br/>";
for($i=0;$i<count($_FILES['image'])-1;$i++)
{
if(!empty($_FILES['image']['tmp_name'][$i]))
{
echo "found";
echo "<br/>";
}
else
{
echo "not found";
echo "<br/>";
}
}
}
else
{
echo "form is not posted";
}
?>
I guess the obvious WTF would be $_FILES['image[]'][$i]
, which should just be $_FILES['image'][$i]
(the []
in the name makes it an array, it's not part of the name).
I'm unwilling to troubleshoot anything beyond this for you without more information. Try this at various points in the code:
echo '<pre>';
var_dump($_POST); // or other variables
echo '</pre>';
This should help you to debug your own code, something you must learn to do.
精彩评论