PHP Code:
$userfile = $_FILES['action_id']['name'];
$file_size = $_FILES['action_id']['size'];
$file_temp = $_FILES['action_id']['tmp_name'];
$file_err = $_FILES['action_id']['error'];
$path = '../../downloads/photoshop/temp_actions/';
// limit the size of the file to 20MB
if($file_size > 20971520) {
echo '<font color="#F0B80F"><b>The file you are trying to upload is too large. Please restrict your file to at least 20MB </b></font><BR />';
exit();
}
// Create a new file name
// This is so if other files on the server have the same name, it will be renamed
$randomizer = rand(00, 99);
$file_name = $randomizer. '_' .$userfile;
// Get the file type
// Using $_FILES to get the file type is sometimes inaccurate, so we are going
// to get the extension ourselves from the name of the file
// This also eliminates having to worry about the MIME type
$file_type = $userfile;
$file_type_length = strlen($file_type) - 3;
$file_type = substr($file_type, $file_type_length);
if(!empty($userfile)) {
echo 'File Uploaded Information
<ul>
<li>File being uploaded: ' .$userfile. '</li>
<li>File Type: .' .$file_type.'</li>
<li>File Size: ' .$file_size. ' KB </li>
<li>File Error: ' . $file_err. '</li>
</ul>';
$errors = array(); // Initialize an errors array
// Set allowed file types
// Set case of all letters to lower case
$file_type = strtolower($file_type);
$files = array("atn"); // Allowed file extensions
$allowed_type = array("text/plain", "application/msword", "application/pdf");
// Search the array for the allowed file type
$ext = strtolower(substr($userfile, strrpos($userfile, '.') + 1));
if ( !in_array($ext, $files) ) { // Check for allowed file extension
$errors[] = '<font color="#F0B80F"><b>The file you are trying to upload has the wrong extension.';
}
if ( empty($errors) ) { // If the errors array is empty, there are no errors, so upload
// UPLOAD THE FILE
echo 'Thank you for your contribution. <br>You should see your action on the site within 24 hours.';
}
if ( !empty($errors) ) { // Check to see if there were errors and echo them to the user
foreach ( $errors as $error ) {
echo $error . ' Please choose another file. </b></font><br />';
}
}
# echo '<b>The file you are uploading is allowed. </b><br />';
# } else {
# echo '<font color="#F0B80F"><b>The file you are attempting to upload has the wrong extension. </b></font><br />';
# exit();
#}
// Check for errors and upload the file
$error_count = count($file_error);
if($error_count > 0) {
for($i = 0; $i <= $error_count; ++$i) {
echo $_FILES['action_id']['error'][$i];
}
} else {
if(move_uploaded_file($file_temp, '../../downloads/photoshop/temp_actions/' .$file_name.'')) {
echo '';
} else {
echo '<font color="#F0B80F"><b>Whoops look like there was a mistake during your upload. Please try again.</b></font>';
}
}
} else {
echo '<font color="#F0B80F"><b>No file has been selected.</b></font>';
}
Bookmarks