gitlez

YA: Upload/File Updates 20130617070959AAgg2Zp

Jun 18th, 2013
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.77 KB | None | 0 0
  1. <?php
  2. // Yahoo Answers Question http://answers.yahoo.com/question/index?qid=20130617070959AAgg2Zp
  3.  
  4.  
  5. /***   Variables   ***/
  6. $message = '';
  7.  
  8. /***   Functions   ***/
  9. function uploadErrorMsg( $error ){
  10.     $errors = array(
  11.         1 => 'The uploaded file exceeds the Server\'s Maximum Allowable File Size',
  12.         2 => 'The uploaded file exceeds the Form\'s Maximum Allowable File Size.',
  13.         3 => 'The uploaded file was only partially uploaded, then interrupted or the connection was dropped.',
  14.         4 => 'No file was uploaded.',
  15.         6 => 'Missing a temporary folder. The server requires a temporary folder for file uploads.', // Server Setup Error
  16.         7 => 'Failed to write file to disk.', // Internal Server Error
  17.         8 => 'A PHP extension stopped the file upload.' // PHP Extension Library stopped the upload.
  18.     );
  19.     return $errors[(int) $error];
  20. }
  21.  
  22. /***   Processing   ***/
  23. if( $_SERVER[ 'REQUEST_METHOD' ] === 'POST' && isset($_POST['file']) && trim($_POST['file']) !== ''){
  24.     // Variables
  25.     $file = (isset($_POST['file']))? trim($_POST['file']) : '';
  26.     $num = (int) str_replace(Array('product','.txt'), '', $file);
  27.     $content = (isset($_POST['content']))? trim( $_POST['content'] ) : '';
  28.     $image = (isset($_FILES['image']))? $_FILES['image'] : false;
  29.     $imageFile = 'downloads/product' . $num . '.jpg';
  30.     $statsFile = 'stats' . $num . '.txt';
  31.    
  32.     // Replace/Write Content Of Product File
  33.     $fh = @fopen($file, 'w');
  34.     if( !$fh ){
  35.         $message .= 'Couldn\'t Open File: "' . $file . '" (Line: ' . (__LINE__ - 2) . ')<br />';
  36.     }else if( !fwrite($fh, $content) ){
  37.         $message .= 'Couldn\'t Write Out to File. (Line: ' . (__LINE__ - 1) . ')<br />';
  38.     }else if( is_resource($fh) ){
  39.         @fclose($fh);
  40.         $message .= 'Product File Content Added!<br />';
  41.     }
  42.    
  43.     // Image Upload
  44.     if($image && $image['error'] === UPLOAD_ERR_OK && ($image['type'] === 'image/jpg' || $image['type'] === 'image/jpeg')){
  45.         if(move_uploaded_file( $image['tmp_bame'], $imageFile) ){
  46.             $message .= 'Image Uploaded Successfully!<br />';
  47.         }else{
  48.             $message .= 'Image Upload Failed, due to internal error. Check folder permissions (line: ' . (__LINE__ - 3) . ').<br />';
  49.         }
  50.     }else if($image['type'] !== 'image/jpg' && $image['type'] !== 'image/jpeg'){
  51.         $message .= 'Image is not a JPEG. Only JPEG are formatted to work.<br />';
  52.     }else if( !$image ){
  53.         $message .= 'No Image was selected to upload.<br />'; // iOS Safari and some versions of Safari do not post empty file form elements, if no file was selected.
  54.     }else{
  55.         $message .= 'Image Upload Error: (' . $image['error'] . ') ' . uploadErrorMsg($image['error']) . '<br />';
  56.     }
  57.    
  58.    
  59.     // Stats
  60.     $fh = @fopen($statsFile, 'w+');
  61.     $size = filesize($statsFile);
  62.     if( !is_resource($fh) ){
  63.         $message .= 'Cannot Open Stats file "' . $statsFile . '" (Line: ' . (__LINE__ - 2) . '). Check Permissions.<br />';
  64.     }else{
  65.         $stat = (file_exists($statsFile) && $size > 0)? @fread($fh, $size) : 0;
  66.         if( $stat === false){
  67.             $message .= 'Couldn\'t Read From Stat File (Line: ' . (__LINE__ - 2) . '). Check Permissions.<br />';
  68.         }else{
  69.             $stat++;
  70.             if( @fwrite($fh, $stat) ){
  71.                 $message .= 'Stat File updated.<br />';
  72.             }else{
  73.                 $message .= 'Couldn\'t write to stats file (Line: ' . (__LINE__ - 3) . '). Check Permissions.<br />';
  74.             }
  75.         }
  76.     }
  77. }else if( $_SERVER[ 'REQUEST_METHOD' ] === 'POST' ){
  78.     $message .= 'Please select a Product File.<br />';
  79. }
  80.  
  81.  
  82.  
  83. /***   Message Formatting   ***/
  84. if( isset($message{1}) ){
  85.     $message = '        <div style="padding: 0;border: 1px solid #CCC;">
  86.            <h3 style="background-color: #CCC;margin: 0;padding: 4px;">Message</h3>
  87.            <p style="padding: 8px;">
  88.            ' . $message . '
  89.            </p>
  90.        </div>';
  91. }
  92.  
  93. /***  HTML Output    ***/
  94. echo '<!DOCTYPE html>
  95. <html lang="en-us">
  96.    <head>
  97.        <title>Change Top 3 cars</title>
  98.    </head>
  99.    <body>
  100.        ' . $message . '
  101.        <form method="post" action="connor_lockett_p5.php" enctype="multipart/form-data">
  102.            New Content: <input type="text" name="content" />
  103.            <br />
  104.            New Image: <input type="file" name="image" size="10" />
  105.            <br />
  106.            Products:
  107.            <select name="file">
  108.                <option value="product1.txt">Product 1</option>
  109.                <option value="product2.txt">Product 2</option>
  110.                <option value="product3.txt">Product 3</option>
  111.            </select>
  112.            <br />
  113.            <input type="submit" value="Upload" />
  114.        </form>
  115.    </body>
  116. </html>';
Advertisement
Add Comment
Please, Sign In to add comment