Jump to content

why is it over writing


Dorky

Recommended Posts

$filepaths[] = $_FILES['new_image'];
foreach ($filepaths as $filepath)
{
$imagename = strtolower($filepath['name']);
$charref = substr($imagename, 0, strrpos($imagename, '.')); 
if(ctype_alnum($charref)) 
{

this is for a form with multiple image uploads that need to be resized. im trying to loop them thrue the resize script but it over writes as it goes untill the only img when its done is the final image field.

Link to comment
https://forums.phpfreaks.com/topic/179600-why-is-it-over-writing/
Share on other sites

i added $imgkey => and use it to rename the file and whats happening is it writes the final image as 0 so its as if my foreach loop isnt advancing thrue the array but it is because it because its running the first image but then overwriting it with the name of the first.

if (isset($_FILES['new_image']))
{
if (!empty($_POST['address']) && !empty($_POST['city']) && !empty($_POST['state']) )
{
//...
$filepaths[] = $_FILES['new_image'];
$listing = $_POST['address'];
mkdir("/home/p14tnue3/public_html/howton/images/gallery/$listing" , 0755 );
mkdir("/home/p14tnue3/public_html/howton/images/gallery/thumbs/$listing" , 0755 );
foreach ($filepaths as $imgkey => $filepath)
{
$imagename = strtolower($filepath['name']);
$charref = substr($imagename, 0, strrpos($imagename, '.')); 
if(ctype_alnum($charref)) 
{
if (($pos = strrpos($imagename, ".")) === FALSE) { echo  "<p class=\"p7\">Fail</p><br />"; exit; }
else {  $extension = substr($imagename, $pos + 1); $imagename = "$imgkey.$extension"; }
if ( $extension == "jpg" || $extension == "gif" || $extension == "png"   ) 
{ 
$source = $filepath['tmp_name'];
$target = "images/gallery/$listing/$imagename";
move_uploaded_file($source, $target);
$file = "images/gallery/$listing/$imagename"; 
$save = "images/gallery/$listing/$imagename";
list($width, $height) = getimagesize($file) ; 
if ($width > "500" )
{ 
$modwidth = 500; 
}
else
{
$modwidth = $width ;
} 
$diff = $width / $modwidth;
$modheight = $height / $diff; 
$tn = imagecreatetruecolor($modwidth, $modheight) ; 
if( $extension == "jpg" ) { $image = imagecreatefromjpeg ($file); }
if( $extension == "gif" ) { $image = imagecreatefromgif ($file);  }
if( $extension == "png" ) { $image = imagecreatefrompng ($file);  } 
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; 
if( $extension == "jpg" ) { imagejpeg($tn, $save, 100) ;  }
if( $extension == "gif" ) { imagegif($tn, $save, 100) ;  }
if( $extension == "png" ) { imagepng($tn, $save, 9) ;  }
$save = "images/gallery/thumbs/$listing/$imagename";
list($width, $height) = getimagesize($file) ; 
if ($width > "100" )
{ 
$modwidth = 100; 
}
else
{
$modwidth = $width ;
}  
$diff = $width / $modwidth;
$modheight = $height / $diff; 
$tn = imagecreatetruecolor($modwidth, $modheight) ; 
if( $extension == "jpg" ) { $image = imagecreatefromjpeg ($file); }
if( $extension == "gif" ) { $image = imagecreatefromgif ($file);  }
if( $extension == "png" ) { $image = imagecreatefrompng ($file);  } 
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; 
if( $extension == "jpg" ) { imagejpeg($tn, $save, 100) ;  }
if( $extension == "gif" ) { imagegif($tn, $save, 100) ;  }
if( $extension == "png" ) { imagepng($tn, $save, 9) ;  }
$imginfo = "Images Uploaded Successfully";
$address = "";
$city = "";
$state = "";
} 
else 
{
$imginfo = "Invalid or empty File <br>Valid file types .jpg .gif .png";
$address = $_POST['address'];
$city = $_POST['city'];
$state = $POST['state'];
} 
}
else 
{ 
$imginfo = "Image name may have alphanumeric characters only and no spaces $charref";
$address = $_POST['address'];
$city = $_POST['city'];
$state = $POST['state'];
}
}
//...
}
else
{
$imginfo = "Please complete all fields for listing";
$address = $_POST['address'];
$city = $_POST['city'];
$state = $POST['state'];
}
}

Right what you need to do is make a HTML array in your form so that all of the images are recieved not just the last image. Then you will want to capture either the entire $_FILES array or the HTML array that you create.

http://php.net/manual/en/features.file-upload.php

yeah i figured that out but i have no idea how to get this into a workable array. it doesnt keep the right structure for some reason. this is the structure before and after the foreach loop indicated by "inside" and "outside"

 

outside Array ( [name] => Array ( [0] => d.png [1] => sky.jpg ) [type] => Array ( [0] => image/png [1] => image/jpeg ) [tmp_name] => Array ( [0] => /tmp/phpX40sIK [1] => /tmp/php1TmAiS ) [error] => Array ( [0] => 0 [1] => 0 ) => Array ( [0] => 1028 [1] => 110116 ) )

inside Array ( [name] => [type] => [tmp_name] => [error] => 4 => 0 ) inside Array ( [name] => Array ( [0] => d.png [1] => sky.jpg ) [type] => Array ( [0] => image/png [1] => image/jpeg ) [tmp_name] => Array ( [0] => /tmp/phpX40sIK [1] => /tmp/php1TmAiS ) [error] => Array ( [0] => 0 [1] => 0 ) => Array ( [0] => 1028 [1] => 110116 ) )

outside Array

(

[name] => Array ( [0] => d.png [1] => sky.jpg )

[type] => Array ( [0] => image/png [1] => image/jpeg )

[tmp_name] => Array ( [0] => /tmp/phpeKr1qV [1] => /tmp/phpiII3hp )

[error] => Array ( [0] => 0 [1] => 0 )

=> Array ( [0] => 1028 [1] => 110116 )

)

 

inside Array ( [0] => d.png [1] => sky.jpg )

inside Array ( [0] => image/png [1] => image/jpeg )

inside Array ( [0] => /tmp/phpeKr1qV [1] => /tmp/phpiII3hp )

inside Array ( [0] => 0 [1] => 0 )

inside Array ( [0] => 1028 [1] => 110116 )

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.