Too many duplicate files

On a PHP web site, I have an Upload Form where visitors upload a video.
I’ve added (with help) the option to choose a document to be uploaded with the video.
It works successfully. When a web visitor watches the uploaded video, he can select “More Info” link and see the document.

When an uploading web visitor 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”, if the uploading web visitor chose not to upload an optional document) 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? Or any other ideas?

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", "txt", "rtf", "pdf", "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="https://www...com/upload/' . $thumbnail . '">' . $thumbnail . '</a>';

endif;
if(isset($error)){echo $error;}

Any help/ideas will be appreciated.

Aren’t you using a database? I’d imagine you must be, so, I’d store the video and document metadata (file availability and link) in the database and only store the necessary files in the file system.

Scott

Wouldn’t it be simple to relate the filename of the uploaded file to the filename of the video, and simply have the page that delivers the video and optional “more info” file check to see if the latter exists, and if it does not, draw the “No info” graphic?

Main video filename after upload: upload/myvideo3225252.avi
More info filename after uploads: upload/myvideo3225252_info.doc (or whatever)

Although the database modification makes more sense.

Thanks for your replies.
Can you give me an idea (or example) of what I would need to change in my code, to store the NoInfoAvailable.png in the database, and then show it each time “More Info” link is selected (when no document was uploaded with the video)?

You wouldn’t store the image in the database. You would store the metadata of the document (and the video) like the filename and path. If the value of the document filename or path is null, because the user never uploaded the file, then you show the no info image.

Scott

Thanks for that explanation. Currently in the db, in the ‘videos’ table, when a video is uploaded a video_id is entered into the video_id column, and a renamed document name is entered into the ‘thumbnail’ column. The document name is either the renamed uploaded document name, or the renamed NoInfoAvailable.png name. At the same time the video file shows in the video uploads/ folder and a renamed document shows in the upload/ folder.

So, according to what you explained, it appears that the (renamed) file name is already stored in the database > videos > thumbnail. And somehow, instead of the renamed NoInfoAvailable.png, (for example 2-048464884.png), being stored in the ‘thumbnail’ column of the db, ‘null’ should appear there, so that will trigger some new code to show upload/NoInfoAvailable.png?

Is that correct?

Something along those lines. Yes.

Scott

Thanks for your reply. So, the would the code posted in my initial thread (from the uploader.php file) and the Upload Form (code below - I know it’s not state-of-the-art-code) both need to be modified for this change? Anything else? Any examples will be welcomed.

<form enctype="multipart/form-data" action="uploader.php" method="POST">
<div id="upload-video">
<ul>
<li>[var.lang_title]:</li>
<li><input name="title" type="text" class="upload-video-form-input" value="[var.title]" size="38" /></li>

 <li>[var.lang_description]:</li>
 <li><textarea rows="4" name="description" id="description" cols="29" class="upload-video-form-input">[var.description]</textarea><br /></li>
<!--<select class="upload-video-form-input" style="width:160px;" size="1" name="channel"  onchange="javascript:ahahscript.ahah('[var.base_url]/uploader.php?sub_cat='+  document.form_upload.channel.value, 'sub_change', '', 'GET', '', this);">-->
 <input type="hidden" name="channel" value="1"/>
</li>
<table id="tab1">
<tbody>
<tr><td>
<li><input type="hidden" name="public_private" value="public"></li>
<li><input class="button-form3" type="submit" value="UPLOAD" name="B3" />
</li>
</td></tr>
</tbody>
</table>
<input class="upload-video-form-input" type="hidden" name="form_submitted" value="yes" />
<table id="tab2">
<tbody>
<tr><td>
<li>(Optional): Upload a document</li>
<li><input type="file" name="file" id="file" class="upload-video-form-input2"></li>
</td></tr>
</tbody>
</table>
</form>
</div>

A quick workaround would be instead of putting the renamed Noinfoavailable.png into this column, just put “NoInfoAvailable.png” and stop copying the file.

When you present the video and optional document file, is that in the form of a link for the user to download the document? If so, how do you deal with it being nia.png, still display it as a link to download?

Thanks for your reply, however I’m not clear on this statement "instead of putting the renamed Noinfoavailable.png into this column, just put “NoInfoAvailable.png”

Regarding your link question,Yes, there is a “More Info” link. When selected, the link will show an uploaded doc, or if no doc has been uploaded the link will show the NoInfoAvailable.png, currently.
I don’t understand what you mean by “it being nia.png”
I look forward to any clarification, and any suggestions.

What I meant was, if you have a document name in the thumbnail column in your table, if the user doesn’t upload a file, instead of copying “Noinfoavailable.png” to another file and putting the name of that new file in the column, why don’t you just put “noinfoavailable.png” in the column, and not bother copying it to something else? One advantage of that approach would be that if you update the graphic to redesign the look of the site, you won’t have to copy it to loads of places that you might not be able to easily find.

What I meant by the other thing was whether you still provide the link to download the extra document even if there isn’t one. Normally if there is no additional information to download, you wouldn’t put a link there - there’s nothing more annoying than following a link and finding it just goes to “no file available” or whatever. So you’d be better of checking if there’s any extra information available, and only displaying the link if there is. And for that, leaving the column blank is probably the easiest way.

When I wrote “nia.png” I meant your “noinfoavailable.png”, I just got fed up of typing the full name.

So, in the upload program:

  • upload the video
  • store the video name in the database
  • if there is a document, upload it and store the name in the database
  • if there is not, leave that column blank

and in the display program:

  • display the video
  • if the thumbnail column is not blank, display the link to that file
  • if it is blank, don’t display the link at all.

Thanks for your replies

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.