0

Hi
I have a multiple-query issue I'd like some advice and help with.
I am trying to create a website, in which users can upload their own images to the site.
I am a beginning in PHP (and only solid in html and some sql querying)
What I think I need to be able to do is the following:
1. Enable the user to upload images to be stored in a folder on the server.
2. A location URL to also be uploaded (with other parts of the form data) to a database table.
3. Some way to have the folder create a new name for itself each time a new user uploads imags - and for the folder to connect to the database record for that user.

I do not want the images to immediately show on the webpage on uploading.
I seem to be able to create scripts which can either store the image in the folder, or store the image in the database (which I dont' want) or to just simply upload the URL to the database . I can't seem to have the upload.php script do what I want above.

The script below is called from a simple form I have which allows upto x number of pics to be selected from the User's file directory.

Any help on how I can start to go about understanding how to make the upload function do the above - or indeed if there is a simpler way? Then I would be very grateful.....
Many thanks

<?php
//set where you want to store files
//in this example we keep file in folder upload 
//$HTTP_POST_FILES['ufile']['name']; = upload file name
//for example upload file name cartoon.gif . $path will be upload/cartoon.gif
$path1= "upload/".$HTTP_POST_FILES['ufile']['name'][0];
$path2= "upload/".$HTTP_POST_FILES['ufile']['name'][1];
$path3= "upload/".$HTTP_POST_FILES['ufile']['name'][2];

//copy file to where you want to store file
copy($HTTP_POST_FILES['ufile']['tmp_name'][0], $path1);
copy($HTTP_POST_FILES['ufile']['tmp_name'][1], $path2);
copy($HTTP_POST_FILES['ufile']['tmp_name'][2], $path3);

//code to send URL and form data to database

<?php require_once('Connections/erinpubl_localhost.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}  
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO main_tb (id, title, image) VALUES (%s, %s, %s",
                       GetSQLValueString($_POST['id'], "text"),
                       GetSQLValueString($_POST['title'], "text"),
                       GetSQLValueString($_POST['image'], "text"));
					   
					   

  mysql_select_db($database_erinpubl_localhost, $erinpubl_localhost);
  $Result1 = mysql_query($insertSQL, $erinpubl_localhost) or die(mysql_error());
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Inserting Product Records</title>
</head>

<body>
<p>Insert a New Product Record</p>
<table width="529" border="1">
  <tr>
    <td>Please enter details of the new product, including full location URL of images.</td>
  </tr>
  <tr>
    <td height="230"><form action="<?php echo $editFormAction; ?> method="" enctype="multipart/form-data" id="form1"post> 
      <table align="left">
          <tr valign="baseline">
            <td nowrap="nowrap" align="right">Product ID:</td>
            <td><input type="text" name="id" value="" size="32" /></td>
          </tr>
          <tr valign="baseline">
            <td nowrap="nowrap" align="right">Prod_name:</td>
            <td><input type="text" name="title" value="" size="32" /></td>
          </tr>
          <tr valign="baseline">
            <td nowrap="nowrap" align="right">Image:</td>
            <td><input type="text" name="image" value="" size="32" /></td>
          </tr>
          <tr valign="baseline">
            <td nowrap="nowrap" align="right">&nbsp;</td>
            <td><input type="submit" value="Insert record" /></td>
          </tr>
          </table>
        <p>
          <input type="hidden" name="MM_insert" value="form1" />
        </p>
        <p>&nbsp;</p>
        <p>&nbsp;</p>
    </form>
    <p>&nbsp;</p></td>
  </tr>
</table>
</body>
</html>
2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by nonshatter
0

This is an old function I used to use. Not sure how helpful it will be, but at least it shows how to give the image a unique name using time():

<?php

define("MAX_SIZE","100");

$errors = 0;

if (isset($_POST['upload'])) {
	$image = $_FILES['image']['name'];
		
	if ($image) {
		$filename = stripslashes($_FILES['image']['name']);
		$extension = getExtension($filename);
		$extension = strtolower($extension);
			
		if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "gif") && ($extension != "png")) {
			$errors = 1;
			echo 'wrong extension';
		}
		else {
			$size = filesize($_FILES['image']['tmp_name']);
		
				if ($size > MAX_SIZE*1000) {
					$errors = 1;
					echo 'Sorry, the image is too large, Please specify another';
				}		
			//give unique name to image
			$image_name = time().'.'.$extension;
			$newname = "images/".$image_name;
			
			$copied = copy($_FILES['image']['tmp_name'],$newname);
			$_SESSION['img'] = $newname;

				if (!$copied) {
					$errors = 1;
				} 
		}
	}
}

//--- DETERMINES IMAGE EXTENSION ---//
function getExtension($str) {
	$i = strrpos($str,".");
		if (!$i) {
			return "";
		}
	$l = strlen($str)-$i;
	$exten = substr($str,$i+1,$l);
	return $exten;
}
?>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.