Jump to content

upload and insert new filename


Ne0_Dev

Recommended Posts

Hi

 

I am trying to use this code to upload 4 images format the filename and the insert the new filename into the correct field in the table.  the upload and insert works fine however i am unable to assign the correct formatted filename to each of the image variables to get inserted into the table.

 

any help would be greatly appreciated.

 

<?php

// define a constant for the maximum upload size
define ('MAX_FILE_SIZE', 512000);
// check if form has been submitted
if (array_key_exists('submit', $_POST)) {
  // define constant for upload folder
  define('UPLOAD_DIR', '/uploadedimages/');
  // convert the maximum size to KB
  $max = number_format(MAX_FILE_SIZE/1024, 1).'KB';
  // create an array of permitted MIME types
  $permitted = array('image/jpeg', 'image/jpg');
  
  foreach ($_FILES['image']['name'] as $number => $file) {
    // replace any spaces in the filename with underscores
$file = str_replace(' ', '_', $file);
    // begin by assuming the file is unacceptable
    $sizeOK = false;
    $typeOK = false; 
    // check that file is within the permitted size
    if ($_FILES['image']['size'][$number] > 0 || $_FILES['image']['size'][$number] <= MAX_FILE_SIZE) {
    $sizeOK = true;
}
    // check that file is of an permitted MIME type
    foreach ($permitted as $type) {
      if ($type == $_FILES['image']['type'][$number]) {
        $typeOK = true;
    break;
    }
  }
    if ($sizeOK && $typeOK) {
      switch($_FILES['image']['error'][$number]) {
    case 0:
          // check if a file of the same name has been uploaded
	  if (!file_exists(UPLOAD_DIR.$file)) {
	    // move the file to the upload folder and rename it
	    $success = move_uploaded_file($_FILES['image']['tmp_name'][$number], UPLOAD_DIR.$file);
	    }
	  else {
	    // get the date and time
	    ini_set('date.timezone', 'Europe/London');
	    $now = date('Y-m-d-His');
	    $success = move_uploaded_file($_FILES['image']['tmp_name'][$number], UPLOAD_DIR.$now.$file);
	    }
	  if ($success) {
            $result[] = "$file uploaded successfully";
        }
	  else {
	    $result[] = "Error uploading $file. Please try again.";
	    }
      break;
    case 3:
	  $result[] = "Error uploading $file. Please try again.";
	default:
          $result[] = "System error uploading $file. Contact webmaster.";
    }
      }
    elseif ($_FILES['image']['error'][$number] == 4) {
  $result[] = 'No file selected';
  }
else {
      $result[] = "$file cannot be uploaded. Maximum size: $max. Acceptable file types: jpeg";
  }
}
	// get form field values and assign to variable
$listingtype = $_POST['listingtype'];
$proptype = $_POST['proptype'];
$housenum = $_POST['housenum'];
$addr1 = $_POST['addr1'];
$addr2 = $_POST['addr2'];
$country = $_POST['country'];
$state = $_POST['state'];
$city = $_POST['city'];
$pcode = $_POST['pcode'];
$price = $_POST['price'];
$bedrooms = $_POST['bedrooms'];
$bathrooms = $_POST['bathrooms'];
$parking = $_POST['parking'];
$garden = $_POST['garden'];
$shortdes = $_POST['shortdes'];
$longdes = $_POST['longdes'];
$feature1 = $_POST['feature1'];
$feature2 = $_POST['feature2'];
$feature3 = $_POST['feature3'];
$feature4 = $_POST['feature4'];
$feature5 = $_POST['feature5'];
$feature6 = $_POST['feature6'];
$image1 = $_FILES['image'][$number][0];
$image2 = $_FILES['image'][$number][1];
$image3 = $_FILES['image'][$number][2];
$image4 = $_FILES['image'][$number][3];
$db = mysql_connect($dbhost, $dbuser, $dbpassword) or die ('Cannot connect to MySQL server');
    mysql_select_db($dbdatabase, $db) or die ('Cannot open database');
$sql = "INSERT INTO nxtbl_property (listingType, propertyType, housenum, addr1, addr2, country, state, city, pcode, price, bedrooms, bathrooms, parking, garden, shortdes, longdes, feature1, feature2, feature3, feature4, feature5, feature6, image1, image2, image3, image4) VALUES('$listingtype', '$proptype', '$housenum', '$addr1', '$addr2', '$country', '$state', '$city', '$pcode', '$price', '$bedrooms', '$bathrooms', '$parking', '$garden', '$shortdes', '$longdes', '$feature1', '$feature2', '$feature3', '$feature4', '$feature5', '$feature6', '$image1', '$image2', '$image3', '$image4')";
mysql_query($sql) or die(mysql_error());
header("Location: " .$config_basedir . "createsuccess.php");
  }
?> 

Link to comment
https://forums.phpfreaks.com/topic/180668-upload-and-insert-new-filename/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.