herghost Posted September 23, 2009 Share Posted September 23, 2009 Hi All, I have the below: <?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'); ?> Everything works fine, too fine in fact! How can I change this so file upload is not mandatory? Many Thanks Link to comment https://forums.phpfreaks.com/topic/175251-picture-upload-help/ Share on other sites More sharing options...
herghost Posted September 23, 2009 Author Share Posted September 23, 2009 tried everything I can think off any ideas guys n girls? Link to comment https://forums.phpfreaks.com/topic/175251-picture-upload-help/#findComment-923707 Share on other sites More sharing options...
JasonLewis Posted September 23, 2009 Share Posted September 23, 2009 What have you tried? Have you tried to simply check if there are no files uploaded, $_FILES is empty, then don't process any of the file uploading code? Link to comment https://forums.phpfreaks.com/topic/175251-picture-upload-help/#findComment-923832 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.