Log in

View Full Version : Rename file during upload



gm8net
10-17-2008, 10:32 PM
Could someone please help me with the code below, I want the file that is being uploaded to be renamed with a random number in addition to the unix timestamp to prevent a filename from ever being duplicated.

I also want to check that files being uploaded are .jpg, .jpeg, .gif, or .png

I've been trying for ages done a lot of googling but can't seem to get it to work for me.


echo 'Upload result:<br>';
$uploaddir = dirname($_SERVER['SCRIPT_FILENAME'])."/UploadedFiles/";

$target_encoding = "ISO-8859-1";
echo '<pre>';
if(count($_FILES) > 0)
{
$arrfile = pos($_FILES);
$uploadfile = $uploaddir . iconv("UTF-8", $target_encoding,basename($arrfile['name']));

if (move_uploaded_file($arrfile['tmp_name'], $uploadfile))
echo "File is valid, and was successfully uploaded.\n";
}
else
echo 'No files sent. Script is OK!';
echo 'Here is some more debugging info:';
print_r($_FILES);

echo "</pre>";

?>

Clark Wells
10-18-2008, 01:47 AM
Gm -

Try this for the Required Filetypes:


$allowed_filetypes = array('.jpg','.gif','.jpeg','.png');

// Get the name of the file (including file extension).
$filename = $_FILES['userfile']['name'];

// Get the extension from the filename.
$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1);

// Check if the filetype is allowed, if not DIE and inform the user.
if(!in_array($ext,$allowed_filetypes))
die('The file you attempted to upload is not allowed.');