473,396 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Image Upload, Resize, Rename and create record in mysql

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.

Nov 25 '06 #1
3 6693
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($path, $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($rootName)) {
preg_match('/^(.*?)(\.[^.]*)?$/', $baseName, $matches);
$rootName = $matches[1];
$dotExt = count($matches) 2 ? $matches[2] : '';
}
$newPath = "$dir/$rootName-" . uniqid(mt_rand(), true) .
$dotExt;
}

if (file_exists($newPath)) {
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($newPath)) {
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($path, $dir);
if ($newPath === false) {
echo "Failed to add file.";
} else {
echo "File was moved to \"$newPath\".";
}
}

error_reporting(E_ALL);
testIt();

?>
gs**********@gmail.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.
Nov 26 '06 #2
That was quite a helpful post. I just had a suggestion for your error
handling in the addFileToDir function. You don't specify the error type
for the trigger_error function. Its default is E_USER_NOTICE, which
allows execution to continue. You should change it to E_USER_ERROR, so
execution stops. If one error occurs, in this case, there's no point in
continuing.

Curtis

On Nov 25, 6:35 pm, "petersprc" <peters...@gmail.comwrote:
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($path, $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($rootName)) {
preg_match('/^(.*?)(\.[^.]*)?$/', $baseName, $matches);
$rootName = $matches[1];
$dotExt = count($matches) 2 ? $matches[2] : '';
}
$newPath = "$dir/$rootName-" . uniqid(mt_rand(), true) .
$dotExt;
}

if (file_exists($newPath)) {
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($newPath)) {
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($path, $dir);
if ($newPath === false) {
echo "Failed to add file.";
} else {
echo "File was moved to \"$newPath\".";
}

}error_reporting(E_ALL);
testIt();

?>

gsoguerri...@gmail.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.
Nov 26 '06 #3
Yeah that's a great point. The function should fall-through after an
error, but they are E_USER_ERRORs...

dyer85 wrote:
That was quite a helpful post. I just had a suggestion for your error
handling in the addFileToDir function. You don't specify the error type
for the trigger_error function. Its default is E_USER_NOTICE, which
allows execution to continue. You should change it to E_USER_ERROR, so
execution stops. If one error occurs, in this case, there's no point in
continuing.

Curtis

On Nov 25, 6:35 pm, "petersprc" <peters...@gmail.comwrote:
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($path, $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($rootName)) {
preg_match('/^(.*?)(\.[^.]*)?$/', $baseName, $matches);
$rootName = $matches[1];
$dotExt = count($matches) 2 ? $matches[2] : '';
}
$newPath = "$dir/$rootName-" . uniqid(mt_rand(), true) .
$dotExt;
}

if (file_exists($newPath)) {
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($newPath)) {
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($path, $dir);
if ($newPath === false) {
echo "Failed to add file.";
} else {
echo "File was moved to \"$newPath\".";
}

}error_reporting(E_ALL);
testIt();

?>

gsoguerri...@gmail.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.
Nov 30 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: dave | last post by:
Hello there, I am at my wit's end ! I have used the following script succesfully to upload an image to my web space. But what I really want to be able to do is to update an existing record in a...
4
by: DH | last post by:
I have a "file upload form" that works OK, but I have been unsuccessful in my attempt to also resize the uploaded .JPG (if it is too wide), over-writing the original .JPG, and then create and save...
0
by: Jay | last post by:
Hi guys, trying to fix a problem with an image resize routine. The code posted below uploads and resizes a jpeg, probablem is, the outlook can look a bit bitty becuase of teh samller size...is...
15
by: David Lozzi | last post by:
Howdy, I have a function that uploads an image and that works great. I love ..Nets built in upload, so much easier than 3rd party uploaders! Now I am making a public function that will take the...
0
by: Mattia | last post by:
************************************************** Manage image without exhausted memory ************************************************** Hi; I have a big problem. I must create a script that...
2
by: Poppa Pimp | last post by:
ImageResizer.php Image Resizer PLEASE HELP The URL of the page this is on in my site is http://poppa-pimps-wallpapers.com//ImageResizer.php You can click on browse and get image,but...
3
by: Shawn Northrop | last post by:
I have created a site where users can upload images to a mysql server and then the images are displayed on another page. If possible i would like to resize the image before the data uploads. for...
7
by: mishrarajesh44 | last post by:
hii all Truly telling i hav got this code from net & i am finding error while running the code below.. code:- <?php $idir = "photo/"; // Path To Images Directory $tdir =...
3
by: neovantage | last post by:
Hey all, i have created image by mixing logo and the original image. It create image and show in the same window. I have done 3 steps for this. First i upload an image to my server by using...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.