Hi,
Some links you might use:
phpThumb image resizing library:
http://phpthumb.sourceforge.net
Image manipulation code samples:
http://www.zend.com/codex.php?CID=344
Some mysql insert examples with PDO:
http://wiki.cc/php/PDO_Basics#Insert...und_parameters
Here's a function which can be used to add an uploaded file to your
image directory:
<?
// addFileToDir: Move a file to a directory and rename
// the file if necessary to avoid clobbering any existing
// file with the same name.
function addFileToDir($p ath, $dir, $useRename = true)
{
$ok = false;
$baseName = basename($path) ;
$tries = 4;
for ($i = 0; $i < $tries; $i++) {
if ($i == 0) {
$newPath = $dir . '/' . $baseName;
} else {
if (!isset($rootNa me)) {
preg_match('/^(.*?)(\.[^.]*)?$/', $baseName, $matches);
$rootName = $matches[1];
$dotExt = count($matches) 2 ? $matches[2] : '';
}
$newPath = "$dir/$rootName-" . uniqid(mt_rand( ), true) .
$dotExt;
}
if (file_exists($n ewPath)) {
if ($i == $tries - 1) {
trigger_error(" Failed to get unique destination path " .
"while adding file \"$path\" to directory \"$dir\".");
}
$file = false;
} elseif ($i < $tries - 1) {
$file = @fopen($newPath , 'x');
} elseif (!($file = fopen($newPath, 'x'))) {
trigger_error(" Failed to open destination path \"$newPath\" " .
"while moving file \"$path\".") ;
}
if ($file) {
if (!fclose($file) ) {
trigger_error(" Failed to close file \"$newPath\".") ;
} else {
if ($useRename) {
if (!rename($path, $newPath)) {
trigger_error(" Failed to move file \"$path\" to " .
"\"$newPath\"." );
} else {
$ok = true;
}
} elseif (!copy($path, $newPath)) {
trigger_error(" Failed to copy file \"$path\" to " .
"\"$newPath\"." );
} elseif (!unlink($path) ) {
trigger_error(" Failed to delete file \"$path\".") ;
} else {
$ok = true;
}
}
if (!$ok && !unlink($newPat h)) {
trigger_error(" Failed to delete file \"$newPath\".") ;
}
break;
}
}
return $ok ? $newPath : false;
}
function testIt()
{
$path = dirname(__FILE_ _) . '/test.jpg';
$dir = dirname(__FILE_ _) . '/images';
$newPath = addFileToDir($p ath, $dir);
if ($newPath === false) {
echo "Failed to add file.";
} else {
echo "File was moved to \"$newPath\" .";
}
}
error_reporting (E_ALL);
testIt();
?>
gs**********@gm ail.com wrote:
Hi,
I have limited knowledge in php and I am having trouble with uploading
an image to a remote directory and resizing it if it's larger and
renaming it to a unique id, while at the same time I would like to
create a record in mysql database. I've tried to find some tutorials
but I've had trouble finding any good ones. Wondering if anybody has
good examples or link to a good tutorial.
Thanks in advance.