pictureupload / uploadname needs to differ everytime
Posted: Thu Jun 12, 2008 8:48 am
Hi guys,
I have a code which uploads pictures to a directory on my Server. Here is the code:
$filename_new = 'new_picture.jpg';
The part in red needs to be changed. As it is now, a picture that would be uploaded would get that name. However, if I upload another picture it would have the same name and hence be overwritten. So, how can the picture be uploaded so it has a different name everytime.
I thought about a text field so that the user can input a filename and if it is already taken that he had to chose a different name. But I really can't do that and it would take you guys to long either.
So, I thought about a loop. What about if the pictures are named 1.jpg, 2.jpg etc... But I don't know how o do that either because you'd have to check if there is already one picture of that name on the server.
Please help me. Thanks a lot in advance.
I have a code which uploads pictures to a directory on my Server. Here is the code:
Code: Select all
<?php
// Is it a jpg picture?
if ((($_FILES['meinbild']['type'] == 'image/pjpeg') || ($_FILES['meinbild']['type'] == 'image/jpeg')) && ($_FILES['meinbild']['error'] == 0) && ($_FILES['meinbild']['tmp_name'] != none) && ($_FILES['meinbild']['name']) && ($_FILES['meinbild']['size'] > 0)) {
$filename_old = $_FILES['meinbild']['name'];
$filename_new = 'new_picture.jpg';
$savepath = "images/$ID_user/";
$size = getimagesize($_FILES['meinbild']['tmp_name']);
$width_old = $size[0];
$height_old = $size[1];
// new format
if ($width_old > $height_old) {
$width_new = '200';
$height_new = intval($height_old * $width_new / $width_old);
} else {
$height_new = '200';
$width_new = intval($width_old * $height_new / $height_old);
}
// processing
$picture_old = imagecreatefromjpeg($_FILES['meinbild']['tmp_name']);
$picture_new = imagecreatetruecolor($width_new, $height_new);
imagecopyresampled($picture_new, $picture_old, 0, 0, 0, 0, $width_new, $height_new, $width_old, $height_old);
// save
$create = imagejpeg($picture_new, $savepath.$filename_new);
$create_old = imagejpeg($picture_old, $savepath.$filename_old);
imagedestroy($picture_new);
imagedestroy($picture_old);
if ($create && $create_old) {
echo '<p>picture uploaded view: <a href="'.$savepath.$filename_new.'">here</a> .</p>'."\n";
}
} else {
echo '<p>not successfull</p>'."\n";
}
?>
The part in red needs to be changed. As it is now, a picture that would be uploaded would get that name. However, if I upload another picture it would have the same name and hence be overwritten. So, how can the picture be uploaded so it has a different name everytime.
I thought about a text field so that the user can input a filename and if it is already taken that he had to chose a different name. But I really can't do that and it would take you guys to long either.
So, I thought about a loop. What about if the pictures are named 1.jpg, 2.jpg etc... But I don't know how o do that either because you'd have to check if there is already one picture of that name on the server.
Please help me. Thanks a lot in advance.