I'm having some problems with resizing and uploading an image. The normal upload works fine, the image is uploaded and the entry for it is inserted into the database, but when the checkbox on the form is checked to resize the image, the entry is inserted but the image is nowhere to be found.
if (isset($_POST['addmood'])) {
$moods = str_replace(" ", "_", $_POST['moods']);
$size = $_POST['size'];
$description = $_POST['description'];
if ($size == "") { echo '<p class="warning">Error: You must enter a size</p>'; die(); }
if ($description == "") { echo '<p class="warning">Error: You must enter a description</p>'; die(); }
if ($moods == "") { echo '<p class="warning">Error: You must enter a name</p>'; die(); }
if ($_POST['filename'] != "") {
$filename = $_POST['filename'];
} elseif ($_FILES['imgfile'] != "") {
if(isset($_POST['overwrite'])) { $overwrite = "yes"; } else { $overwrite = "no"; }
$allowed_ext = "jpg,gif,png,jpeg";
$folder = "images/";
chmod($folder, 0777);
$match = ""; // For security purposes
$filesize = $_FILES['imgfile']['size'];
$filename = strtolower($_FILES['imgfile']['name']);
$filename = str_replace(" ", "_", $filename);
$tempname = $_FILES['imgfile']['tmp_name'];
if(!$filename || $filename=="") {
$error = "- No file selected for upload.<br>";
} elseif (file_exists($folder.$filename) && $overwrite=="no") {
$error = "File already exists: ".$filename."<br>";
}
if($filesize < 1){
$error .= "File is empty.<br>";
} elseif ($filesize > 1000000) {
$error .= "File size is too big.<br>";
}
$file_ext = preg_split("/./",$filename);
if(($file_ext[1] == "png") || ($file_ext[1] == "gif") || ($file_ext[1] == "jpeg") || ($file_ext[1] == "jpg")) {
$match = "1";
}
if(!isset($match)){
$error .= "File type isn't allowed: ".$filename."<br>";
}
if(isset($error)) {
echo "Error trying to upload file:<br>" . $error;
} else {
if(isset($_POST['resizesmiley'])) {
$newname = $folder.'small_'.$filename;
if ($_FILES['imgfile']['type'] == "image/gif"){
$im = ImageCreateFromGIF($tempname);
$width = ImageSx($im);
$height = ImageSy($im);
$aspect = $width / 20;
$n_width = 20;
$n_height = $height / $aspect;
$newimage = imagecreatetruecolor($n_width,$n_height);
imageCopyResized($newimage,$im,0,0,0,0,20,20,$width,$height);
// imageCopyResized($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);
if (function_exists("imagegif")) {
Header("Content-type: image/gif");
ImageGIF($newimage,$newname);
} elseif (function_exists("imagepng")) {
Header("Content-type: image/png");
ImagePNG($newimage,$newname);
} elseif (function_exists("imagejpeg")) {
Header("Content-type: image/jpeg");
ImageJPEG($newimage,$newname);
}
}
if($_FILES['imgfile']['type'] == "image/pjpeg"){
$im = ImageCreateFromJPEG($tempname);
$newimage = imagecreatetruecolor($n_width,$n_height);
imageCopyResized($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);
ImageJpeg($newimage,$newname);
}
$filename = $sigfilesurl . $newname;
} else {
if(move_uploaded_file($tempname, $folder.$filename)) { $filename = $smileypath.$filename; }
else { echo "Error! File size might exceed upload limit of server. Try again."; die(); }
}
}
}
elseif (($_FILES['imgfile'] == "") && ($_POST['filename'] == "")) { echo '<p class="warning">Error: You must enter a file to upload</p>'; die(); }
$sql = "INSERT INTO mood (moods,filename,size,description) VALUES ('$moods','$filename','$size','$description')";
mysql_query($sql);
echo "<p>Added mood: " . $description . "</p>";
?>
<p align="center">Reloading in 2 seconds...</p><script language="javascript" type="text/javascript">setTimeout("location.href='sigadmin.php'",2000)</script></body></html>
<?php
mysql_close();
die();
}
The problem isn't with permissions because the standard upload works, it's just the resize causing probs.