473,782 Members | 2,534 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6737
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.
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...@gmai l.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($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_reportin g(E_ALL);
testIt();

?>

gsoguerri...@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.
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...@gmai l.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($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_reportin g(E_ALL);
testIt();

?>

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

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

Similar topics

3
11764
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 table in MySQL with the path & filename to the image. I have successfully uploaded and performed an update query on the database, but the problem I have is I cannot retain the primary key field in a variable which is then used in a SQL update...
4
2457
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 a thumbnail.jpg .... all at the same time. Links to a working example would be appreciated. Thanks.
0
2326
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 there a way I can anti alias / smooth the resized image before saving? If myThumb.ContentLength > 0 Then Dim myThumbData(myThumb.ContentLength) As Byte myThumb.InputStream.Read(myThumbData, 0, myThumb.ContentLength)
15
5366
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 path of the uploaded image, and resize it with the provided dimensions. My function is below. The current function is returning an error when run from the upload function: A generic error occurred in GDI+. Not sure what exactly that means. From what...
0
1914
by: Mattia | last post by:
************************************************** Manage image without exhausted memory ************************************************** Hi; I have a big problem. I must create a script that upload an image an then resize it, if width or height are more than 250px. Now, after upload an image (in this example I suppose that it's a JPEG image):
2
2366
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 when you upload image it will go to another page and says ]((unable to create emp directory)) Here is a site to be able to see script actually work http://tech.tailoredweb.com/image-editor-52/ and can be DL from there also. I am using FP 2003 and...
3
1264
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 example if the image is 1024 x 2000 and 2mb. It would resize the image to 400 x 600 and 200kb. or something of the sorts. then it would upload that data and store it in my database. As of now my script uploads, stores, and displays the image. However if...
7
17058
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 = "photo/thumbs/"; // Path To Thumbnails Directory $twidth = "125"; // Maximum Width For Thumbnail Images
3
3106
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 move_upload_file, then i resize it using class "thumbnail.class.php" My next step was to embed company logo in it(which i already done) and save new embedded logo image to the path where i first upload the original image i mean i want to overwrite...
0
9641
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10313
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10146
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7494
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6735
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5378
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4044
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3643
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.