Link to home
Create AccountLog in
Avatar of satmanuk
satmanukFlag for United Kingdom of Great Britain and Northern Ireland

asked on

PHP upload script need to rename files on upload

Hi all,

i have an upload script that i was given. It has been working fine for me but i would like to add a feature where i rename the files on upload.
I want to have a date stamp and the userid of the logged in user as a prefix to the file name. So in effect keep the file name just make the prefix unique for every pic thats uploaded.

below is a snippet of the pahe the handles the upload.

i have a recordset that gets the userid and you will see i have the variable ready for the time stamp.

Thanks in advance
<table border="0" cellpadding="5" width="100%" align="center" bgcolor="#F0F0FF">
<tr><td colspan="2" bgcolor="#6699CC" align="center"><font color="#FFFFFF" size="+1" align="center">Files Uploaded</font></td></tr>
<tr  bgcolor="#FCFCFF"><td><nobr>File Name</nobr></td>
	<td align="right"><nobr>File size</nobr></td></tr>
<?
 
// Set the save path here
$save_path="../images/uploadtest/temp/$user_ID/";    
 
$userfile_parent=$_REQUEST['userfile_parent'];
$file = $_FILES['userfile'];
$k = count($file['name']);
 
$time_stamp = date("m/d/y H:i:s");
$user_ID = $row_rts_userid['user_id'];
 
for($i=0 ; $i < $k ; $i++)
{
	if($i %2)
	{
		echo '<tr bgcolor="#FAFAFA"> ';
	}
	else
	{	
		echo '<tr>';
	}
	
	$name = split('/',urldecode($file['name'][$i]));
	
	echo '<td align="left">' . $name[count($name)-1] ."</td>\n";
	echo '<td align="right">' . $file['size'][$i] ."</td></tr>\n";
 
	if(isset($save_path) && $save_path!="")
	{
		move_uploaded_file($file['tmp_name'][$i], $save_path . $name[count($name)-1]);
	}
	
}
 
// checks if the save path variable has been set above. Providing there is something set on $save_path the following wont execute
if(! isset($save_path) || $save_path =="")
{
	echo '<tr style="color: #0066cc"  bgcolor="#FCFCFC" ><td colspan=2 align="left">Files have been uploaded but not saved because the destination folder has not been set. Please change the $save_path in upload.php</td></tr>';
}
// end of code snippet
 
if(isset($userfile_parent))
{
	echo "<tr bgcolor='#FCFCFC' style='color: #0066cc'><td colspan=2></td></tr>";
}
 
?>
</table>

Open in new window

Avatar of aragornol
aragornol
Flag of Slovakia image

move_uploaded_file($file['tmp_name'][$i], $save_path . $user_ID . "_" . $time_stamp . "_" . $name[count($name)-1]);
Avatar of satmanuk

ASKER

hi thanks.

i tried that but the upload fails to happen?

any ideas?
i done some googling and i had seen that this was the method to rename on upload. so i tried it as you suggested prior to this post but it failed.

ASKER CERTIFIED SOLUTION
Avatar of aragornol
aragornol
Flag of Slovakia image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
i have a java applet that uses this upload script to upload the pictures.

I dont get an error, i have a page that the uplaod directs to after and this displays the images uploaded, it doesnt show the images and when iphysicall ftp to that folder they dont exist either.

i dont see it being a java applet issue though as it merely resizes images and then uses the upload.php page i have to physically move the files.

$user_ID is assinged by a recordset query i have at the top of the upload.php page.

i have added the complete page below.

i changed the date format as you suggested.

