Jump to content
Larry Ullman's Book Forums

Search the Community

Showing results for tags 'add file'.

  • Search By Tags

    Type tags separated by commas.
  • Search By Author

Content Type


Forums

  • Single Editions
    • Modern Javascript: Develop and Design
    • The Yii Book
    • Effortless Flex 4 Development
    • Building a Web Site with Ajax: Visual QuickProject
    • Ruby: Visual QuickStart Guide
    • C++ Programming: Visual QuickStart Guide
    • C Programming: Visual QuickStart Guide
    • Adobe AIR: Visual QuickPro Guide
  • PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide
    • PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide (5th Edition)
    • PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide (4th Edition)
    • PHP 6 and MySQL 5 for Dynamic Web Sites: Visual QuickPro Guide (3rd Edition)
    • PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide (2nd Edition)
    • PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide (1st Edition)
  • PHP for the Web: Visual QuickStart Guide
    • PHP for the Web: Visual QuickStart Guide (5th Edition)
    • PHP for the Web: Visual QuickStart Guide (4th Edition)
    • PHP for the Web: Visual QuickStart Guide (3rd Edition)
    • PHP for the World Wide Web: Visual QuickStart Guide (2nd Edition)
    • PHP for the World Wide Web: Visual QuickStart Guide (1st Edition)
  • Effortless E-commerce with PHP and MySQL
    • Effortless E-Commerce with PHP and MySQL (2nd Edition)
    • Effortless E-Commerce with PHP and MySQL
  • PHP Advanced: Visual QuickPro Guide
    • PHP Advanced and Object-Oriented Programming: Visual QuickPro Guide (3rd Edition)
    • PHP 5 Advanced: Visual QuickPro Guide (2nd Edition)
    • PHP Advanced: Visual QuickPro Guide
  • MySQL: Visual QuickStart Guide
    • MySQL: Visual QuickStart Guide (2nd Edition)
    • MySQL: Visual QuickStart Guide (1st Edition)
  • Other
    • Announcements
    • Newsletter, Blog, and Other Topics
    • Forum Issues
    • Social

Find results in...

Find results that contain...


Date Created

  • Start

    End


Last Updated

  • Start

    End


Filter by number of...

Joined

  • Start

    End


Group


