TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 10-16-2008, 01:39 PM   #1 (permalink)
The Contributor
 
Join Date: Sep 2008
Posts: 39
Thanks: 9
code_junkie is on a distinguished road
Default Image Uploading

I am trying to make an image upload script. This script needs to upload the image to a specific folder within the site, then write its info to mysql. I've searched and searched, done alot of reading however almost everything I've looked at is different from each other. Can anyone help me in the right direction with finding the correct way of doing this? I'm still trying to understand php and this project has confused the hell out of me. Any help will be greatly appreciated.
__________________
Trying to learn all I can about PHP. Teach me what you know...
code_junkie is offline  
Reply With Quote
Old 10-16-2008, 02:34 PM   #2 (permalink)
The Wanderer
 
Join Date: Oct 2008
Posts: 14
Thanks: 0
Lou_Gato is on a distinguished road
Default

I can use this too. I hope someone offers you some info soon.!!!
Lou_Gato is offline  
Reply With Quote
Old 10-16-2008, 03:32 PM   #3 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Here you go guys. A nice simplified example for you both. You don't have to check the extension, but it's most certainly advisable, so I've added it in for you. Simply add the extensions to the array and voila!

Any explanations needed, just ask ! Ensure you create the "uploads" folder, else the script will fail -- gracefully, of course.
Attached Files
File Type: php Image-Upload.php (1.4 KB, 198 views)
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
The Following User Says Thank You to Wildhoney For This Useful Post:
code_junkie (10-16-2008)
Old 10-16-2008, 03:52 PM   #4 (permalink)
The Contributor
 
Join Date: Sep 2008
Posts: 39
Thanks: 9
code_junkie is on a distinguished road
Default

I have an error from the code you provided.
Error as follows:
Fatal error: Uncaught exception 'Exception' with message 'Image "8-28-2008 001.jpg" could not be uploaded.' in /var/www/vhosts/domain.com/httpdocs/admin/image_handler.php:31 Stack trace: #0 {main} thrown in /var/www/vhosts/domain.com/httpdocs/admin/image_handler.php on line 31
__________________
Trying to learn all I can about PHP. Teach me what you know...
code_junkie is offline  
Reply With Quote
Old 10-16-2008, 04:12 PM   #5 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

check the permissions of the uploads folder
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
The Following User Says Thank You to sketchMedia For This Useful Post:
code_junkie (10-16-2008)
Old 10-16-2008, 05:08 PM   #6 (permalink)
The Contributor
 
Join Date: Sep 2008
Posts: 39
Thanks: 9
code_junkie is on a distinguished road
Default

Permission are 777 and still have the error.
__________________
Trying to learn all I can about PHP. Teach me what you know...
code_junkie is offline  
Reply With Quote
Old 10-16-2008, 05:35 PM   #7 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Remove the "at" sign (@) from the front of move_uploaded_file. That way you'll be able to see the raw PHP error of what's happening. That should tell you a bit more than my custom exception.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
The Following User Says Thank You to Wildhoney For This Useful Post:
code_junkie (10-16-2008)
Old 10-16-2008, 05:50 PM   #8 (permalink)
The Contributor
 
Join Date: Sep 2008
Posts: 39
Thanks: 9
code_junkie is on a distinguished road
Default

I took the @ out and made one small change and it worked. Small error on my part, needed to change path out of one folder to go into the uploads folder. Thanks alot for that script. Now I need to get working on making it the info to the db. Thanks again...
__________________
Trying to learn all I can about PHP. Teach me what you know...
code_junkie is offline  
Reply With Quote
Old 10-16-2008, 06:11 PM   #9 (permalink)
The Contributor
 
Join Date: Sep 2008
Posts: 39
Thanks: 9
code_junkie is on a distinguished road
Default

Ok, I was hoping I wouldn't have to do this. When the script runs, it uploads then writes to the table... Problem is when it writes the name of the image, its writing it as Object id #1. Any thoughts?
__________________
Trying to learn all I can about PHP. Teach me what you know...
code_junkie is offline  
Reply With Quote
Old 10-16-2008, 06:20 PM   #10 (permalink)
The Contributor
 
Join Date: Sep 2008
Posts: 39
Thanks: 9
code_junkie is on a distinguished road
Default

Spoke to soon and didn't stare at it enough. Got it working now. Thanks for everything
__________________
Trying to learn all I can about PHP. Teach me what you know...
code_junkie is offline  
Reply With Quote
Old 10-16-2008, 07:14 PM   #11 (permalink)
The Contributor
 
