Jump to content

Uploading images to multiple folders


unplugged_web

Recommended Posts

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 slashesforeach ($_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 overwrittenfunction 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/' . basename( $_FILES['photo']['name']);$temp_name = $_FILES['photo']['tmp_name'];$type = $_FILES["photo"]["type"];$pic2  = $_FILES['photo']['name'];$target2 = '/uploads/image/filename/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 directorysfit_image_file_to_width($temp_name, 700, $type);fit_image_file_to_width($temp_name2, 100, $type2); //Writes the photo to the serverif(move_uploaded_file($temp_name, $target))if(move_uploaded_file($temp_name2, $target2))

Link to comment
Share on other sites

once you use move_upload_file() to move temporary file to a destination the uploaded file has been moved to its destination. you can copy() the file from moved desstination to a thumbnail folder.

Link to comment
Share on other sites

once you use move_upload_file() to move temporary file to a destination the uploaded file has been moved to its destination. you can copy() the file from moved desstination to a thumbnail folder.
How do I do that, sorry I'm very new to uploading images - this is the first time I've tried to do it
Link to comment
Share on other sites

did you read about copy()? what specific questions do you have or what don't you understand? Do you understand that from the tmp folder you moved the image to another folder, right?Well now, if you want to move the image to another folder, but still want to keep it in the current folder.. you would have to make a copy of it. Just think of it like making copies of files on your computer with an OS. copy, paste, etc.

Link to comment
Share on other sites

did you read about copy()? what specific questions do you have or what don't you understand? Do you understand that from the tmp folder you moved the image to another folder, right?Well now, if you want to move the image to another folder, but still want to keep it in the current folder.. you would have to make a copy of it. Just think of it like making copies of files on your computer with an OS. copy, paste, etc.
I understand what you're saying, but I don't just want to make a copy of the image I want to resize it as well so that it's only 100 pixels wide instead of 700. Would this work:
$style = (isset($_POST['style']) ? implode(' ', $_POST['style']) : '');//create array to temporarily grab variables$input_arr = array();//grabs the $_POST variables and adds slashesforeach ($_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 overwrittenfunction 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/' . basename( $_FILES['photo']['name']);$temp_name = $_FILES['photo']['tmp_name'];$type = $_FILES["photo"]["type"];$pic2  = $_FILES['photo']['name'];$target2 = '/uploads/image/filename/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 directorysfit_image_file_to_width($temp_name, 700, $type);fit_image_file_to_width($temp_name2, 100, $type2); //Writes the photo to the serverif(move_uploaded_file($temp_name, $target))if(copy($temp_name2, $target2))

or am I thinking about it in the wrong way?

Link to comment
Share on other sites

no. $temp_name2 is still the same location as $temp_name, which won't be there when you call move_uploaded_file. think of it logically. personally, I would1) copy the file in tmp.2) then resize the original to 700 (in tmp)3) then resize the copy you made to 100 (in tmp).4) move the 700 to $target5) move the 100 to $target2 there's probably other ways, but that's just one. the only reason i copy before resizing is to not resize from a resized image. this might help maintain quality. also, i don't see the need for an if statement if you have no logic for it, or an else. i.e.

if(move_uploaded_file($temp_name, $target))if(copy($temp_name2, $target2))

Edited by thescientist
Link to comment
Share on other sites

I'm sorry I just don't get it, I tried to follow what you said, but just got a whole heap of errors:

Warning: getimagesize(/tmp/php7FVaT0) [function.getimagesize]: failed to open stream: No such file or directory in /new_site/admin/portfolio_image_added.php on line 14 Warning: Division by zero in /new_site/admin/portfolio_image_added.php on line 16 Warning: imagecreatefromjpeg(/tmp/php7FVaT0) [function.imagecreatefromjpeg]: failed to open stream: No such file or directory in //new_site/admin/portfolio_image_added.php on line 20 Warning: imagecreatetruecolor() [function.imagecreatetruecolor]: Invalid image dimensions in /new_site/admin/portfolio_image_added.php on line 33 Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /new_site/admin/portfolio_image_added.php on line 34 Warning: imagejpeg(): supplied argument is not a valid Image resource in /new_site/admin/portfolio_image_added.php on line 38 Warning: imagedestroy(): supplied argument is not a valid Image resource in /new_site/admin/portfolio_image_added.php on line 53
I've changed the code to this:
// resizes an image to fit a given width in pixels.// works with BMP, PNG, JPEG, and GIF// $file is overwrittenfunction 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/' . basename( $_FILES['photo']['name']);$temp_name = $_FILES['photo']['tmp_name'];$type = $_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 directorysfit_image_file_to_width($temp_name, 700, $type); //Writes the photo to the serverif(move_uploaded_file($temp_name, $target))$pic2  = $_FILES['photo']['name'];$target2 = '/uploads/image/filename/thumb/thumbnailbig/' . basename( $_FILES['photo']['name']);$temp_name2 = $_FILES['photo']['tmp_name'];$type2 = $_FILES["photo"]["type"];fit_image_file_to_width($temp_name2, 100, $type2);if(move_uploaded_file($temp_name2, $target2))

Link to comment
Share on other sites

You're trying to work on the temp file after moving it. Moving does exactly what it says, it moves the file. That means it isn't in the place it was moved from after it is moved, it is in the destination. Moving is not copying, it does not create a copy of the file. So you're resizing the file, then moving it, then trying to resize the original file again but it's not there any more because you just moved it. That's why he suggested creating another copy of the temp file first, then resizing and moving each temp file to the appropriate destination.

Link to comment
Share on other sites

You're trying to work on the temp file after moving it. Moving does exactly what it says, it moves the file. That means it isn't in the place it was moved from after it is moved, it is in the destination. Moving is not copying, it does not create a copy of the file. So you're resizing the file, then moving it, then trying to resize the original file again but it's not there any more because you just moved it. That's why he suggested creating another copy of the temp file first, then resizing and moving each temp file to the appropriate destination.
That's what I thought I was doing, I thought the last bit
if(move_uploaded_file($temp_name, $target))

was what was moving it?

Link to comment
Share on other sites

The move_uploaded_file function moves an uploaded file, hence the name. Once you use that function the original file no longer exists. You need to use the copy function to copy the file before resizing and moving it. The copied file will need a different name. You copy the file, resize and move the first one, then resize and move the second (copied) one. http://www.php.net/m...nction.copy.php

Link to comment
Share on other sites

if you don't know what your code is doing it will be hard to fix it.

Link to comment
Share on other sites

I'm sorry I really don't understand where or how to cop it. I didn't write the original code myself and I don't know what bit does what.
I provided you some steps to follow, but it does not seem like you followed it. I would just try again and try re-read my steps. The critical points to understand is that once you MOVE a file, it has been moved, hence why I suggested making a COPY of it first. Then you can resize/move each one as you see fit. We're only talking about a couple of lines of additional code here. Just take it from the beginning and really think about each line of code. Ask yourself, what is this doing? Does it make sense for what I am trying to do? Edited by thescientist
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...