PHP Coding
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsPHP RelatedPHP Coding

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
  #1  
Old September 23rd, 2009, 03:16 PM
herghost herghost is offline
Registered User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Sep 2009
Posts: 1 herghost User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 m 47 sec
Reputation Power: 0
Image upload help

Hi Everyone!

1st post here and I am hoping you can help.

I have this:
Code:
<?php   
include('../include/dbconnect.php');
include('../include/auth.inc.php'); $redirect =  ('../index.php');
$query = 'SELECT user_id, username FROM users_credits WHERE username = "' . mysql_real_escape_string($_SESSION['username'], $conn) . '"';          $result = mysql_query($query, $conn) or die(mysql_error($conn));  $row = mysql_fetch_array($result); extract($row); mysql_free_result($result);   // get data that sent from form $cat=$_POST['cat']; $os=$_POST['os']; $ram=$_POST['ram']; $graphics=$_POST['graphics']; $harddrive=$_POST['harddrive']; $detail=$_POST['detail'];  $datetime=date("d/m/y h:i:s"); //create date time  $query="INSERT INTO forum_question (id, user_id, cat, os, ram, graphics, harddrive, detail, username, datetime)  VALUES('', '$user_id', '$cat', '$os', '$ram', '$graphics', '$harddrive', '$detail', '$username', '$datetime')"; mysql_query($query) or die('Error, insert query failed');  $post_no = mysql_insert_id();   //change this path to match your images directory $dir ='../forum/screens';  //make sure the uploaded file transfer was successful if ($_FILES['uploadfile']['error'] != UPLOAD_ERR_OK) { switch ($_FILES['uploadfile']['error']) { case UPLOAD_ERR_INI_SIZE: die('The uploaded file exceeds the upload_max_filesize directive ' . 'allowed'); break; case UPLOAD_ERR_FORM_SIZE: die('The uploaded file exceeds the MAX_FILE_SIZE directive that ' . 'was specified in the HTML form.'); break; case UPLOAD_ERR_PARTIAL: die('The uploaded file was only partially uploaded.'); break; case UPLOAD_ERR_NO_FILE: die('No file was uploaded.'); break; case UPLOAD_ERR_NO_TMP_DIR: die('The server is missing a temporary folder.'); break; case UPLOAD_ERR_CANT_WRITE: die('The server failed to write the uploaded file to disk.'); break; case UPLOAD_ERR_EXTENSION: die('File upload stopped by extension.'); break; } }  //get info about the image being uploaded $image_caption = $_POST['caption']; $image_user_id = $user_id; $image_username = $username; $image_date = date('Y-m-d'); list($width, $height, $type, $attr) = getimagesize($_FILES['uploadfile']['tmp_name']);  // make sure the uploaded file is really a supported image switch ($type) { case IMAGETYPE_GIF: $image = imagecreatefromgif($_FILES['uploadfile']['tmp_name']) or die('The file you uploaded was not a supported filetype.'); $ext = '.gif'; break; case IMAGETYPE_JPEG: $image = imagecreatefromjpeg($_FILES['uploadfile']['tmp_name']) or die('The file you uploaded was not a supported filetype.'); $ext = '.jpg'; break; case IMAGETYPE_PNG: $image = imagecreatefrompng($_FILES['uploadfile']['tmp_name']) or die('The file you uploaded was not a supported filetype.'); $ext = '.png'; break; default: die('The file you uploaded was not a supported filetype.'); }  //insert information into image table $query = 'INSERT INTO images (image_caption, image_user_id, image_date, post_no) VALUES  ("' . $image_caption . '", "' . $image_user_id . '", "' . $image_date .'", "' . $post_no . '")'; $result = mysql_query($query, $conn) or die (mysql_error($conn)); //retrieve the image_id that MySQL generated automatically when we inserted //the new record $last_id = mysql_insert_id(); //because the id is unique, we can use it as the image name as well to make //sure we don't overwrite another image that already exists $imagename = $last_id . $ext; // update the image table now that the final filename is known. $query = 'UPDATE images SET image_filename = "' . $imagename . '" WHERE image_id = ' . $last_id; $result = mysql_query($query, $conn) or die (mysql_error($conn)); //save the image to its final destination switch ($type) { case IMAGETYPE_GIF: imagegif($image, $dir . '/' . $imagename); break; case IMAGETYPE_JPEG: imagejpeg($image, $dir . '/' . $imagename, 100); break; case IMAGETYPE_PNG: imagepng($image, $dir . '/' . $imagename); break; } imagedestroy($image);  $query = "UPDATE users_credits SET credits = credits - 1 WHERE username = '$username'"; mysql_query($query) or die('Error, insert query failed');  header('Location: ../index.php'); ?>