Join Date: Sep 2008
Posts: 39
Thanks: 9
code_junkie is on a distinguished road
Default

I got a quick question for you Wildhoney. My fried had a resize script laying around but does know how to use it. How would I implement it into the script you gave me?
PHP Code:
<?php
  $source_pic 
'images/source.jpg';
  
$destination_pic 'images/destination.jpg';
  
$max_width 200;
  
$max_height 200;
  
$src imagecreatefromjpeg($source_pic);
  list(
$width,$height) = getimagesize($source_pic);
  
$x_ratio $max_width/$width;
  
$y_ration $max_height/$height;
  
  if (
$width <= $max_width) && ($height <= $max_height)
    {
      
$tn_width $width;
      
$tn_height $height;
    }
    
  else if ((
$x_ratio*$height) < $max_height))
    {
      
$tn_height ceil($x_ratio*$height);
      
$tn_width=$max_width;
    }

  else
    {
      
$tn_height ceil($y_ration*$width);
      
$tn_height $max_height;
    }
    
  
$tmp imgcreatetruecolor ($tn_width,$tn_height);
  
imagecopyresample($tmp,$src,0,0,0,0,$tn_width,$tn_height,$width,$height);
  
imagejpeg($tmp,$destination_pic,100);
  
imagedestroy($src);
  
imagedestroy($tmp);
?>
__________________
Trying to learn all I can about PHP. Teach me what you know...
code_junkie is offline  
Reply With Quote
Old 10-17-2008, 01:07 AM   #12 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

You would drop it into a function. However, you may use my function in which I use to resize the images. I'm not saying mine is better, as it's been a while since I wrote it, but I know how it works !

You would call it like so:

php Code:
image_resize('left_2405181707586.jpg', 'left_2405181707586-resized.jpg', 400, 400);

The function:

php Code:
function image_resize($szSource, $szDestination, $iResizeWidth, $iResizeHeight)
{
    declare(ticks = 1)
    {
        $szExtension = pathinfo($szSource, PATHINFO_EXTENSION);
       
       switch($szExtension)
       {
          case('jpg'):   $pSource = imagecreatefromjpeg($szSource); break;
          case('jpeg'):  $pSource = imagecreatefromjpeg($szSource); break;
          case('gif'):   $pSource = imagecreatefromgif($szSource); break;
          case('wbmp'):  $pSource = imagecreatefromwbmp($szSource); break;
          case('png'):   $pSource = imagecreatefrompng($szSource); break;
       }
       
       $pResized = imagecreatetruecolor($iResizeWidth, $iResizeHeight);
       list($iWidth, $iHeight) = getimagesize($szSource);
       
       if(!imagecopyresampled($pResized, $pSource, 0, 0, 0, 0, $iResizeWidth, $iResizeHeight, $iWidth, $iHeight))
       {
          return false
       }

        switch($szExtension)
        {
          case('jpg'):   $pResult = imagejpeg($pResized, $szDestination, 100); break;
          case('jpeg'):  $pResult = imagejpeg($pResized, $szDestination, 100); break;
          case('gif'):   $pResult = imagegif($pResized, $szDestination); break;
          case('wbmp'):  $pResult = imagewbmp($pResized, $szDestination); break;
          case('png'):   $pResult = imagepng($pResized, $szDestination); break;
       }
       
        if($pResult)
        {
            return true;
        }
           
        return false;
    }
}
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 03-16-2009, 05:20 PM   #13 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Hello, I am having an issue with this script. I have not changed the code at all. I simply uploaded it and I got a parsing error here:

Parse error: parse error in /usr/local/apache/upload.html on line 17

I have my apache parsing HTML as PHP so I don't think that is the issue since the rest of my site works with HTML extension.

Just to make sure I renamed it back to .php and still got the error.

This is line #17:

Code:
/* Ensure the extension we're upload is allowed. */
	if (!in_array($szExtension, $aAllowedExtensions))
	{
		throw new Exception('Extension "' . $szExtension . '" has been disallowed.');
	}
It seems to be this line exactly:

throw new Exception('Extension "' . $szExtension . '" has been disallowed.');


Any idea's?
allworknoplay is offline  
Reply With Quote
Old 03-16-2009, 09:52 PM   #14 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Also one more question, should the UPLOAD directory be in a publicly accessible directory or in a private directory?

I'm not sure what the security concerns are?
allworknoplay is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 05:22 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design