Jump to content

multi image upload form


regoch

Recommended Posts

I got this script for upload images i few years ago, and working fine on 12 different pages, but on one of them stop working 2 days ago. Pages are all on same hosting so I guess  if they change php version that script won't work on every page, not on just one. Script sart upload images (take 10-15 seconds) but wont upload image, just put name of image in sql. So if anyone have some idea please help! Thanks!

p.s. slika means image on croatian!

<?php
require_once('zastita.php');
//Start session
session_start();

//Podaci za spajanje na bazu podataka
require_once('config.php');

//Array za provjeru jesu li ispunjena sva polja
$errmsg_arr = array();

//Isključivanje provjere jesu li ispunjena sva polja
$errflag = false;

//Spajanje na mysql server
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) {
	die('Greška prilikom spajanja na server: ' . mysql_error());
}

//Odabir baze podataka
$db = mysql_select_db(DB_DATABASE);
if(!$db) {
	die("Greška prilikom spajanja na bazu podataka");
}
?>
<?php include ("include/head.php"); ?>
<body>
<div id="main_container">

<div class="header">
    <div class="logo"><a href="index.php"><img src="images/logo.gif" alt="" title="" border="0" /></a></div>
    
    <div class="right_header">Administrator: <?php echo $_SESSION['SESS_FIRST_NAME'];?> | <a href="odjava.php" class="logout">Odjavi se</a></div>
    <div class="jclock"></div>
    </div>
    
    <div class="main_content">
    
                    <?php include ("include/menu.php");?>                
                    
    <div class="center_content">  
    
           
    <div class="right_content">            
        
       
     <h2>Admin Panel - Unos slika</h2>
     
         <div class="form">
<?php 
error_reporting(0);

$change="";
$abc="";


define ("MAX_SIZE","4096");
function getExtension($str) {
         $i = strrpos($str,".");
         if (!$i) { return ""; }
         $l = strlen($str) - $i;
         $ext = substr($str,$i+1,$l);
         return $ext;
}

$errors=0;




$slike = array(file, file1, file2, file3, file4);
  if($_SERVER["REQUEST_METHOD"] == "POST")
{
//	$image =$_FILES["file"]["name"];
//	$uploadedfile = $_FILES['file']['tmp_name'];
$filearray = array();
$filearray1 = array();
$k=0;

  foreach($slike as $slika){ 
$image =$_FILES[$slika]["name"];
$uploadedfile = $_FILES[$slika]['tmp_name'];
	if ($image) 
	{

		$filename = stripslashes($_FILES[$slika]['name']);

  		$extension = getExtension($filename);
		$extension = strtolower($extension);


if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) 
		{

			$change='<div class="msgdiv">Unknown Image extension </div> ';
			$errors=1;
		}
		else
		{

$size=filesize($_FILES[$slika]['tmp_name']);


if ($size > MAX_SIZE*4096)
{
$change='<div class="msgdiv">Slika je veća od 4096kb!</div> ';
$errors=1;
}


if($extension=="jpg" || $extension=="jpeg" )
{
$uploadedfile = $_FILES[$slika]['tmp_name'];
$src = imagecreatefromjpeg($uploadedfile);

}
else if($extension=="png")
{
$uploadedfile = $_FILES[$slika]['tmp_name'];
$src = imagecreatefrompng($uploadedfile);

}
else 
{
$src = imagecreatefromgif($uploadedfile);
}

echo $scr;

list($width,$height)=getimagesize($uploadedfile);


$newwidth=600;
$newheight=($height/$width)*$newwidth;
$tmp=imagecreatetruecolor($newwidth,$newheight);

$newheight1=60;
$newwidth1=($width/$height)*$newheight1;
$tmp1=imagecreatetruecolor($newwidth1,$newheight1);

imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);

imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1,$width,$height);


//$filename = "images/". $_FILES['file']['name'];

//$filename1 = "images/small". $_FILES['file']['name'];
$rand = rand(0000000000, 9999999999);
$image_name=(date("dmY")+$k).'_'.$rand.'.'.$extension;
$filename = "../slike/".$image_name;
$filename1 = "../slike/thumbnails/".$image_name;
$filename3 = $image_name;
$filename4 = $image_name;
$nekretnina_id = $_GET['nekretnina_id'];

imagejpeg($tmp,$filename,100);

imagejpeg($tmp1,$filename1,100);



$filearray[$k]= $filename3;
$filearray1[$k]= $filename4;
$filearray2[$k]= $nekretnina_id;
$k++;

// 3. Perform database query
mysql_query("INSERT INTO slike set picture_name='$filename3', picture_name1='$filename4', nekretnina_id='$nekretnina_id'");
echo '<br /><img src="/slike/thumbnails/'.$filename4.'"><br />';
imagedestroy($src);
imagedestroy($tmp);
imagedestroy($tmp1);
}}
}
}
// If no errors registred, print the success message
if(isset($_POST['Submit']) && !$errors) 
{
	if ($filename3==""){
echo " <table width='400px' align='center' cellspacing='0'>
<tr bgcolor='#E6e6e6' height='26'>
	<td align='center' width='100%' colspan='2'><strong>Potvrda</strong></td>
</tr>
<tr bgcolor='#F6f6f6'>
    <td align='center' style='border-left:#E6e6e6 1px solid;border-bottom:#E6e6e6 1px solid;'><img src='images/warning.png'></td>
<td align='center' width='80'% style='border-right:#E6e6e6 1px solid;border-bottom:#E6e6e6 1px solid;'><strong>Slike nisu unešene.</strong></td>
</tr>
</table><br />";
}
else{ 
	echo " <table width='400px' align='center' cellspacing='0'>
<tr bgcolor='#E6e6e6' height='26'>
	<td align='center' width='100%' colspan='2'><strong>Potvrda</strong></td>
</tr>
<tr bgcolor='#F6f6f6'>
    <td align='center' style='border-left:#E6e6e6 1px solid;border-bottom:#E6e6e6 1px solid;'><img src='images/valid.png'></td>
<td align='center' width='80'% style='border-right:#E6e6e6 1px solid;border-bottom:#E6e6e6 1px solid;'><strong>Slike uspješno unesene.</strong></td>
</tr>
</table><br />";
}
}
/*If no errors registred, print the success message
if(isset($_POST['Submit']) && !$errors) 
{

   // mysql_query("update {$prefix}users set img='$big',img_small='$small' where user_id='$user'");
	$change=' <div class="msgdiv">Image Uploaded Successfully!</div>';
}*/


