On a PHP web site, I have an Upload Form where visitors upload a video and have an option to upload a document with the video. When a web visitor watches the uploaded video, he can select "More Info" link and see the document. When an web visitor uploads a video and skips the add-a-document option, a NoInfoAvailable.png is uploaded with the video, (so that when a web visitor watches a video and selects the "More Info" link, he sees "No Info Available" (instead of nothing). It all works successfully. The down-side is that the Upload folder accumulates more and more NoInfoAvailable.png files, each time just a video is uploaded. Any ideas on how to just show one NoInfoAvailable to all visitors who click a "More Info" link, when no document has been uploaded? Here's the current code: if (isset($_POST['form_submitted'])): // $randomString needed regardless of passing tests so put outside error condition $randomString = time(); if((isset($_FILES) && $_FILES['file']['error'] != 0) || !isset($_FILES)){ //Unable to upload file to temp //Set variable for final move/copy condition with no message $error = ''; }else{ $allowedExts = array("doc", "docx", "gif", "jpeg", "jpg", "png", "txt"); $temp = explode(".", $_FILES['file']['name']); $extension = strtolower( end($temp) ); if(!in_array($extension,$allowedExts)){ $error = '<div id="errorMessage">>> Error: Invalid File</div>'; }elseif($_FILES['file']['size'] >= 100000){ $error = '<div class="errorMessage1">>> Error: Image File Size Exceeds Limit</div>'; } } if(!isset($error)){ $uploadedFile = $_FILES['file']['tmp_name']; $thumbnail = $_SESSION['user_id'] . '-' . $randomString . "." . $extension; move_uploaded_file($uploadedFile, "upload/" . $thumbnail); }else{ //Make sure NoInfo image has png extension $thumbnail = $_SESSION['user_id'] . '-' . $randomString . ".png"; copy("upload/NoInfoAvailable.png", "upload/" . $thumbnail); } $_SESSION['thumbnail'] = $thumbnail; $file_location = '<a href="http://www...com/upload/' . $thumbnail . '">' . $thumbnail . '</a>'; endif; if(isset($error)){echo $error;} Code (markup): Someone suggested that there was no "need to copy blank image to filename, just write to DB field the file name of the blank image. Adjust your script to check if db field hold blank or actual image and use IF statement to display link something like: if ( img is default ) { $file_location = ‘No Thumbnail Available’; } else { $file_location = '<a href="https://www...com/upload/' . $thumbnail . '">' . $thumbnail . '</a>'; } Code (markup): But, I'm not sure how to add that with the existing script and mysql. Any guidance will be appreciated.
Well, since you have no mysql involved in the code you posted, the easiest would be to just remove the else {} with the copy of the noinfoavailable image, and on load, check to see if there is a document in the relevant folder (for info), and if not, just link to the noinfoavailable.
I'd also suggest using pathinfo: http://php.net/manual/en/function.pathinfo.php Instead of the explode for the file extension. Using an explode can be unreliable since filenames may have multiple periods and indexing the last involves a count -- don't brute force things PHP has functions to do. Some formatting helps clean up the logic too... I'd be approaching that more like this: if (isset($_POST['form_submitted'])) { $errors = []; $randomString = time(); if ((isset($_FILES) && $_FILES['file']['error'] == 0)) { $extension = pathInfo($_FILES['file']['name'], PATHINFO_EXTENSION); if (!in_array( $extension, ['doc', 'docx', 'gif', 'jpeg', 'jpg', 'png', 'txt'] ) { $errors[] = '» Error: Invalid File'; } else if ($_FILES['file']['size'] >= 100000) { $errors[] = '» Error: Image File Size Exceeds Limit'; } } else $errors[] = 'No files Sent'; if (count($errors)) { echo ' <ul id="errorList">'; foreach ($errors as $text) echo ' <li>', $text, '</li>'; echo ' </ul>'; } else { $uploadedFile = $_FILES['file']['tmp_name']; $thumbnail = $_SESSION['user_id'] . '-' . $randomString . "." . $extension; move_uploaded_file($uploadedFile, "upload/" . $thumbnail); } } else { $thumbnail = $_SESSION['user_id'] . '-' . $randomString . ".png"; copy("upload/NoInfoAvailable.png", "upload/" . $thumbnail); } $_SESSION['thumbnail'] = $thumbnail; $file_location = ' <a href="http://www...com/upload/' . $thumbnail . '"> ' . $thumbnail . ' </a>'; Code (markup): I might even be tempted to make all the error checks NOT be "else" so that you can list multiple rejection reasons, hence my change to a list and using an array. I also switched it to PHP 5.4+ arrays since there's NO legitimate reason to still be supporting 5.3 other than crappy hosts and crappier outdated code.
I am mind boggled whenever I see someone post unformatted code... I would not be able to effectively program if my code wasn't properly spaced, indented, and structured..