Jump to content

Recommended Posts

My current uploading process calls for random numbers and date, time etc. my image after upload will always be random and its driving me crazy because I would like to upload the image file of a same name to over write the existing. Could anyone advise me on how to do this and of any sql changes needed? Here is my code:

 

<?php
require dirname(__FILE__) . '/config.php';
session_id($_GET['auth']);
session_start();
$allowedExtensions = array(
    "jpg",
    "jpeg",
    "gif",
    "png"
);
function generateRandom($length = 20)
{
    $vowels     = 'aeuyAEUY';
    $consonants = 'bdghjmnpqrstvzBDGHJLMNPQRSTVWXZ123456789';
    $password   = '';
    $alt        = time() % 2;
    for ($i = 0; $i < $length; $i++) {
        if ($alt == 1) {
            $password .= $consonants[(rand() % strlen($consonants))];
            $alt = 0;
        } else {
            $password .= $vowels[(rand() % strlen($vowels))];
            $alt = 1;
        }
    }
    return $password;
}
function checkAccountLimit($file_size)
{
    global $qry;
    $accounts_limits = $qry->querySelectSingle("SELECT `accounts` FROM settings");
    $accounts_limits = @unserialize($accounts_limits['accounts']);
    $upload_limit    = false;
    if ($_SESSION['userid'] < 1) {
        $data = $qry->querySelectSingle("SELECT SUM(`size`) AS `total_upload` FROM `images` WHERE `session` = '" . session_id() . "'");
        if ($data['total_upload'] + $file_size > $accounts_limits['unreg_max_image_size'] * 1024 * 1024) {
            $upload_limit = true;
        }
    } else {
        $user = $qry->querySelectSingle("select * from users WHERE `id`= {$_SESSION['userid']}");
        if (!$user['pro']) {
            $data = $qry->querySelectSingle("SELECT SUM(`size`) AS `total_upload` FROM `images` WHERE `userid` = '{$_SESSION['userid']}'");
            if ($data['total_upload'] + $file_size > $accounts_limits['reg_max_image_size'] * 1024 * 1024) {
                $upload_limit = true;
            }
        }
    }
    if ($upload_limit > 0) {
        echo '{"cellId":' . $_GET['cell'] . ',"error":"Upload limit!"}';
        exit;
    }
}
ob_start();
require_once("class/sqlQuery.class.php");
list($upload_year, $upload_month) = explode('-', date('Y-m'));
$moveToDir      = UPLOAD_DIR;
$uploadFileName = $_FILES['Filedata']['name'];
$content        = sprintf('%s_%s_%d_%s', $upload_year, $upload_month, rand(1000000, 9999999), str_replace(" ", "_", $uploadFileName));
if (is_uploaded_file($_FILES['Filedata']['tmp_name'])) {
    if (in_array(end(explode(".", strtolower($_FILES['Filedata']['name']))), $allowedExtensions)) {
        $file_path = $moveToDir . $content;
        $file_size = $_FILES['Filedata']['size'];
        checkAccountLimit($file_size);
        move_uploaded_file($_FILES['Filedata']['tmp_name'], $file_path);

        if ($_SESSION['albumid'] < 1) {
            $data                = $qry->querySelectSingle("select MAX(id) as m from albums");
            $_SESSION['albumid'] = $data['m'] + 1;
            $qry->queryExecute("insert into `albums`(userid,name,code) VALUES('" . $_SESSION['albumid'] . "','','" . generateRandom() . "')");
        }
        $qry->queryExecute("insert into `images`(file_name,pos,albumid,userid,`session`,date,`size`) VALUES('" . $content . "','" . $_GET["cell"] . "','" . $_SESSION['albumid'] . "','" . $_SESSION['userid'] . "','" . session_id() . "','" . time() . "','" . $file_size . "')");
?>{"cellId":<?= $_GET['cell']; ?>,"imgId":"<?= mysql_insert_id(); ?>","album_id":<?= $_SESSION['albumid'] ?>,"name":"ul","sid":null,"img":"http://<?= DOMAIN ?>/pockets/<?= $content; ?>"}<?
    } else {
        echo '{"cellId":1,"error":"File is not image: image\/php"}';
    }
    ;
}
?>

 

Link to comment
https://forums.phpfreaks.com/topic/239168-uploading-images/
Share on other sites

wouldn't that be bad?

 

If user 1 uploads a file called 'timber.png', then user 2 uploads an image called 'timber.png' would you handle them seperately?

 

I'd assume you would, just curious though..

 

What you would do in any event, You'd save the original file name in the database, aswell as the renamed file name. Example table would be:

 

id (int) | uploader (int) | uploaded (datetime) | file_name (text) | file_name_new (text) | file_size (text) | description (text)

 

when the new file is uploaded, you'd run a query through the database like so:

 

SELECT id, file_name_new FROM uploads WHERE uploader = '{$user->id}' AND file_name = '{$oldFileName}'

 

if that returns a result, set in your php the value of 'file_name_new' from your database, store it in a variable for later.. unlink the old file which matches the name "file_name_new", then upload the file.. using the variable you stored..

 

once the upload is complete you will make another call to the database

UPDATE uploads SET uploaded = NOW() WHERE id = '{the id from the query above}'

 

and that should be it mostly

Link to comment
https://forums.phpfreaks.com/topic/239168-uploading-images/#findComment-1228829
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.