Paste Search Dynamic
Recent pastes
PHP upload
  1. <?php
  2.  
  3. // filename: upload.processor.php
  4.  
  5. // first let's set some variables
  6.  
  7. // make a note of the current working directory, relative to root.
  8. $directory_self = str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['PHP_SELF']);
  9.  
  10. // make a note of the directory that will recieve the uploaded file
  11. $uploadsDirectory = 'C:/Users/Stefan/Documents/phpTest/' . $_POST['type'];
  12.  
  13. // make a note of the location of the upload form in case we need it
  14. $uploadForm = 'adminupload.php';
  15.  
  16. // make a note of the location of the success page
  17. $uploadSuccess = 'uploadsuccess.php';
  18.  
  19. // fieldname used within the file <input> of the HTML form
  20. $fieldname = 'file';
  21.  
  22. // Now let's deal with the upload
  23.  
  24. // possible PHP upload errors
  25. $errors = array(1 => 'php.ini max file size exceeded',
  26.                 2 => 'html form max file size exceeded',
  27.                 3 => 'file upload was only partial',
  28.                 4 => 'no file was attached');
  29.  
  30. // check the upload form was actually submitted else print the form
  31. isset($_POST['submit'])
  32.     or error('the upload form is needed', $uploadForm);
  33.  
  34. // check for PHP's built-in uploading errors
  35. ($_FILES[$fieldname]['error'] == 0)
  36.     or error($errors[$_FILES[$fieldname]['error']], $uploadForm);
  37.      
  38. // check that the file we are working on really was the subject of an HTTP upload
  39. @is_uploaded_file($_FILES[$fieldname]['tmp_name'])
  40.     or error('not an HTTP upload', $uploadForm);
  41.      
  42. // validation... since this is an image upload script we should run a check  
  43. // to make sure the uploaded file is in fact an image. Here is a simple check:
  44. // getimagesize() returns false if the file tested is not an image.
  45. @getimagesize($_FILES[$fieldname]['tmp_name'])
  46.     or error('only image uploads are allowed', $uploadForm);
  47.      
  48. // make a unique filename for the uploaded file and check it is not already
  49. // taken... if it is already taken keep trying until we find a vacant one
  50. // sample filename: 1140732936-filename.jpg
  51. $now = 0;
  52. echo $now;
  53. $extension = substr($_FILES[$fieldname]['name'], $_FILES[$fieldname]['name'].length - 4);
  54. while(file_exists($uploadFilename = $uploadsDirectory.$_POST['itemName'].'+'.$now . $extension))
  55. {
  56.     $now++;
  57. }
  58.  
  59. // now let's move the file to its final location and allocate the new filename to it
  60. @move_uploaded_file($_FILES[$fieldname]['tmp_name'], $uploadFilename)
  61.         or error('receiving directory insuffiecient permission', $uploadForm);
  62.  
  63. $fileHandle = fopen($uploadsDirectory.$_POST['itemName']. '+' . $now . '.html', 'w');
  64. fwrite($fileHandle, "<p style = 'color:white;font-family: 'Helvetica', 'Arial'>" . $_POST['description'] . '</p>');
  65. fclose($fileHandle);
  66.      
  67. // If you got this far, everything has worked and the file has been successfully saved.
  68. // We are now going to redirect the client to a success page.
  69. header('Location: ' . $uploadSuccess);
  70.  
  71. // The following function is an error handler which is used
  72. // to output an HTML error page if the file upload fails
  73. function error($error, $location, $seconds = 5)
  74. {
  75.     header("Refresh: " . $seconds);
  76.     echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"'."n".
  77.     '"http://w...content-available-to-author-only...3.org/TR/html4/strict.dtd">'."nn".
  78.     '<html lang="en">'."n".
  79.     '    <head>'."n".
  80.     '        <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">'."nn".
  81.     '        <link rel="stylesheet" type="text/css" href="stylesheet.css">'."nn".
  82.     '    <title>Upload error</title>'."nn".
  83.     '    </head>'."nn".
  84.     '    <body>'."nn".
  85.     '    <div id="Upload">'."nn".
  86.     '        <h1>Upload failure</h1>'."nn".
  87.     '        <p>An error has occurred: '."nn".
  88.     '        <span class="red">' . $error . '...</span>'."nn".
  89.     '         The upload form is reloading</p>'."nn".
  90.     '     </div>'."nn".
  91.     '</html>';
  92.     exit;
  93. } // end error handler
  94.  
  95. ?>
Parsed in 0.062 seconds