Found 1 result

  1. Hi Larry, I used your add_pdf.php script from Chapter 5 as template for an add_video.php script (below) to upload MP4 videos instead of PDF files (I only changed the PDF variable to MP4 per your script). The script executes perfectly fine and the validation works on all files except .MOV files. When I test with a .MOV upload, the script prints the following errors: An error occurred in script 'C:\xampp\htdocs\site\html\add_video.php' on line 39: Undefined index: mp4 An error occurred in script 'C:\xampp\htdocs\site\html\add_video.php' on line 39: Trying to access array offset on value of type null An error occurred in script 'C:\xampp\htdocs\site\html\add_video.php' on line 91: Undefined index: mp4 An error occurred in script 'C:\xampp\htdocs\site\html\add_video.php' on line 92: Trying to access array offset on value of type null I have search for a solution but could not find anything on Stack Overflow or any other source. Any suggestions would be appreciated. Thank you. <?php // This page is used by an administrator to add a video to the site. // Require the configuration before any PHP code as the configuration controls error reporting: require('./includes/config.inc.php'); // If the user isn't logged in as an administrator, redirect them: redirect_invalid_user('user_admin'); // Require the database connection: require(MYSQL); // Include the header file: $page_title = 'Add a Video'; include('./templates/header.html'); // For storing errors: $add_video_errors = array(); // Check for a form submission: if ($_SERVER['REQUEST_METHOD'] === 'POST') { // Check for a title: if (!empty($_POST['title'])) { $t = escape_data(strip_tags($_POST['title']), $dbc); } else { $add_video_errors['title'] = 'Please enter the title!'; } // Check for a description: if (!empty($_POST['description'])) { $d = escape_data(strip_tags($_POST['description']), $dbc); } else { $add_video_errors['description'] = 'Please enter the description!'; } // Check for a mp4: if (is_uploaded_file($_FILES['mp4']['tmp_name']) && ($_FILES['mp4']['error'] === UPLOAD_ERR_OK)) { // Get a reference: $file = $_FILES['mp4']; // Find the size: $size = ROUND($file['size']/1024); // Validate the file size (5MB max): if ($size > 3072) { $add_video_errors['mp4'] = 'The uploaded file was too large.'; } // Validate the file type: // Create the resource: $fileinfo = finfo_open(FILEINFO_MIME_TYPE); // Check the file: if (finfo_file($fileinfo, $file['tmp_name']) !== 'video/mp4') { $add_video_errors['mp4'] = 'The uploaded file was not an mp4.'; } // Close the resource: finfo_close($fileinfo); // Move the file over, if no problems: if (!array_key_exists('mp4', $add_video_errors)) { // Create a tmp_name for the file: $tmp_name = sha1($file['name']) . uniqid('',true); // Move the file to its proper folder but add _tmp, just in case: $dest = VIDEOS_DIR . $tmp_name . '_tmp'; if (move_uploaded_file($file['tmp_name'], $dest)) { // Store the data in the session for later use: $_SESSION['mp4']['tmp_name'] = $tmp_name; $_SESSION['mp4']['size'] = $size; $_SESSION['mp4']['file_name'] = $file['name']; // Print a message: echo '<div class="alert alert-success"><h3>The file has been uploaded!</h3></div>'; } else { trigger_error('The file could not be moved.'); unlink ($file['tmp_name']); } } // End of array_key_exists() IF. } elseif (!isset($_SESSION['mp4'])) { // No current or previous uploaded file. switch ($_FILES['mp4']['error']) { case 1: case 2: $add_video_errors['mp4'] = 'The uploaded file was too large.'; break; case 3: $add_video_errors['mp4'] = 'The file was only partially uploaded.'; break; case 6: case 7: case 8: $add_video_errors['mp4'] = 'The file could not be uploaded due to a system error.'; break; case 4: default: $add_video_errors['mp4'] = 'No file was uploaded.'; break; } // End of SWITCH. } // End of $_FILES IF-ELSEIF-ELSE. if (empty($add_video_errors)) { // If everything's OK. // Add the video to the database: $fn = escape_data($_SESSION['mp4']['file_name'], $dbc); $tmp_name = escape_data($_SESSION['mp4']['tmp_name'], $dbc); $size = (int) $_SESSION['mp4']['size']; $q = "INSERT INTO videos (title, description, tmp_name, file_name, size) VALUES ('$t', '$d', '$tmp_name', '$fn', $size)"; $r = mysqli_query($dbc, $q); if (mysqli_affected_rows($dbc) === 1) { // If it ran OK. // Rename the temporary file: $original = VIDEOS_DIR . $tmp_name . '_tmp'; $dest = VIDEOS_DIR . $tmp_name; rename($original, $dest); // Print a message: echo '<div class="alert alert-success"><h3>The MP4 has been added!</h3></div>'; // Clear $_POST: $_POST = array(); // Clear $_FILES: $_FILES = array(); // Clear $file and $_SESSION['mp4']: unset($file, $_SESSION['mp4']); } else { // If it did not run OK. trigger_error('The MP4 could not be added due to a system error. We apologize for any inconvenience.'); unlink ($dest); } } // End of $errors IF. } else { // Clear out the session on a GET request: unset($_SESSION['mp4']); } // End of the submission IF. // Need the form functions script, which defines create_form_input(): require('includes/form_functions.inc.php'); ?><h1>Add a Video</h1> <form enctype="multipart/form-data" action="add_video.php" method="post" accept-charset="utf-8"> <input type="hidden" name="MAX_FILE_SIZE" value="5242880"> <fieldset><legend>Fill out the form to add a video to the site:</legend> <?php create_form_input('title', 'text', 'Title', $add_video_errors); create_form_input('description', 'textarea', 'Description', $add_video_errors); // Add the file input: echo '<div class="form-group'; // Add classes, if applicable: if (array_key_exists('mp4', $add_video_errors)) { echo ' has-error'; } else if (isset($_SESSION['mp4'])) { echo ' has-success'; } echo '"><label for="mp4" class="control-label">Add MP4 file</label><input type="file" name="mp4" id="mp4">'; // Check for an error: if (array_key_exists('mp4', $add_video_errors)) { echo '<span class="help-block">' . $add_video_errors['mp4'] . '</span>'; } else { // No error. // If the file exists (from a previous form submission but there were other errors), // store the file info in a session and note its existence: if (isset($_SESSION['mp4'])) { echo '<p class="lead">Currently: "' . $_SESSION['mp4']['file_name'] . '"</p>'; } } // end of errors IF-ELSE. echo '<span class="help-block">MP4 only, 3MB Limit</span> </div>'; ?> <input type="submit" name="submit_button" value="Add This Video" id="submit_button" class="btn btn-default" /> </fieldset> </form> <?php // Include the HTML footer: include('./templates/footer.html'); ?>
×
×
  • Create New...