I have been playing with image uploads recently and have just got a little script working ... but not being an expert I was wondering if it was a pretty safe script and also how to do a few things with the filename.
Here's my code;
PHP
<?php
ini_set("upload_max_filesize", "8mb");
// $userfile is where file went on webserver
$userfile = $HTTP_POST_FILES['userfile']['tmp_name'];
// $userfile_name is original file name
$userfile_name = $HTTP_POST_FILES['userfile']['name'];
// $userfile_size is size in bytes
$userfile_size = $HTTP_POST_FILES['userfile']['size'];
// $userfile_type is mime type e.g. image/gif
$userfile_type = $HTTP_POST_FILES['userfile']['type'];
// $userfile_error is any error encountered
$userfile_error = $HTTP_POST_FILES['userfile']['error'];
// userfile_error was introduced at PHP 4.2.0
// use this code with newer versions
// put the file where we'd like it
$upfile = 'upload/'.$userfile_name;
// is_uploaded_file and move_uploaded_file
if (is_uploaded_file($userfile))
{
if (!move_uploaded_file($userfile, $upfile))
{
echo '<p><strong>Error:</strong> Could not move file to destination directory.<p>';
exit;
}
} else {
echo '<p><strong>Error:</strong> Possible file upload attack. Filename: '.$userfile_name.'</p>';
}
echo '<p>Success! File uploaded successfully. Filename: '.$userfile_name.'.</p>';
?>
ini_set("upload_max_filesize", "8mb");
// $userfile is where file went on webserver
$userfile = $HTTP_POST_FILES['userfile']['tmp_name'];
// $userfile_name is original file name
$userfile_name = $HTTP_POST_FILES['userfile']['name'];
// $userfile_size is size in bytes
$userfile_size = $HTTP_POST_FILES['userfile']['size'];
// $userfile_type is mime type e.g. image/gif
$userfile_type = $HTTP_POST_FILES['userfile']['type'];
// $userfile_error is any error encountered
$userfile_error = $HTTP_POST_FILES['userfile']['error'];
// userfile_error was introduced at PHP 4.2.0
// use this code with newer versions
// put the file where we'd like it
$upfile = 'upload/'.$userfile_name;
// is_uploaded_file and move_uploaded_file
if (is_uploaded_file($userfile))
{
if (!move_uploaded_file($userfile, $upfile))
{
echo '<p><strong>Error:</strong> Could not move file to destination directory.<p>';
exit;
}
} else {
echo '<p><strong>Error:</strong> Possible file upload attack. Filename: '.$userfile_name.'</p>';
}
echo '<p>Success! File uploaded successfully. Filename: '.$userfile_name.'.</p>';
?>
Now this works, outputs the exact name of the file. But the thing is, I want to change the filenames all to lowercase and exchange all spaces to underscores (for constistancy).
Is this possible using preg_replace or would I have to use something else?
Cheers