?>
     <div align="center" id="err">
<?php echo $change; ?>  </div>
		    
<form method="post" action="" enctype="multipart/form-data" name="form1">
<?php foreach($slike as $c){ ?>
    <input size="25" name="<?php echo $c?>" type="file" style="font-family:Verdana, Arial, Helvetica, sans-serif; font-size:10pt" class="box"/><br />
    <?php } ?>
Dozvoljen je unos slika veličine do 4 mb! <br />
<input type="submit" id="mybut" value="Unesi slike" name="Submit"/>
</form>
  
<?php 
// 5. Close connection
mysql_close($connection);
?>
         </div>  
      
     
     </div><!-- end of right content-->
            
                    
  </div>   <!--end of center content -->               
                    
                    
    
    
    <div class="clear"></div>
    </div> <!--end of main content-->

    
    <div class="footer"><br />
    
<?php include ("include/footer.php"); ?>
    
    </div>

</div>		
</body>
</html>

Link to comment
Share on other sites

If that script is working fine on many pages and just not one, then obviously there is something different happening on that one page. Plus, if you are using that same code on multiple pages you should create a function and include that function on the multiple pages!!! That way you don't have to maintain multiple copies of the same code.

 

But, I think I see your problem. You have a foreach loop to apparently try and access the multiple files:

  foreach($slike as $slika){ 
$image =$_FILES[$slika]["name"];
$uploadedfile = $_FILES[$slika]['tmp_name'];

 

But, look at the $slike array

$slike = array(file, file1, file2, file3, file4);

 

Those are not the strings of 'file', 'file1', 'file2', etc. Since you did not enclose the values in quote marks the PHP parser will interpret those as constants. Since I assume you didn't define those constants the parser is probably using a null value. (Although I'm surprised the first one, 'file', is not producing an error, because that is a function name). Your array should probably look like this

 

$slike = array('file', 'file1', 'file2', 'file3', 'file4');

 

But, really, that code is horribly inefficient. I'm surprised that you've been using this code for years and haven't made it into a function to make it reusable.

Link to comment
Share on other sites

I change to

 $slike = array('file', 'file1', 'file2', 'file3', 'file4');

but get to upload black images and only thumbnails, not large images.

Permissions for the directories is 777.

Never change script for years because I'm not very good in php, so when I find that one that work didn't change anything! :)

Link to comment
Share on other sites

these suggestions are not fully on topic here, but they are things that I notice..

 

1. an easier way to get a file extension is

$extension = end(explode(".",$_FILES['image']['name']))

 

2. never set permissions to files to 777, this will cause major security issues, the highest you should have to or need to go securely should be 755

 

3. look a view source to make sure the php in your form is doing what you want it to do... something is telling me it's not..

Link to comment
Share on other sites

these suggestions are not fully on topic here, but they are things that I notice..

 

1. an easier way to get a file extension is

$extension = end(explode(".",$_FILES['image']['name']))

 

2. never set permissions to files to 777, this will cause major security issues, the highest you should have to or need to go securely should be 755

 

3. look a view source to make sure the php in your form is doing what you want it to do... something is telling me it's not..

Thanks! I will try to edit!

Link to comment
Share on other sites

  • 1 year later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.