Copy link to clipboard
Copied
I'm uploading an image, but I want it to go to two different folders, one folder the image will be the regular size which is 700 pixels wide and the other folder will just be a thumbnail.
I can get it to add the image to the database as well as upload it to the first folder, but I can't get it to upload to the thumbnail folder too.
This is what I've got, but I'm site sure where it's going wrong:
$style = (isset($_POST['style']) ? implode(' ', $_POST['style']) : '');
//create array to temporarily grab variables
$input_arr = array();
//grabs the $_POST variables and adds slashes
foreach ($_POST as $key => $input_arr) {
$_POST[$key] = addslashes($input_arr);
}
// resizes an image to fit a given width in pixels.
// works with BMP, PNG, JPEG, and GIF
// $file is overwritten
function fit_image_file_to_width($file, $w, $mime = 'image/jpeg') {
list($width, $height) = getimagesize($file);
$newwidth = $w;
$newheight = $w * $height / $width;
switch ($mime) {
case 'image/jpeg':
$src = imagecreatefromjpeg($file);
break;
case 'image/png';
$src = imagecreatefrompng($file);
break;
case 'image/bmp';
$src = imagecreatefromwbmp($file);
break;
case 'image/gif';
$src = imagecreatefromgif($file);
break;
}
$dst = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
switch ($mime) {
case 'image/jpeg':
imagejpeg($dst, $file);
break;
case 'image/png';
imagealphablending($dst, false);
imagesavealpha($dst, true);
imagepng($dst, $file);
break;
case 'image/bmp';
imagewbmp($dst, $file);
break;
case 'image/gif';
imagegif($dst, $file);
break;
}
imagedestroy($dst);
}
// init file vars
$pic = $_FILES['photo']['name'];
$target = '/uploads/image/filename/thumb' . basename( $_FILES['photo']['name']);
$temp_name = $_FILES['photo']['tmp_name'];
$type = $_FILES["photo"]["type"];
$pic2 = $_FILES['photo']['name'];
$target2 = '/uploads/image/filename/thumb/thumbnailbig/' . basename( $_FILES['photo']['name']);
$temp_name2 = $_FILES['photo']['tmp_name'];
$type2 = $_FILES["photo"]["type"];
// Connects to your Database
mysql_connect($host,$username,$password) or die(mysql_error()) ;
mysql_select_db($database) or die(mysql_error()) ;
// get form data
$class = $_POST['class'];
$foreign_id = $_POST['foreign_id'];
$name = mysql_real_escape_string(isset($_POST['name']) ? $_POST['name'] : 'No name');
$order = $_POST['order'];
//Writes the information to the database
mysql_query("INSERT INTO `images` (`id`, `class`, `foreign_id`, `title`, `filename`, `created`, `modified`, `order`, `category`) VALUES (NULL, '$_POST[class]', '$_POST[foreign_id]', '$_POST[name]', '$pic', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, '$_POST[order]', '$style')");
// resize the image in the tmp directorys
fit_image_file_to_width($temp_name, 700, $type);
fit_image_file_to_width($temp_name2, 100, $type2);
//Writes the photo to the server
if(move_uploaded_file($temp_name, $target))
if(move_uploaded_file($temp_name2, $target2))
Copy link to clipboard
Copied
I think the problem is that you're assigning the same image to two variables, and then passing those variables to the fit_image_file_to_width() function, which overwrites the original image. As far as I know, the $_FILES array keeps only a reference to the file. So, when you assign the image to a second variable, you're assigning a reference to the value in the $_FILES array rather than cloning the image. That's why the first time works, but not the second.
To overcome this problem, you need to move the original image to a temporary location on your server, make a copy of it, and then pass each copy to the fit_image_file_to_width() function.