Below is a script that saves a user’s pic to MySQL and to my file system. It also creates a thumbnail and does the same for it. The original size pic gets unlinked and updated flawlessly every time.
Problem is that the thumbnail ‘unlink’ and ‘update’ is not functioning as well as the other. I upload a pic, it creates and saves it fine 1st time. Then I go to upload a 2nd time, and it unlinks the thumb altogether(no replace). Then on the 3rd attempt it works fine(with warning bout no such file or directory). The 4th attempt unlinks the thumb altogether again. And it goes on in that cycle.
I hope somebody can take a sec to find where the problem is in the code. I’ve been toying with it for two days now and cannot fix it.
Thanks for your time and knowledge.
[code=php]<?php
session_start();
header(“Cache-control: private”);
include(“db_connect.php”);
function create_resize_save_jpeg( $path, $newpath, $max = 150)
{
$w = $h = $max;
list( $width_orig, $height_orig ) = getimagesize( $path );
if($width_orig < $height_orig) $w = round(($h / $height_orig) * $width_orig);
else $h = round(($w / $width_orig) * $height_orig);
$image_p = imagecreatetruecolor($w, $h);
if( !$image = imagecreatefromjpeg( $path ) ) {
$_SESSION[‘errormsg’] = “Cant create photo!”;
} else {
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $w, $h, $width_orig, $height_orig);
imagejpeg($image_p, $newpath, 100);
}
imagedestroy($image_p);
imagedestroy($image);
}
function create_resize_save_gif( $path, $newpath, $max = 150)
{
$w = $h = $max;
list( $width_orig, $height_orig ) = getimagesize( $path );
if($width_orig < $height_orig) $w = round(($h / $height_orig) * $width_orig);
else $h = round(($w / $width_orig) * $height_orig);
$image_p = imagecreatetruecolor($w, $h);
if( !$image = imagecreatefromgif( $path ) ) {
$_SESSION[‘errormsg’] = “Cant create photo!”;
} else {
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $w, $h, $width_orig, $height_orig);
imagegif($image_p, $newpath, 100);
}
imagedestroy($image_p);
imagedestroy($image);
}
function create_resize_save_png( $path, $newpath, $max = 150)
{
$w = $h = $max;
list( $width_orig, $height_orig ) = getimagesize( $path );
if($width_orig < $height_orig) $w = round(($h / $height_orig) * $width_orig);
else $h = round(($w / $width_orig) * $height_orig);
$image_p = imagecreatetruecolor($w, $h);
if( !$image = imagecreatefrompng( $path ) ) {
$_SESSION[‘errormsg’] = “Cant create photo!”;
} else {
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $w, $h, $width_orig, $height_orig);
imagepng($image_p, $newpath, 100);
}
imagedestroy($image_p);
imagedestroy($image);
}
function file_extention( $str ) {
$size = strlen($str);
$x=0;
while($x < $size)
{
if($str[$x] == ‘.’)
{
$ext = “”;
$ext_len = ($size – $x);
$y=$x;
while($y < $size)
{
$ext .= $str[$y];
$y++;
}
}
$x++;
}
return $ext;
}
if( !$_SESSION[‘id’] ) {
echo “You’re not logged in!”;
include(“doh.html”);
exit();
}
$id = $_SESSION[‘id’];
$maxfilesize = 81920;
// check if there was a file uploaded
if( !is_uploaded_file( $_FILES[‘userphoto’][‘tmp_name’] ) ) {
$error = “you didn’t select a file to upload.<br />”;
} else {
if( $_FILES[‘userphoto’][‘size’] > $maxfilesize ) {
$error = “your image file was too large.<br />”;
unlink($_FILES[‘userphoto’][‘tmp_name’]);
} else {
if( !preg_match(“/.(gif|jpg|png)$/i”, $_FILES[‘userphoto’][‘name’] ) ) {
$error = “your file was an unacceptable type.<br />”;
unlink($_FILES[‘userphoto’][‘tmp_name’]);
// if it’s there, an okay size and type, copy to server and update the photo value in SQL
} else {
$ext = file_extention( $_FILES[‘userphoto’][‘name’] );
if( $_SESSION[‘pic_1’] != “nopic.jpg” && $_SESSION[‘pic_1’]!=””) {
unlink(“user_files/picture1/”.$_SESSION[‘pic_1’]);
}
if( $_SESSION[‘thumb_1’] != “nopic.jpg” && $_SESSION[‘thumb_1’]!=””) {
unlink(“user_files/thumbnail1/”.$_SESSION[‘thumb_1’]);
}
$newname = $_SESSION[‘id’].$_FILES[‘userphoto’][‘name’];
if( !move_uploaded_file( $_FILES[‘userphoto’][‘tmp_name’], “user_files/picture1/”.$newname ) ) {
$error = “problems moving uploaded file.<br />”;
}
else {
$_SESSION[‘pic_1’] = $newname;
if( $ext == “.jpg” ) {
create_resize_save_jpeg(“user_files/picture1/”.$newname, “user_files/thumbnail1/”.$newname);
} else if( $ext == “.gif” ) {
create_resize_save_gif(“user_files/picture1/”.$newname, “user_files/thumbnail1/”.$newname);
} else if( $ext == “.png” ) {
create_resize_save_png(“user_files/picture1/”.$newname, “user_files/thumbnail1/”.$newname);
}
mysql_query(“UPDATE users SET pic_1=’$newname’ WHERE id=’$id'”) or die (mysql_error());
$_SESSION[‘pic_1’] = $newname;
mysql_query(“UPDATE users SET thumb_1=’$newname’ WHERE id=’$id'”) or die (mysql_error());
$_SESSION[‘thumb_1’] = $newname;
}
}
}
}
?>
<html><head><title>Change Photo Result</title></head><body>
<h1>Change Photo Result</h1>
<?php
if ($error) {
echo “Your photo could not be changed because “.$error.”.”;
} else {
echo “Your photo was successfully uploaded. To view your updated profile, <a href=”changephoto.php”>click here</a>.”;
}
?></body></html>