Which is basically an upload form, it all works great! However, I want the user to have the option of not uploading an image, if this is left blank at the moment then it throws up an error. How would I go about this?

Last edited by icandothat : September 23rd, 2009 at 05:07 PM.

Reply With Quote
  #2  
Old September 23rd, 2009, 05:26 PM
icandothat's Avatar
icandothat icandothat is offline
Super Moderator
Codewalkers Intermediate (1500 - 1999 posts)
 
Join Date: Apr 2007
Location: San Diego, CA
Posts: 1,667 icandothat User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 Days 10 m 43 sec
Reputation Power: 4
Oh man, Can you edit your post and include carriage returns? You might be posting from linux and have different "new line" character or something but this really tough to try to read w/o copying and pasting into an editor and then reformatting with line returns and tabs etc... I started to edit it but it was way to big.
__________________
There is no spoon.

Reply With Quote
  #3  
Old September 23rd, 2009, 05:36 PM
icandothat's Avatar
icandothat icandothat is offline
Super Moderator
Codewalkers Intermediate (1500 - 1999 posts)
 
Join Date: Apr 2007
Location: San Diego, CA
Posts: 1,667 icandothat User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 Days 10 m 43 sec
Reputation Power: 4
I think what I would do is at the top of the script something like this.

PHP Code:
if(! isset($_FILES['uploadfile']['tmp_name'])){
  
$_FILES['uploadfile']['tmp_name'] = 'someLocalFileOnTheServer.jpg';




basically if the user does not upload a file, trick the script into thinking they have uploaded a file by assigning a local file to the uploadfile variable.

You'll also have to get rid of, or ignore, the UPLOAD_ERR switch case.

Reply With Quote
Reply

Viewing: Codewalkers ForumsPHP RelatedPHP Coding > Image upload help


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
Create the Optimal Architecture for your Critical Applications
Warburton's the largest independently owned bakery in the UK faced a number of difficult challenges in providing the most robust yet efficient IT infrastructure for their organization's success. IBM's services combined with their xSeries servers created the perfect platform for their SAP environment with sufficient flexibility, and did so in very time effective fashion.

Request Your Free Technology Downloads!
 
Five Best Practices for Deploying a Successful Service-Oriented Architecture
This white paper describes the benefits you can expect with SOA, and how IBM can help take your business there.

Request Your Free Technology Downloads!
 
Gartner Magic Quadrant for Application Delivery Controllers
Gartner summarizes its view on Application Delivery Controllers, evaluates strengths and weaknesses of solutions, and provides Magic Quadrant reporting for a quick comparison across all vendors. Learn from Gartner how you can benefit from an all-in-one device like Citrix NetScaler that delivers the highest levels of availability, performance and security.

Request Your Free Technology Downloads!
 
Knowledge is Power
What you don't know can hurt you, and is likely costing you money and increasing your security risks during an era of scarce resources. This white paper proposes six key strategies that enterprise security managers can use to improve their network defense posture.

Request Your Free Technology Downloads!
 
Rationalizing the Multi-Tool Environment
The rationalized multi-tool approach is flexible, scalable and cost effective. It provides the necessary input to the IT service management business processes. It preserves prior investments in monitoring tools, empowers technologists to select the best tools with which to do their jobs, and enhances effective response to incidents.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2010 by Developer Shed. All rights reserved. DS Cluster 8 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek