Upload Issues...
Posted: Fri Jul 09, 2010 4:49 pm
Hi all,
I've been using this upload script successfully for a while but I've been trying it today on a different serve
(within a codeigniter framework) and I'm getting some odd results - first it always (I mean always) falls over
and stops at the File Type Check (even though I know I'm using good image files) and if I take out that check it runs
through but lists the image path as "blah/blah/blah." with no "jpgh or "png" on the end and today it simply wouldn't
actually place the files in the desired dir atall - but behaved as if it had.
Can anyone enlighten me as to my mistakes or the peculiarities of codeigniter ?
Best wishes
Monty
I've been using this upload script successfully for a while but I've been trying it today on a different serve
(within a codeigniter framework) and I'm getting some odd results - first it always (I mean always) falls over
and stops at the File Type Check (even though I know I'm using good image files) and if I take out that check it runs
through but lists the image path as "blah/blah/blah." with no "jpgh or "png" on the end and today it simply wouldn't
actually place the files in the desired dir atall - but behaved as if it had.
Can anyone enlighten me as to my mistakes or the peculiarities of codeigniter ?
Best wishes
Monty
Code: Select all
//Define an array of acceptable image extensions
$acceptableextensions = array('jpg','jpeg','png','gif');
//Define an array of acceptable MIME types
$acceptablemimetypes = array('image/jpg','image/jpeg','image/png','image/gif');
//Define maximum file size
$maxfilesize = 1024000; // 1024000 bytes equals imeg
function getExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
// Now initialize vars to be printed to page in the HTML section so our script does not return errors
// they must be initialized in some server environments
$error_msg = "";
$success_msg = "";
$firstname = "";
$lastname = "";
$user_pic = "";
// Parsing section for the picture... only runs if they attempt to upload a picture
if ((!empty($_FILES['fileField'])) && ($_FILES['fileField']['error'] == 0)){
$image = $_FILES['fileField']['name'];
$filename = stripslashes($_FILES['fileField']['name']);
$extension = getExtension($filename);
$extension = strtolower($extension);
//Filetype check
if (!in_array($extension,$acceptableextensions)){
exit("Upload failed.<BR>Unacceptable file type.<br. Use only jpg, jpeg, png or fig formats");
echo '<h1>Only try to upload .jpg, .jpeg, .png and .gif files!</h1>';
$errors=1;
//Filesize check
} else if ($_FILES['fileField']['size'] > $maxfilesize ) {
$error_msg = 'Ooops - your image was too large,<br> 250kb Maximum. Please try again.';
$fail = "true";
unlink($_FILES['fileField']['tmp_name']);
} else {
// rename the picture incrementally
$extension = getExtension($filename);
$extension = strtolower($extension);
$groovy = sizeof(glob("members/$id/images/*"));
$groovy = ++$groovy;
$image_name=$groovy.'.'.$extension;
$newname="".$image_name;
$gowhere = mysql_real_escape_string($_POST['gowhere']);
// INSERT image into the DB
$sql = mysql_query("INSERT INTO pictures
(UID,filename,dirpath,gallery,dtadded)
VALUES ('$id','$newname','/members/$id/images/$newname','$gowhere',now())")
or die (mysql_error());
$place_file = move_uploaded_file( $_FILES['fileField']['tmp_name'], "members/$id/images/".$newname);
chmod ("members/$id/images/$newname", 0644);
//// RESIZE !!!!!
$originalImage = "members/$id/images/".$newname;
$maxHeight = 500;
$maxwidth = 780;
list($width, $height) = getimagesize($originalImage);
$xscale=$width/$maxwidth;
$yscale=$height/$maxHeight;
//// CALCULATE THE NEW SIZE FOR THE IMAGE
if ($yscale>$xscale){
$new_width = round($width * (1/$yscale));
$new_height = round($height * (1/$yscale));
}
else {
$new_width = round($width * (1/$xscale));
$new_height = round($height * (1/$xscale));
}
/// RESIZE THE IMAGE
$imageResized = imagecreatetruecolor($new_width, $new_height);
$imageTmp = imagecreatefromjpeg ($originalImage);
imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
//// OVERWRITE THE ORIGINAL IMAGE WITH THE RESIZED VERSION
imagejpeg($imageResized, $originalImage, 100) ;
$thumbToPrint = "<img src='members/$id/images/".$newname."'>";
//NOW WRITE THE .txt FILE IN THE APPROPRIATE GALLERY FOLDER
//$myFile = "images/gallery/".$_POST ['gowhere']. "/gallarray.txt";
//$fh = fopen($myFile, 'a+') or die("can't open file");
//$stringData = "members/".$id."/images/".$newname .",";
//fwrite($fh, $stringData);
//fclose($fh);
}
if ($fail = "true"){
$success_msg = '<span class="Big_Orange_Times">Your image has been uploaded, it may take a few moments to appear in your gallery, please be patient.</span>';
} else {
$success_msg = '';
}
} else if ((!empty($_FILES['fileField'])) && ($_FILES['fileField']['error'] == 4)){
//Error code 4: No image uploaded
$error_msg = 'Please browse for an image before you press Go.';
}