Advertisement
gitlez

YA: ProductInfo Update 20130615052503AAeEEVI

Jun 15th, 2013
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.22 KB | None | 0 0
  1. <?php
  2. // Yahoo Answers: http://answers.yahoo.com/question/index?qid=20130615052503AAeEEVI
  3. function uploadErrorMsg( $error ){
  4.     $errors = array(
  5.         1 => 'The uploaded file exceeds the Server\'s Maximum Allowable File Size',
  6.         2 => 'The uploaded file exceeds the Form\'s Maximum Allowable File Size.',
  7.         3 => 'The uploaded file was only partially uploaded, then interrupted or the connection was dropped.',
  8.         4 => 'No file was uploaded.',
  9.         6 => 'Missing a temporary folder. The server requires a temporary folder for file uploads.', // Server Setup Error
  10.         7 => 'Failed to write file to disk.', // Internal Server Error
  11.         8 => 'A PHP extension stopped the file upload.' // PHP Extension Library stopped the upload.
  12.     );
  13.     return $errors[(int) $error];
  14. }
  15.  
  16.  
  17. echo '<!DOCTYPE html>
  18. <html lang="en-us">
  19.    <head>
  20.        <title>Change Top 3 helicopters</title>
  21.    </head>
  22.    <body>
  23.        <h1>Change Top 3 helicopters</h1>';
  24.  
  25.  
  26. if( isset($_FILES['file']) ){
  27.     // Variable Defining
  28.     $filename = $_POST['filename'];
  29.     $product_number = (int) str_replace(Array('product','.txt'), '', $filename);
  30.     $content = $_POST['content'];
  31.    
  32.     // Write Product File
  33.     $pfn = 'product' . $product_number . '.txt';
  34.     $fh = fopen($pfn, 'w') or die("Internal Error: Couldn't open File.");
  35.     fwrite($fh, $content) or die('Internal Error: Couldn\'t write to File.');
  36.     fclose($fh);
  37.    
  38.     // Upload Picture;
  39.     $img = $_FILES['file'];
  40.     $location = 'downloads/product' . $product_number . '.jpg';
  41.     if( $img['error'] === UPLOAD_ERR_OK && move_uploaded_file($img['tmp_name'], $location)){
  42.         echo 'File Uploaded!';
  43.     }else if( $img['error'] !== UPLOAD_ERR_OK){
  44.         echo 'Upload Error: ' . uploadErrorMsg($img['error']);
  45.     }else{
  46.         echo 'Internal Error: Couldn\'t move uploaded file to final location.';
  47.     }
  48.     echo '<br />';
  49.  
  50.     // Update Stats
  51.     $statsFile = 'stats' . $product_number . '.txt';
  52.     $cCount = (file_exists($statsFile))? (int) file_get_contents($statsFile) : 0;
  53.     file_put_contents($statsFile, $cCount + 1);
  54. }else{
  55.     echo 'Please Choose a file.';
  56.     error_log('Failed to choose a file.' . PHP_EOL, 3, 'txt.txt');
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement