Jump to content

Archived

This topic is now archived and is closed to further replies.

sjones

How can I overwrite image files in the uploads directory.

Recommended Posts

Does anyone know of an easy script to allow a file to be uploaded and overwrite the existing file in the uploads directory? I have a customer that uploads images and every month these images will change to the new theme. Thanks for any help.
This is the Upload script I am using.

<?php
// This file works to upload the image and store the location in the DataBase
$uploadDir = '../uploads/'; // The uploads directory is in the root folder

if(isset($_POST['upload'])) {
if ($_POST['vendor_id'] == 0) {
echo "<span class='content_blank'><H3>You did not select a Vendor! Please try again</H3></span>";
}else{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];

$filePath = $uploadDir . $fileName;

$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo "Error uploading file";
exit;
}

include ('../connections/mysql_connect.php');

if(!get_magic_quotes_gpc()) {
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
}
$query = "INSERT INTO uploads (file_name, file_size, file_type, image_path, vendor_id ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$filePath', '".$_POST['vendor_id']."')";

mysql_query($query) or die('Error, query failed : ' . mysql_error());

echo "<br><span class='content_blank'><strong>File uploaded</strong></span><br>";
}
}
?>
<form method="post" enctype="multipart/form-data">
<table width="100%" border="0" cellpadding="1" cellspacing="1">
<tr>
<td colspan="2" class="content_blank"><strong>Upload image...</strong><BR>
You must select a vendor, then browse to their coupon image
and upload the file<BR><BR>
</td>
</tr>
<tr>
<td valign="top" class="content_blank">
<?php
include ('../connections/mysql_connect.php');
echo "<strong>Select Vendor.</strong><BR>
<select name='vendor_id'>
<option>- Please select one -</option>\n";
$vendor_query = mysql_query("SELECT * FROM vendors;");
while($rows = mysql_fetch_assoc($vendor_query)) {
echo "<option value='".$rows['vendor_id']."'>".$rows['vendor_name']."</option>\n";
}
echo "</select>";
?>
</td>
<td width="246" class="content_blank" valign="top">
<strong>Browse to select file.</strong><BR>
<input type="hidden" name="MAX_FILE_SIZE" value="1000000">
<input name="userfile" type="file" id="userfile"><BR><BR>
<input name="upload" type="submit" class="box" id="upload" value=" Upload ">
</td>
</tr>
<tr>
<td></td>
</tr>
</table>
</form>

Share this post


Link to post
Share on other sites
You could include this script. (NOTE: the board does NOT like f open, f read etc. SO where ever you see those in this script I had to put a space between the f and the rest. To use the code you will need to remove the spaces.)


Its uses a small text file with the 3 character month in it. If the month in the file and the current month are the same, then the script knows NOT to delete the pictures; if, however, they are NOT the same, then it deletes all the pictures in the appropriate folder.

You will need to make sure that the clearpics.txt file is chmod 777.

Also you need to change the image directory to yours.

Finally, you can change/add/delete file type to be deleted.

Hope it is of some help.

the clearpics.txt file (example)
[code]
Apr
[/code]

the main script

[code]<?php
function clear_pics(){
    ###############################################
    # change this to the name of YOUR image folder

    $what_folder = 'images/'; // the directory, where your images are stored

    ################################################
    # this lists the type of files to be deleted

    $valid_files = array('png','jpg','jpeg','gif');

    $this_folder = opendir($what_folder);
    while($image_name = readdir($$this_folder)) {
        if(in_array(strtolower(substr($image_name,-3)),$valid_files)) {
        $full_name = $what_folder . $image_name;
        unlink($full_name);
        }
    }
}

##################################
# this is the file we use to check if the
# files need deleting

$filename = 'clearpics.txt';



##################################
# this is the current month

$month = date("M");

################################
# check to see if the file exists

if (!file_exists($filename)) {
    touch($filename);
    $fp = f open($filename, "w");
    $string = $month;
    $write = f puts($fp, $string);
    f close($fp);
}

#################################
# open the file and get the contents

$fp = f open($filename, "r");
$contents = trim(f read($fp, filesize($filename)));
f close($fp);

##########################################
# if the current month and the month stored
#        are not the same then
#        delete the files and write the current
#        month to our txt file

if ($month != $contents) {
    clear_pics();
    $fp = f open($filename, "w");
    $string = $month;
    $write = f puts($fp, $string);
    f close($fp);
}

?> [/code]

Lite...

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.