i have echo'd the date and user id variables and they are assigning ok?
<?php 
session_start();
if(!isset($_COOKIE['PHPSESSID']))
setcookie("PHPSESSID",session_id());
?>
<?php require_once('../Connections/reg_con.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $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;
}
}
 
 
if (isset($_SESSION['MM_Username'])) {
// logged in, test user level
if ($_SESSION['MM_UserGroup']=="0") {
header("Location: notvalid.php");
}
} else {
// not logged in, send them to the login page
header("Location: login.php");
}
$username = $_SESSION['MM_Username'];
 
 
$colname_rts_userid = "-1";
if (isset($_SESSION['MM_Username'])) {
  $colname_rts_userid = (get_magic_quotes_gpc()) ? $_SESSION['MM_Username'] : addslashes($_SESSION['MM_Username']);
}
mysql_select_db($database_reg_con, $reg_con);
$query_rts_userid = sprintf("SELECT user_id, user_email FROM users WHERE user_email = %s", GetSQLValueString($colname_rts_userid, "text"));
$rts_userid = mysql_query($query_rts_userid, $reg_con) or die(mysql_error());
$row_rts_userid = mysql_fetch_assoc($rts_userid);
$totalRows_rts_userid = mysql_num_rows($rts_userid);
 
$colname_rts_profile = "-1";
if (isset($_SESSION['MM_Username'])) {
  $colname_rts_profile = (get_magic_quotes_gpc()) ? $_SESSION['MM_Username'] : addslashes($_SESSION['MM_Username']);
}
mysql_select_db($database_reg_con, $reg_con);
$query_rts_profile = sprintf("SELECT * FROM users WHERE user_email = %s", GetSQLValueString($colname_rts_profile, "text"));
$rts_profile = mysql_query($query_rts_profile, $reg_con) or die(mysql_error());
$row_rts_profile = mysql_fetch_assoc($rts_profile);
$totalRows_rts_profile = mysql_num_rows($rts_profile);
 
$user_ID = $row_rts_userid['user_id'];
 
?>
<html>
<head><title>Upload Script</title></head>
 
 
<body  bgcolor="FFFFFF">
<table border="0" cellpadding="5" width="100%" align="center" bgcolor="#F0F0FF">
<tr><td colspan="2" bgcolor="#6699CC" align="center"><font color="#FFFFFF" size="+1" align="center">Files Uploaded</font></td></tr>
<tr  bgcolor="#FCFCFF"><td><nobr>File Name</nobr></td>
	<td align="right"><nobr>File size</nobr></td></tr>
<?
 
// Set the save path here
$save_path="../images/uploadtest/temp/$user_ID/";    
 
$userfile_parent=$_REQUEST['userfile_parent'];
$file = $_FILES['userfile'];
$k = count($file['name']);
 
$time_stamp = date("m/d/y_H:i:s");
 
 
for($i=0 ; $i < $k ; $i++)
{
	if($i %2)
	{
		echo '<tr bgcolor="#FAFAFA"> ';
	}
	else
	{	
		echo '<tr>';
	}
	
	$name = split('/',urldecode($file['name'][$i]));
	
	echo '<td align="left">' . $name[count($name)-1] ."</td>\n";
	echo '<td align="right">' . $file['size'][$i] ."</td></tr>\n";
 
	if(isset($save_path) && $save_path!="")
	{
	//	move_uploaded_file($file['tmp_name'][$i], $save_path . $name[count($name)-1]);
	    move_uploaded_file($file['tmp_name'][$i], $save_path . $user_ID . "_" . $time_stamp . "_" . $name[count($name)-1]);
	}
	
}
 
// checks if the save path variable has been set above. Providing there is something set on $save_path the following wont execute
if(! isset($save_path) || $save_path =="")
{
	echo '<tr style="color: #0066cc"  bgcolor="#FCFCFC" ><td colspan=2 align="left">Files have been uploaded but not saved because the destination folder has not been set. Please change the $save_path in upload.php</td></tr>';
}
// end of code snippet
 
if(isset($userfile_parent))
{
	echo "<tr bgcolor='#FCFCFC' style='color: #0066cc'><td colspan=2></td></tr>";
}
 
?>
</table>
<?php
echo $time_stamp;
echo "<br />";
echo $user_ID;
?>
 
</body>
</html>

Open in new window

SOLUTION
Avatar of AlexanderR
AlexanderR
Flag of Canada image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
and also see what you get when you do
if(isset($save_path) && $save_path!="")
        {
        $destination = $save_path . $user_ID . "_" . $time_stamp . "_" . $name[count($name)-1];
echo $destination;
            move_uploaded_file($file['tmp_name'][$i], $destination);
        }
        

Open in new window

top job!
you were right! and it totally makes sense!

Thanks for your help guys!