I'm stumped.
I have a script that allows a user to upload up to 4 images to a temp folder, then it resizes via imagemagick and stores in a different folder.
It works. sometimes. Sometimes it says it worked but not all of the images were actually loaded/saved. Sometimes it's one out of four that didn't load, or two out of four. Sometimes it's two out of two....
I thought maybe it was because I'd put the resize inside an IF loop, so I commented out the IF loop and it still doesn't work all the time.
Then I thought it might have something to do with punctuation/spaces in file names, but even with solid string file names, it just doesn't work sometimes.
Hopefully it's something very simple.
/************************************
variables to change; site dependent:
************************************/
//
// where you want your uploaded picture to be stored
$destination_file = "images/temp/";
$upload_dir=$destination_file;
//
// where you want the final picture and thumbnail to be stored
$final_pic_location = "images/adverts/";
//
// FTP connection information
$ftp_server = "mywebsite";
$ftp_user_name = "myname";
$ftp_user_pass = "mypassword";
//
// dimension you'd like for your final big image
$image_height=300;
$image_width=300;
//
/******************************/
?>
<?php
if ($_POST['save_pics']=="yes"){
// this establishes unique file names. a bit of overkill perhaps
$client=$_SERVER["REMOTE_ADDR"];
$rightnow=$client.date('MdYHis');
$extension=substr(md5($rightnow),4);
$destination_file.=$extension; // does not yet have .jpg suffix
$final_pic_location.=$extension;
$success = 0;
$fail = 0;
$picslocation="";
$fpicsloc="";
$uploaddir = 'images/temp/';
for ($i=0;$i<4;$i++){
if($_FILES['userfile']['name'][$i]){
$uploadfile = $uploaddir . basename($_FILES['userfile']['name'][$i]);
$ext = strtolower(substr($uploadfile,strlen($uploadfile)-3,3));
if (preg_match("/(jpg|jpeg)/",$ext)){
if (move_uploaded_file($_FILES['userfile']['tmp_name'][$i], $uploadfile)){
$success++;
echo '<br /><b> '.$_FILES['userfile']['name'][$i].' </b>: SUCCESS.'."\n";
$this_final_pic_location=$final_pic_location.$i.'.jpg';
$picslocation.=$uploadfile.'|'; // this is for the database
$fpicsloc.=$this_final_pic_location.'|';
//$sizes = getimagesize($uploadfile);
//if ($sizes[1]>$image_height || $sizes[0]>$image_width){ ; //1=height,0=width
$image_upload= 'convert '.$uploadfile.' -resize '.$image_height.'x'.$image_width.' '.$this_final_pic_location; // reduces the uploaded image to manageable size
$lastline = system($image_upload,$return);
//}
}else{
echo '<br /><b>'.$_FILES['userfile']['name'][$i].'</b>: FAILED. "An error occurred". Please try again.'."\n";
$fail++;
}
}else{
echo '<br /><b>'.$_FILES['userfile']['name'][$i].'</b>: FAILED (wrong file type) Use JPEG images only please.'."\n";
$fail++;
}
}
}
echo "<br /><br /> Number of files Uploaded:".$success;
echo "<br /> Number of files Failed:".$fail;
echo "<br /> Picslocation: $picslocation";
echo "<br /> fpicsloc: $fpicsloc";
} else { //end if $save_pics
?>
<form enctype="multipart/form-data" action="<?php echo $PHP_SELF; ?>" method="post" width="95%">
Image1: <input class="imgfield" name="userfile[]" type="file" /><br />
Image2: <input class="imgfield" name="userfile[]" type="file" /><br />
Image3: <input class="imgfield" name="userfile[]" type="file" /><br />
Image4: <input class="imgfield" name="userfile[]" type="file" /><br />
<input type="hidden" value="yes" name="save_pics" />
<input type="submit" value="Upload" />
</form>
<?php } ?>