Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Yahoo Answers Question http://answers.yahoo.com/question/index?qid=20130617070959AAgg2Zp
- /*** Variables ***/
- $message = '';
- /*** Functions ***/
- function uploadErrorMsg( $error ){
- $errors = array(
- 1 => 'The uploaded file exceeds the Server\'s Maximum Allowable File Size',
- 2 => 'The uploaded file exceeds the Form\'s Maximum Allowable File Size.',
- 3 => 'The uploaded file was only partially uploaded, then interrupted or the connection was dropped.',
- 4 => 'No file was uploaded.',
- 6 => 'Missing a temporary folder. The server requires a temporary folder for file uploads.', // Server Setup Error
- 7 => 'Failed to write file to disk.', // Internal Server Error
- 8 => 'A PHP extension stopped the file upload.' // PHP Extension Library stopped the upload.
- );
- return $errors[(int) $error];
- }
- /*** Processing ***/
- if( $_SERVER[ 'REQUEST_METHOD' ] === 'POST' && isset($_POST['file']) && trim($_POST['file']) !== ''){
- // Variables
- $file = (isset($_POST['file']))? trim($_POST['file']) : '';
- $num = (int) str_replace(Array('product','.txt'), '', $file);
- $content = (isset($_POST['content']))? trim( $_POST['content'] ) : '';
- $image = (isset($_FILES['image']))? $_FILES['image'] : false;
- $imageFile = 'downloads/product' . $num . '.jpg';
- $statsFile = 'stats' . $num . '.txt';
- // Replace/Write Content Of Product File
- $fh = @fopen($file, 'w');
- if( !$fh ){
- $message .= 'Couldn\'t Open File: "' . $file . '" (Line: ' . (__LINE__ - 2) . ')<br />';
- }else if( !fwrite($fh, $content) ){
- $message .= 'Couldn\'t Write Out to File. (Line: ' . (__LINE__ - 1) . ')<br />';
- }else if( is_resource($fh) ){
- @fclose($fh);
- $message .= 'Product File Content Added!<br />';
- }
- // Image Upload
- if($image && $image['error'] === UPLOAD_ERR_OK && ($image['type'] === 'image/jpg' || $image['type'] === 'image/jpeg')){
- if(move_uploaded_file( $image['tmp_bame'], $imageFile) ){
- $message .= 'Image Uploaded Successfully!<br />';
- }else{
- $message .= 'Image Upload Failed, due to internal error. Check folder permissions (line: ' . (__LINE__ - 3) . ').<br />';
- }
- }else if($image['type'] !== 'image/jpg' && $image['type'] !== 'image/jpeg'){
- $message .= 'Image is not a JPEG. Only JPEG are formatted to work.<br />';
- }else if( !$image ){
- $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.
- }else{
- $message .= 'Image Upload Error: (' . $image['error'] . ') ' . uploadErrorMsg($image['error']) . '<br />';
- }
- // Stats
- $fh = @fopen($statsFile, 'w+');
- $size = filesize($statsFile);
- if( !is_resource($fh) ){
- $message .= 'Cannot Open Stats file "' . $statsFile . '" (Line: ' . (__LINE__ - 2) . '). Check Permissions.<br />';
- }else{
- $stat = (file_exists($statsFile) && $size > 0)? @fread($fh, $size) : 0;
- if( $stat === false){
- $message .= 'Couldn\'t Read From Stat File (Line: ' . (__LINE__ - 2) . '). Check Permissions.<br />';
- }else{
- $stat++;
- if( @fwrite($fh, $stat) ){
- $message .= 'Stat File updated.<br />';
- }else{
- $message .= 'Couldn\'t write to stats file (Line: ' . (__LINE__ - 3) . '). Check Permissions.<br />';
- }
- }
- }
- }else if( $_SERVER[ 'REQUEST_METHOD' ] === 'POST' ){
- $message .= 'Please select a Product File.<br />';
- }
- /*** Message Formatting ***/
- if( isset($message{1}) ){
- $message = ' <div style="padding: 0;border: 1px solid #CCC;">
- <h3 style="background-color: #CCC;margin: 0;padding: 4px;">Message</h3>
- <p style="padding: 8px;">
- ' . $message . '
- </p>
- </div>';
- }
- /*** HTML Output ***/
- echo '<!DOCTYPE html>
- <html lang="en-us">
- <head>
- <title>Change Top 3 cars</title>
- </head>
- <body>
- ' . $message . '
- <form method="post" action="connor_lockett_p5.php" enctype="multipart/form-data">
- New Content: <input type="text" name="content" />
- <br />
- New Image: <input type="file" name="image" size="10" />
- <br />
- Products:
- <select name="file">
- <option value="product1.txt">Product 1</option>
- <option value="product2.txt">Product 2</option>
- <option value="product3.txt">Product 3</option>
- </select>
- <br />
- <input type="submit" value="Upload" />
- </form>
- </body>
- </html>';
Advertisement
Add Comment
Please, Sign In to add comment