time stamp a name

[php]

<?php // Start a session for error reporting session_start(); //Include database connection details require_once('config.php'); //Array to store validation errors $errmsg_arr = array(); //Validation error flag $errflag = false; //Connect to mysql server $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if(!$link) { die('Failed to connect to server: ' . mysql_error()); } //Select database $db = mysql_select_db(DB_DATABASE); if(!$db) { die("Unable to select database"); } // Check to see if the type of file uploaded is a valid image type function is_valid_type($file) { // This is an array that holds all the valid image MIME types $valid_image_types = array("image/jpg","image/JPG", "image/jpeg", "image/bmp", "image/gif", "image/png"); if (in_array($file['type'], $valid_image_types)) return 1; return 0; } // Just a short function that prints out the contents of an array in a manner that's easy to read // I used this function during debugging but it serves no purpose at run time for this example function showContents($array) { echo "
";
	print_r($array);
	echo "
"; } // Set some constants // This variable is the path to the image folder where all the images are going to be stored // Note that there is a trailing forward slash $TARGET_PATH_IMAGES = "upload-images/"; $TARGET_PATH_DOCS = "upload-docs/"; // Get our POSTed variables $fname = $_FILES['uploaded_file']; $about = $_POST['about']; $spec = $_POST['spec']; $image = $_FILES['image']; // Sanitize our inputs $about = mysql_real_escape_string($about); $spec = mysql_real_escape_string($spec); $image['name'] = mysql_real_escape_string($image['name']); $fname['name'] = mysql_real_escape_string($fname['name']); // Build our target path full string. This is where the file will be moved do // i.e. images/picture.jpg $TARGET_PATH_IMAGES .= $image['name']; $TARGET_PATH_DOCS .= $fname['name']; // Make sure all the fields from the form have inputs if ( $fname['name'] == "" || $about == "" || $spec == "" || $image['name'] == "" ) { $errmsg_arr[] = "All fields are required"; $errflag = true; } // Check to make sure that our file is actually an image // You check the file type instead of the extension because the extension can easily be faked if (!is_valid_type($image)) { $errmsg_arr[] = "You must upload a jpeg, gif, or bmp"; $errflag = true; } // Here we check to see if a file with that name already exists // You could get past filename problems by appending a timestamp to the filename and then continuing if (file_exists($TARGET_PATH_IMAGES)) { $errmsg_arr[] = "A image with that name already exists"; $errflag = true; } if (file_exists($TARGET_PATH_DOCS)) { $errmsg_arr[] = "A document with that name already exists"; $errflag = true; } //If there are input validations, redirect back to the form if($errflag) { $_SESSION['ERRMSG_ARR'] = $errmsg_arr; session_write_close(); header("location: My-Area.php"); exit(); } // move the file from its temporary directory to its new directory if (move_uploaded_file($fname['tmp_name'], $TARGET_PATH_DOCS)) { if (move_uploaded_file($image['tmp_name'], $TARGET_PATH_IMAGES)) { // NOTE: This is where a lot of people make mistakes. // We are *not* putting the image into the database; we are putting a reference to the file's location on the server $sql = "insert into file (name, about, spec, data, image) values ('".$fname['name']."', '$about', '$spec', '$data','".$image['name']."')"; $result = mysql_query($sql) or die ("Could not insert data into DB: " . mysql_error()); $errmsg_arr[] = "New item has been added"; $errflag = true; } } else { // A common cause of file moving failures is because of bad permissions on the directory attempting to be written to // Make sure you chmod the directory to be writeable $errmsg_arr[] = "Could not upload file. Check read/write persmissions on the directory"; $_SESSION['ERRMSG_ARR'] = $errmsg_arr; $errflag = true; } if($errflag) { $_SESSION['ERRMSG_ARR'] = $errmsg_arr; session_write_close(); header("location: My-Area.php"); exit(); } ?>

[/php]

[php] if (move_uploaded_file($image[‘tmp_name’], $TARGET_PATH_IMAGES))
{[/php]

i am trying to add a timestamp to the image on copy so it dont overide a previouse one with the same name

Hello cogga28 replace below code [php] //replace this code $TARGET_PATH_IMAGES .= $image['name']; [/php]

with below code (use below code)
[php]
// use this code
$img_parts = pathinfo($image[‘name’]);
$newimgname = $img_parts[‘filename’].’_’.time().’.’.$img_parts[‘extension’];
$TARGET_PATH_IMAGES .= $newimgname;
[/php]

i hope this will helpful for you.
reply your feedback and other issues.
SR

cool thank :0)

Enjoy… :slight_smile: :slight_smile:
ask anything regarding php you will get a positive answer and help…

SR
:slight_smile: :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service